Using the Variant (Setting it's memory location)

Pedro Lacerda pslacerda at gmail.com
Tue Feb 7 08:07:57 PST 2012


You can roll your own tagged union instead. The S struct can store long and
byte[], S.ptr is a pointer to the data.

    enum Type { Long, Bytes }
    struct S {
        Type type;
        void* ptr;
        union {
            long _long;
            byte[] _bytes;
        }
        this(long l) {
            _long = l;
            type = Type.Long;
            ptr = &_long;
        }
        this(byte[] bytes) {
            _bytes = bytes;
            type = Type.Bytes;
            ptr = &_long;
        }
    }

    auto s = S(99);
    assert(s.ptr == &(s._long));
    assert(s.ptr == &(s._bytes));

Pedro Lacerda



2012/2/7 Jesse Phillips <jessekphillips+D at gmail.com>

> On Tuesday, 7 February 2012 at 00:39:00 UTC, Era Scarecrow wrote:
>
>> Unfortunately I'd need to reference a buffer for the known structured
>> types. Variant seems far more useful for making an interpreted language,
>> than for my purposes.
>>
>
> I've been using Variant with LuaD for some time. Sorry it isn't what you
> need but hopefully you'll know when it will be useful.
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d-learn/attachments/20120207/d6b6cadb/attachment.html>


More information about the Digitalmars-d-learn mailing list