www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Multiple implicit type converters

reply "Bahman Movaqar" <b.movaqar gmail.com> writes:
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
Sep 11 2015
next sibling parent reply "Adam D. Ruppe" <destructionator gmail.com> writes:
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?
multiple alias this is supposed to work and might some day fyi But for today, the explicit is the only way to go. That's easy to do, just write like a .get method or something that does the conversion and returns it.
Sep 11 2015
parent "Bahman Movaqar" <b.movaqar gmail.com> writes:
On Friday, 11 September 2015 at 16:31:46 UTC, Adam D. Ruppe wrote:
 explicit is the only way to go. That's easy to do, just write 
 like a .get method or something that does the conversion and 
 returns it.
Fair enough. Type conversion is one of those spots that I'd like it to as explicit as possible.
Sep 11 2015
prev sibling parent reply "Meta" <jared771 gmail.com> writes:
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
Sep 11 2015
parent reply "Bahman Movaqar" <b.movaqar gmail.com> writes:
On Friday, 11 September 2015 at 16:33:52 UTC, Meta wrote:
 The only ways to get implicit conversion between two types in D 
 are through `alias this`, inheritance, or implementing an 
 interface.
That's enough for me, I suppose. I am thinking of having a family of functions in my structs/classes as `as` family, such as `asDouble`, `asFooBar`.
Sep 11 2015
parent reply "Dave Akers" <dave dazoe.net> writes:
On Friday, 11 September 2015 at 19:34:46 UTC, Bahman Movaqar 
wrote:
 On Friday, 11 September 2015 at 16:33:52 UTC, Meta wrote:
 The only ways to get implicit conversion between two types in 
 D are through `alias this`, inheritance, or implementing an 
 interface.
That's enough for me, I suppose. I am thinking of having a family of functions in my structs/classes as `as` family, such as `asDouble`, `asFooBar`.
Would it be possible to create it as an 'as' template?
Sep 11 2015
next sibling parent "Bahman Movaqar" <b.movaqar gmail.com> writes:
On Friday, 11 September 2015 at 19:51:09 UTC, Dave Akers wrote:
 That's enough for me, I suppose.
 I am thinking of having a family of functions in my 
 structs/classes as `as` family, such as `asDouble`, `asFooBar`.
Would it be possible to create it as an 'as' template?
Hmm...there's already the `to` template, right?
Sep 11 2015
prev sibling parent "Adam D. Ruppe" <destructionator gmail.com> writes:
On Friday, 11 September 2015 at 19:51:09 UTC, Dave Akers wrote:
 Would it be possible to create it as an 'as' template?
Yeah, the way I'd do it is something like: T as(T)() { import std.traits; static if(isIntegral!T) return to!T(convert_to_some_int); else static if(isSomeString!T) return to!T(convert_to_some_string); else /// and so on and so forth } These isX templates from std.traits will help you break down a gazillion potential types into just a handful of type families which are easier to handle. But you could also just static if(is(T == something)) or whatever to list the types you do want to support.
Sep 11 2015