Multiple implicit type converters

Meta via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Sep 11 09:33:50 PDT 2015


On Friday, 11 September 2015 at 16:25:53 UTC, Bahman Movaqar 
wrote:
> As only one `alias this` is possible for any type, how should 
> one implement multiple implicit type converters?
>
> Actually I'm looking for something similar to Groovy's `asType` 
> method[1].  An example in Groovy:
>
>     Point p = new Point(1, 1)
>     assert (p as BigDecimal[]) == [1, 1]
>     assert (p as BigDecimal) == Math.sqrt(2)
>     assert (p as Region) == new Region(p, p)
>
> This allows for multiple type converters which are *explicit* 
> --in contrast to `alias this` implicit nature.
>
> [1] 
> http://docs.groovy-lang.org/latest/html/groovy-jdk/java/util/Collection.html#asType%28java.lang.Class%29

The only ways to get implicit conversion between two types in D 
are through `alias this`, inheritance, or implementing an 
interface. There is a pull request open for multiple alias-this, 
but it has yet to be pulled.

https://github.com/D-Programming-Language/dmd/pull/3998


More information about the Digitalmars-d-learn mailing list