% scala3
Welcome to Scala version 2.11.0-20120905-004841-4f9851d1fd (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_31).
Type in expressions to have them evaluated.
Type :help for more information.
scala> final case class UInt(val x: Int) extends AnyVal with Ordered[UInt] {
def toLong = 0x00000000FFFFFFFFL & x
def compare(other: UInt) = toLong compare other.toLong
def +(other: UInt) = UInt((toLong + other.toLong).toInt)
override def toString = toLong + "(u)"
}
defined class UInt
scala> implicit def mkUInt(x: Int) = new UInt(x)
wmkUInt: (x: Int)UInt
scala> def f(x: UInt, y: UInt) = println(s"$x < $y ? ${x < y}")
f: (x: UInt, y: UInt)Unit
scala> 1 << 30
res0: Int = 1073741824
scala> 1 << 31
scala> f(1 << 30, 1 << 31)
scala> UInt(1 << 30) + UInt(1 << 31)
res3: UInt = 3221225472(u)
scala> class A { def f(x: UInt, y: UInt) = x + y }
defined class A
scala> :javap -v A
Compiled from "<console>"
public class A extends java.lang.Object
...
public int f(int, int);
Code:
Stack=3, Locals=3, Args_size=3
0: getstatic #13; //Field UInt$.MODULE$:LUInt$;
3: iload_1
4: iload_2
5: invokevirtual #16; //Method UInt$.extension$$plus:(II)I
8: ireturn
scala> :javap -v UInt$
...
public final long extension$toLong(int);
0: ldc2_w #57; //long 4294967295l
3: iload_1
4: i2l
5: land
6: lreturn
public final int extension$$plus(int, int);
0: aload_0
1: iload_1
2: invokevirtual #70; //Method extension$toLong:(I)J
5: aload_0
6: iload_2
7: invokevirtual #70; //Method extension$toLong:(I)J
10: ladd
11: l2i
12: ireturn