Created
October 1, 2010 19:48
-
-
Save sadache/606744 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
def substract(i:Int)=extension{ | |
//owner represents the object you are extending | |
owner:Int => | |
{ | |
owner-i | |
} | |
} | |
//operator tild ~ instead of . for extensions | |
1~substract(2)~substract(1)~substract(1) | |
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
//implementation of extension methods in scala: | |
object ExtensionFunctions{ | |
case class ExtensionFunction[A,R](f:Function[A,R]) extends Function[A,R]{override def apply(a:A)=f(a)} | |
def extension[A,R](f:A=>R)=ExtensionFunction(f) | |
implicit def anythingIsExtensible[A](a:A):ExtensionsOn[A]= ExtensionsOn(a) | |
case class ExtensionsOn[A](a:A){ | |
//adding this generic operator on type Any would ?remove? the overhead of doing implicit conversions here | |
def ~[R] (f:ExtensionFunction[A,R]):R = f(a) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment