[Issue 17921] New: allow to use `alias function this` to be used for implicit conversions
d-bugmail at puremagic.com
d-bugmail at puremagic.com
Fri Oct 20 21:47:01 UTC 2017
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 at puremagic.com
Reporter: code at 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.
--
More information about the Digitalmars-d-bugs
mailing list