Skip to content

Instantly share code, notes, and snippets.

@sadache
Created April 20, 2010 09:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save sadache/372254 to your computer and use it in GitHub Desktop.
Save sadache/372254 to your computer and use it in GitHub Desktop.
What is/is_not a function in Scala
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))
}
}
@sadache
Copy link
Author

sadache commented Apr 20, 2010

updated to remove ambiguity with println taking a String

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