Created
April 20, 2010 09:53
-
-
Save sadache/372254 to your computer and use it in GitHub Desktop.
What is/is_not a function in Scala
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
object looksLikeAFunction{ | |
def apply(s:String):Int=1 | |
} | |
object nowAFunction extends Function[String,Int]{ | |
def apply(s:String):Int=1 | |
} | |
object TestMethods { | |
implicit def functionToAnother(f:Function[String,Int]):Function[Int,String] = | |
(i:Int)=> f(i.toString()).toString() | |
def someMethod(s:String):Int=1 | |
def someFunction:Function[String,Int]= s=>1 | |
def main(args:Array[String]){ | |
// Wouldn't compile: looks like methods aren't functions? | |
//println(someMethod(1)) | |
//objects aren't functions either if they don't implement Function trait | |
//println(looksLikeAFunction(1)) | |
// an object that is a function | |
println(nowAFunction(1)) | |
//a function | |
println(someFunction(1)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
updated to remove ambiguity with println taking a String