www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 17921] New: allow to use `alias function this` to be used for

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

          Issue ID: 17921
           Summary: allow to use `alias function this` to be used for
                    implicit conversions
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: enhancement
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: code dawg.eu

cat > enh.d << CODE
struct IPv4Addr
{
  alias IPAddr this; // alias type constructor for implicit conversion
}

// either IPv4 or IPv6
struct IPAddr
{
  this(IPv4Addr) {}
}

void func(IPAddr)
{
}

unittest
{
    func(IPv4Addr());
}
CODE
dmd -c -unittest enh.d
----
enh.d(3): Error: IPAddr is not a member of IPv4Addr
----

At the moment alias this for implicit conversions needs a dummy member method.
When you already have `IPAddr(IPv4Addr.init)` it's somewhat annoying to add
`IPv4Addr.init.toIPAddr` to the public API, only to support implicit
conversions. Now there are 2 ways to do the same thing, sth. that's usually to
be avoided in API design.

Alias-thising a free function or type would be resolved like `var.func` calls,
and according to the current alias this rules.

Obviously this should be coordinated with the multiple alias this work.

--
Oct 20