digitalmars.D.learn - Multiple implicit type converters
- Bahman Movaqar (12/12) Sep 11 2015 As only one `alias this` is possible for any type, how should one
- Adam D. Ruppe (6/8) Sep 11 2015 multiple alias this is supposed to work and might some day fyi
- Bahman Movaqar (3/6) Sep 11 2015 Fair enough. Type conversion is one of those spots that I'd like
- Meta (7/19) Sep 11 2015 The only ways to get implicit conversion between two types in D
- Bahman Movaqar (4/7) Sep 11 2015 That's enough for me, I suppose.
- Dave Akers (3/10) Sep 11 2015 Would it be possible to create it as an 'as' template?
- Bahman Movaqar (2/6) Sep 11 2015 Hmm...there's already the `to` template, right?
- Adam D. Ruppe (15/16) Sep 11 2015 Yeah, the way I'd do it is something like:
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
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
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
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%29The 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
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
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:Would it be possible to create it as an 'as' template?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
On Friday, 11 September 2015 at 19:51:09 UTC, Dave Akers wrote:Hmm...there's already the `to` template, right?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
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