www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - For the new std.variant

reply Steve Teale <steve.teale britseyeview.com> writes:
Robert,

Maybe you can fix this along the way:

import std.variant;
struct B
{
   int p, q, r, s;
}
typedef B C;

void main()
{
   B b;
   C c;
   b = c;   // ok
   Variant v = c;
   assert(v.convertsTo!(B));   // no dice
}

Steve
Oct 31 2011
next sibling parent reply Jesse Phillips <jessekphillips+D gmail.com> writes:
typedef shouldn't exist anymore, use alias.

Steve Teale Wrote:

 Robert,
 
 Maybe you can fix this along the way:
 
 import std.variant;
 struct B
 {
    int p, q, r, s;
 }
 typedef B C;
 
 void main()
 {
    B b;
    C c;
    b = c;   // ok
    Variant v = c;
    assert(v.convertsTo!(B));   // no dice
 }
 
 Steve
Oct 31 2011
parent reply Steve Teale <steve.teale britseyeview.com> writes:
On Mon, 31 Oct 2011 11:50:42 -0400, Jesse Phillips wrote:

 typedef shouldn't exist anymore, use alias.
Jesse, Maybe you want to do something like: struct X { int a; double b; } typedef X Y; alias X Z; void discriminate(T)(T t) { if (is(T == X)) writeln("It's an X"); else writeln("No it's not"); } void main() { Y y; Z z; discriminate(y); discriminate(z); } Steve
Oct 31 2011
parent reply Jesse Phillips <jessekphillips+D gmail.com> writes:
I literally meant it is not supposed to exist. There was a suggestion to even
axe it from 2.056 I believe.

A library solution is intended to be created which will allow for the type to
act as either a parent or child to the type.



So I understand its usage, but you shouldn't use it because it won't be there.
Oct 31 2011
parent reply Trass3r <un known.com> writes:
 I literally meant it is not supposed to exist. There was a suggestion to  
 even axe it from 2.056 I believe.
At least they prepared to actually deprecate it.
 A library solution is intended to be created which will allow for the  
 type to act as either a parent or child to the type.
http://d.puremagic.com/issues/show_bug.cgi?id=5467
Oct 31 2011
parent Steve Teale <steve.teale britseyeview.com> writes:
On Mon, 31 Oct 2011 21:03:39 +0100, Trass3r wrote:

 I literally meant it is not supposed to exist. There was a suggestion
 to even axe it from 2.056 I believe.
At least they prepared to actually deprecate it.
 A library solution is intended to be created which will allow for the
 type to act as either a parent or child to the type.
http://d.puremagic.com/issues/show_bug.cgi?id=5467
OK, my use case is that in the database interface modules, as far as possible I would like to infer an SQL type from a D type. There are often SQL types for TIME, DATE, DATETIME, and TIMESTAMP, but the latter two will usually correspond to D structs with the same members. I can define two identical structs with the same set of protocol parsing and packing methods, but with different names, but to me this seems clunky, error-prone, and not a very good advert for the language. There's was also a temptation to create typedefs of char[] like tinyString, mediumString and longString. I have not gone there - just as well. Forgetting typedef, which I should assume does not exist, what is the elegant approach to this? Steve
Oct 31 2011
prev sibling parent "Robert Jacques" <sandford jhu.edu> writes:
On Mon, 31 Oct 2011 06:30:40 -0400, Steve Teale <steve.teale britseyeview.com>
wrote:
 Robert,

 Maybe you can fix this along the way:

 import std.variant;
 struct B
 {
    int p, q, r, s;
 }
 typedef B C;

 void main()
 {
    B b;
    C c;
    b = c;   // ok
    Variant v = c;
    assert(v.convertsTo!(B));   // no dice
 }

 Steve
As others have mentioned, typedef is being deprecated. That said, variant now supports duck-typing. So 'v.to!B()' works. However, 'convertsTo's mandate is to test for implicit conversion, which isn't covered as there's no way in __traits to recover a typedef's base type.
Oct 31 2011