Scala equivalent of C#’s extension methods?
ScalaExtension MethodsScala Problem Overview
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?
Scala Solutions
Solution 1 - Scala
The Pimp My Library pattern is the analogous construction:
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)
}
Solution 2 - Scala
Since version 2.10 of Scala, it is possible to make an entire class eligible for implicit conversion
implicit class RichInt(i: Int) {
def square = i * i
}
In addition, it is possible to avoid creating an instance of the extension type by having it extend AnyVal
implicit class RichInt(val i: Int) extends AnyVal {
def square = i * i
}
For more information on implicit classes and AnyVal, limitations and quirks, consult the official documentation:
Solution 3 - Scala
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 )
}
}
Solution 4 - Scala
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.
Solution 5 - Scala
Scala 3 now has extension methods. Functionally it seems similar as expected to C# and Kotlin.
https://dotty.epfl.ch/docs/reference/contextual/extension-methods.html<br/> https://github.com/scala/scala<br/> https://github.com/lampepfl/dotty
A recent (as of this post) pull shows the syntax being simplified. Stable version as of this post is still 2.x. But there is a 3.xRC, and I noticed Jetbrains already supports it in Idea, partially I assume.