[Issue 12461] Typedef and opOpAssign

via Digitalmars-d-bugs digitalmars-d-bugs at puremagic.com
Wed Apr 23 14:49:23 PDT 2014


https://issues.dlang.org/show_bug.cgi?id=12461

--- Comment #4 from bearophile_hugs at eml.cc ---
(In reply to Andrej Mitrovic from comment #3)

> struct UserTypedef(T)
> {
>     mixin MixOverload!"opUnary";  // implement opUnary
>     mixin MixOverload!"opBinary";  // implement opBinary
> }
> 
> This is probably simpler than having a super-complicated generic Typedef
> structure such as:
> 
> struct Typedef(bool useOpUnary, bool useOpBinary, ...);

Note: I didn't open this enhancement request.

A simpler solution is to offer a way to get the underlying value:

a.get += b.get;


In Haskell you can define the Num typeclass operations of a newclass, or just
define a simple function that uses pattern matching, and I use pattern matching
again to print it:

newtype T = T Int

add :: T -> T -> T
T a `add` T b = T (a + b)

main = do
    let a = T 10
    let b = T 20
    let c = a `add` b
    let T d = c
    print d

--


More information about the Digitalmars-d-bugs mailing list