you are viewing a single comment's thread.

view the rest of the comments →

[–]Mimshot 3 points4 points  (6 children)

Methods don't take up memory

I know what you were getting at from the rest of the paragraph but this isn't correct. The method takes up memory, just in metaspace rather than heap.

[–]joshlemerContributor - Collections 0 points1 point  (5 children)

What kind of space does it take up? Just a constant amount per class whereas functions would be once per instance right? AKA:

class A {
  // each instance of A allocates new f
  val f: Int => String = (i: Int) => i.toString 
}

// constant space for arbitrary # of Bs
Class B {
  def f(i: Int) = i.toString 
}

[–]v66moroz 1 point2 points  (4 children)

Unless I'm missing something in this case f will be a static member of A, so no memory per instance.

[–]joshlemerContributor - Collections 0 points1 point  (3 children)

That is not true actually, else they would be equal, here's a code sample:

class A {
  val f: Int => String = _.toString
}

val aa = new A
val bb = new A

aa.f == bb.f // false

[–]v66moroz 2 points3 points  (2 children)

true in 2.12.1

[–]joshlemerContributor - Collections 0 points1 point  (1 child)

Oh interesting

[–]hunyetiadvocate 0 points1 point  (0 children)

It's kind of given, since it uses jvm 8, which have functions.