Dynamic alter-ego of D.

Gor Gyolchanyan gor.f.gyolchanyan at gmail.com
Tue Oct 25 04:00:44 PDT 2011


That's a marvelous example!
I think Variant should become a built-in type.
Since variants are basically value with no type, it could be a good
idea to allow declaring objects of type void, which also have no type.
just like void[] is an array of no type and void* is a pointer of no
type. The size of a void would be (2*size_t.sizeof) bytes long, which
can hold any data (structs, bigger then (2.size_t.sizeof) are passed
by pointer) just like it's passed to functions. taking address of such
a variable results in a typeless pointer (void*) and slicing that
pointer results in a typeless array (void[]).

The only controversy is the size of that array:

static assert(void.sizeof == 2 * size_t.sizeof);
version(traditional)
    static assert(void[4].sizeof == 4);
version(dynamic)
    static assert(void[4].sizeof == 8);

The version block is for show only. I think changing the second one
would be better (although it would probably be a breaking change),
because if you want an array of bytes, use an array of bytes: byte[].
Any type would be implicitly convertible to void, but not back.
You could pass typeless parameters directly into functions with 0
overhead and without knowing the exact signature of the function.
This low-level dynamic typing can unleash TREMENDOUS possibilities to
make incredibly fast and powerful tools.

Due to potential problems with using void keyword, other stuff like
__void can be used until such a breaking change can be implemented.
The void _word_ is used because it correctly names the type of the
object (which is not present) and it clearly highlights the relation
between objects of those types and pointer (void*) which are already
in use.

See, dynamic typing should not start from a heavy type-checked
variant, it should begin with a minimal, very fast and compact
concept, upon which other solutions can be built.

Naturally, you can't use objects of type void in any way except cast
them. Casts can be type-checked in debug versions and unchecked in
release versions, making a perfect solution for both safety and speed.

What do you think?

On Tue, Oct 25, 2011 at 2:31 PM, Piotr Szturmaj <bncrbme at jadamspam.pl> wrote:
> Gor Gyolchanyan wrote:
>>
>> I think adding more dynamic typing to D would be a splendid idea to
>> further widen the variety of solutions for different problems.
>> Modular app development is a very good practice and modularity means
>> dynamicity, which in turn means, that one needs to give up on lots of
>> sweet stuff like templates, overloading and string mixins.
>> Variant is the first step towards dynamic alter-ego of D, which is
>> completely undeveloped currently.
>
> If Algebraic/Variant could be recognized by the language it would give some
> interesting possibilities, like Variant array literals:
>
> auto array = [10, 2.0, "abc"];
>
> types it as Algebraic!(int, double, string)[];
>
> auto AA = [ "a" : 10, "b" : 2.0, 3 : "abc" ];
>
> types it as Algebraic!(int, double, string)[Algebraic!(string, int)];
>
> for example, that may be used to express JSON structures with literals only:
>
> int x, y;
>
> auto json = [
>    "x" : x,
>    "y" : y,
>    "colors" : [
>        [
>            r : 255,
>            g : 0,
>            b : 0
>        ],
>        [
>            r : 0,
>            g : 0,
>            b : 255
>        ]
>    ]
> ]
>


More information about the Digitalmars-d mailing list