Created
August 25, 2014 18:38
-
-
Save wrobstory/7b5777a2732729717cc7 to your computer and use it in GitHub Desktop.
Scala Default Args
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
scala> def addStringOneParam(input: String): String = input + "foo" | |
addStringOneParam: (input: String)String | |
scala> def addStringDefParam(input: String, toAdd: String = "foo"):String = input + toAdd | |
addStringDefParam: (input: String, toAdd: String)String | |
scala> addStringOneParam("baz") | |
res3: String = bazfoo | |
scala> addStringDefParam("baz") | |
res4: String = bazfoo | |
scala> val myMap = Map("one" -> "foo", "two" -> "bar") | |
myMap: scala.collection.immutable.Map[String,String] = Map(one -> foo, two -> bar) | |
scala> myMap.mapValues(addStringOneParam) | |
res5: scala.collection.immutable.Map[String,String] = Map(one -> foofoo, two -> barfoo) | |
scala> myMap.mapValues(addStringDefParam) | |
<console>:11: error: type mismatch; | |
found : (String, String) => String | |
required: String => ? | |
myMap.mapValues(addStringDefParam) | |
scala> val mySeq = Array("one", "two", "three") | |
mySeq: Array[String] = Array(one, two, three) | |
scala> mySeq.map(addStringOneParam) | |
res8: Array[String] = Array(onefoo, twofoo, threefoo) | |
scala> mySeq.map(addStringDefParam) | |
<console>:11: error: type mismatch; | |
found : (String, String) => String | |
required: String => ? | |
mySeq.map(addStringDefParam) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here's the answer to "why": http://stackoverflow.com/a/5582400