0

Say I have a method like the following (curried for better type inference)

def dropWhile[A](l: List[A]) (f: A => Boolean): List[A] = {

  l match {
    case h::t if f(h) => dropWhile(t)(f)
    case _ => l
  }
}

and then I call it as follows

scala> val lst = (1 to 10).toList
lst: List[Int] = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)

scala> dropWhile(lst) ( _ <= 4)
res0: List[Int] = List(5, 6, 7, 8, 9, 10)

and get the expected result back. My question is

lst is a list with its own memory address. By default, method arguments are passed by value so in

dropWhile(lst) ( _ <= 4)

lst will be copied over to method parameter l? and so the method will have its own copy in l and what is more, each recursive invocation should have its own copy? In that context, how does data sharing work?

  • 1
    Does this answer your question? [Is Java "pass-by-reference" or "pass-by-value"?](https://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value) – user Jul 22 '20 at 15:35
  • 2
    @LuisMiguelMejíaSuárez: This question isn't about the JVM, it is about Scala. And Scala only has pass-by-value (by default) and call-by-name (for by-name parameters). Scala *never* does pass-by-reference. Not on the JVM, and also not in Scala.js, Scala-native, or Scala-CLR. The parameter passing convention of the underlying host environment is *completely and utterly irrelevant* because any Scala implementation *must* comply with the Scala Language Specification, which specifies pass-by-value and call-by-name. – Jörg W Mittag Jul 23 '20 at 04:45
  • Didn't know that passing a reference of an object was called pass by value. – Luis Miguel Mejía Suárez Jul 23 '20 at 05:03
  • @Jörg W Mittag thanks for sharing your thoughts. The intention of my question was to understand data sharing (an attribute of purely functional object like immutable Lists) in the context of method evaluation. So if the method has it's own copy to work with, where and how does data sharing come into play? – Rupam Bhattacharjee Jul 23 '20 at 13:09
  • @RupamBhattacharjee it has its own copy of the address, but the data itself is the same one. Also, data sharing is used for things like `foo :: foos` where you can create a new list which is composed of another list and a new element. Give the list is immutable and will never change, you can reuse the old one without copying data. – Luis Miguel Mejía Suárez Jul 23 '20 at 13:32
  • @Luis Miguel Mejía Suárez thank you! in other words, what gets passed is a pointer to the actual list created in the calling program? – Rupam Bhattacharjee Jul 23 '20 at 14:27
  • 1
    @RupamBhattacharjee more or less, you really do not need to think in a pointer, it is more higher level. You can just think that you have the same object. – Luis Miguel Mejía Suárez Jul 23 '20 at 15:05

1 Answers1

3

lst will be copied over to method parameter l?

No, only the reference to the list will be copied as lst is passed in to dropWhile, not the elements of the list. The effect is both lst and l will each have a copy of the reference that points to the same object in memory.

Mario Galic
  • 47,285
  • 6
  • 56
  • 98