www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Assigning a value to a Struct when its declared question.

Can someone please point me into the right direction with
assigning a value to a struct on the same line where it is being

In the sample code below I've gotten the new opAssign() operator
for structs to work just fine (the testAssign() function), and I
can use a function to init a declared struct with a value (the
testInitAssign1() function), but I can't seem to get the
testInitAssign2() function to work. I must be doing something
wrong, or using the wrong operator(s).

// varoper.d
// WinXP SP2, dmd v0.177
private import std.stdio;

struct var
    union U
        char[] s;
        real   r;

    private U u;

    //var opCall(char[] s)      { this.u.s = s; return *this;}
    var opAssign(in char[] s) { this.u.s = s; return *this; }
    var opAssign(in real   r) { this.u.r = r; return *this; }

    public char[] getCharA() { return this.u.s; }
    public real   getReal()  { return this.u.r; }

public var toVariant(in char[] s)
{ var v; v.opAssign(s); return v; }
public var toVariant(in real   r)
{ var v; v.opAssign(r); return v; }

int main()
    return 0;

// Works
void testAssign()
    var v1;

    v1 = "char[] value"c;
    writefln("v1=\"%s\"", v1.getCharA);

    v1 = 123.66L;
    writefln("v1=\"%s\"", v1.getReal);

// Works
void testInitAssign1()
    char[] s = "char[] value"c;
    var v2 = toVariant(s);
    var v3 = toVariant("char[] value"c);

    writefln("v2=\"%s\"", v2.getCharA);
    writefln("v3=\"%s\"", v3.getCharA);

// What to do to make this work?
void testInitAssign2()
    // ** How do I get opCall() for work here **
    //var v4 = "char[] value"c;
    //writefln("v4=\"%s\"", v4.getCharA);

Output with only the first two functions:
C:\dmd>dmd varoper.d
C:\dmd\bin\..\..\dm\bin\link.exe varoper,,,user32+kernel32/noi;

v1="char[] value"
v2="char[] value"
v3="char[] value"


Thanks in advance for any help,
David L.
Dec 15 2006