A few more placeholder instances that I have remembered or was reminded of.
- scala> class MyClass {
- | var a:Int = _
- | def countDown = 10 to 5 by -1
- | }
- defined class MyClass
- scala> val obj = new MyClass()
- obj: MyClass = MyClass@6ff0239
- scala> val countDownMethod = obj.countDown _
- countDownMethod: () => Range = < function>
- scala> def multiple(a:Int)(b:Int) = a*b
- multiple: (Int)(Int)Int
- scala> val triple = multiple(3) _
- triple: (Int) => Int = < function>
- scala> List(1,2,3) foreach { _ => Console.println("Hello") }
- Hello
- Hello
- Hello
- initialize a variable to its default value. If the = _ is left out then the definition will be an abstract declaration and will have to be defined in subclasses
- create a reference to the method (rather than invoking the method and having a reference to the resulting value) (Thanks Daniel)
- This is an example of currying; a new function is created with a single parameter.
- where the underscore is used as an ignored and unnamed parameter (Thanks Alex)
This one I recalled yesterday, answering a friend. It's devious:
ReplyDeletexml match {
case m @ <tag>{_*}</tag> => m
}
That means pretty much "anything". One can also extract that, as an array buffer of nodes:
xml match {
case <tag>{m @ _*}</tag> => m
}
Of course, one can also use it normally in matches:
optional match {
case Some(_) => println("Something there")
case None => println("Nothing to see, move along")
}
And it can be used in the "_*" notation as well (see scaladoc for object Elem).
How about "case _ =>" in pattern matching?
ReplyDeleteI've got a #2 problem (and yes, Im proud of the pun).
ReplyDeleteI want to achieve the following:
def oneParamFcn(n: Int) = n + 10
def noParamFcn = oneParamFcn(1)
var noParamFcnVar = noParamFcn
noParamFcn is useless in this case. Is there a shorthand way to assign noParamFcnVar to be a function (instead of the result of calling that function)? Something like this (but it doesn't compile):
def oneParamFcn(n: Int) = n + 10
var noParamFcnVar = oneParamFcn(1) _
results in
found : Unit
required : () => Unit
(Maybe if I specify the type of noParamFcnVar...? But what is the type... I guess () => Unit?)
Update: This is the best I have been able to do (not the greatest notation):
ReplyDeletevar noParamFcnVar = () => oneParamFcn(1)
This is kind of lame because I've got other functions in the block that do take parameters after the currying and they need the underscore notation to end up as PartialFunctions. It just doesn't work for creating PartialFunctions that take zero parameters (I'm not sure if such a thing even exists in Scala).
Sorry to say that the best I can come up with is also:
ReplyDeletevar noParamFcnVar = () => oneParamFcn(1)
I am believe the reason is that the _ is a placeholder but in your example it is not standing in for anything, you want it to turn a method call into a function, which is not its primary functionality.
Just curious about what your final goal is. Do you want to be able to simply assign functions to a variable? If so then I am afraid you are stuck with that syntax.
Yep, thats what I was trying to do. The notation isn't bad but now I have something like:
ReplyDeletedef oneParam(i: Int)
def twoParam(i: Int, j: Int)
var noParam1 = () => oneParam(1)
var noParam2 = twoParam(2) _
which doesn't look like noParam1 and noParam2 are built using the same 'concept' but they are. I could do:
var noParam2 = (i: Int) => twoParam(2, i)
but then you lose the convenient short-hand...