Skip to content

Instantly share code, notes, and snippets.

@deanwampler
Forked from sadache/scala-functions.scala
Created April 20, 2010 13:02
Show Gist options
  • Save deanwampler/372400 to your computer and use it in GitHub Desktop.
Save deanwampler/372400 to your computer and use it in GitHub Desktop.
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))
}
}
@sadache
Copy link

sadache commented Apr 20, 2010

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment