matovitch:

struct Test
{
        immutable (ubyte)[] data;

        T get(T)()
        {
                return *(cast(T*)(&(data[0])));
        }

It's better to return const/immutable data. Otherwise the program gives undefined results. In alternative use mutable ubytes for data.

Also &data[0] is probably data.ptr.


This won't compile :

import std.stdio;

void main()
{
        Test t;
        t.data = [152, 32, 64, 28, 95];
        float b = t.get;
        writefln("%s", b);

D doesn't perform inference on the return type.


This neither:
...
        Test t;
        t.data = [152, 32, 64, 28, 95];
        float b = t.get!(typeof(b));
        writefln("%s", b);

I don't know. Perhaps b is not yet defined. This in theory could work.


In fact this would allow a nicer syntax for the Json struct in vibe.d for example.

I don't think in future D will behave differently on this code.

Bye,
bearophile

Reply via email to