On Thu, May 09, 2013 at 02:52:34PM +0200, Mark Hammons wrote:
> Right now I'm having trouble with overloading. I have function a with
> signatures Int => UInt and UInt => UInt. It fails to compile because of
> double definition. Is there a way to get past this issue?
I don't think you can do this without introducing a third type:
case class UIntBoxed(u: UInt) { ... }
class UInt(val n: Int) extends AnyVal {
implicit def box = UIntBoxed(this)
}
object Qux {
def apply(n: Int) = ...
def apply(n: UIntBoxed) = ...
}
Of course, this will negate the runtime benefits of using a value
class in the first place. In my opinion, overloading based solely on
value class types is antithetical to their purpose--it would be better
to use different method names or have the user provide some other
indicator of desired behavior.
-- Erik
P.S. If you're defining UInt you may be interested to compare your
implementation with Spire's:
https://github.com/non/spire/blob/2.10.0/core/src/main/scala/spire/math/UInt.scala