-
-
Save deanwampler/372400 to your computer and use it in GitHub Desktop.
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]){ | |
// Actually will compile if you pass it a String: | |
println("someMethod: " + someMethod("1")) | |
// Objects don't have to implement the Function trait: | |
println("looksLikeAFunction: " + looksLikeAFunction("1")) | |
// an object that is a function | |
println("nowAFunction: " + nowAFunction(1)) | |
//a function | |
println("someFunction: " + someFunction(1)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Actually the point was to show the difference between 1,2 and 3,4
3,4 the implicit conversion applies since they have the desired types. This is not true for 1 and 2