Template method and type resolution of return type

matovitch via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Apr 19 10:24:58 PDT 2014


Hi everyone !

Let's say I have a struct :

struct Test
{
	immutable (ubyte)[] data;

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

This code will work :

import std.stdio;

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

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);
}

This neither:

import std.stdio;

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

And this will work:

import std.stdio;

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

Why can't dmd infere the type 'float' ? In fact this would allow 
a nicer syntax for the Json struct in vibe.d for example.


More information about the Digitalmars-d-learn mailing list