views:

221

answers:

3

In C# you can write:

using System.Numerics;
namespace ExtensionTest {
public static class MyExtensions {
    public static BigInteger Square(this BigInteger n) {
        return n * n;
    }
    static void Main(string[] args) {
        BigInteger two = new BigInteger(2);
        System.Console.WriteLine("The square of 2 is " + two.Square());
    }
}}

How would this simple extension method look like in Scala? Thank you.

+10  A: 

The "Pimp My Library" pattern is the analogous construction: http://www.artima.com/weblogs/viewpost.jsp?thread=179766

object MyExtensions {
  implicit def richInt(i: Int) = new {
    def square = i * i
  }
}


object App extends Application {
  import MyExtensions._

  val two = 2
  println("The square of 2 is " + two.square)

}

Per @Daniel Spiewak's comments, this will avoid reflection on method invocation, aiding performance:

object MyExtensions {
  class RichInt(i: Int) {
    def square = i * i
  }
  implicit def richInt(i: Int) = new RichInt(i)
}
Mitch Blevins
Warning: by using the `new { ... }` syntax, you're actually causing the compiler to infer a structural type for the `richInt` method, meaning that any invocations of `square` will be done using reflection (a performance killer). It would be better to define a `RichInt` class which defines `square` and is returned from the `richInt` method.
Daniel Spiewak
@Daniel, could you post the code that you're describing?
OscarRyz
@Support Add `class RichInt(i: Int) { def square = i * i }` to `MyExtensions` and do `new RichInt(i)` on the `richInt` method.
Daniel
@Daniel, great, I have an answer with your comment.
OscarRyz
@Daniel interesting! I did a small test, the version with the structural type is indeed much slower: http://www.scala-notes.org/2010/06/avoid-structural-types-when-pimping-libraries/
Jesper
@Jesper You can even get a little less sneaky and do the following: `val a = new { def b() { error("") } }; a.b()`. This way, you can inspect the stack trace.
Daniel Spiewak
+3  A: 

In Scala we use the so-called (by the inventor of the language) Pimp My Library pattern, which is much discussed and pretty easy to find on the Web, if you use a string (not keyword) search.

Randall Schulz
+1  A: 

This would be the code after Daniel's comment.

object MyExtensions {
    class RichInt( i: Int ) {
        def square = i * i
    }
    implicit def richInt( i: Int ) = new RichInt( i )

    def main( args: Array[String] ) {
        println("The square of 2 is: " + 2.square )
    }
}
OscarRyz