Return by 'ref' problems...

Steven Schveighoffer schveiguy at yahoo.com
Fri May 4 07:35:51 PDT 2012


On Fri, 04 May 2012 10:19:08 -0400, H. S. Teoh <hsteoh at quickfur.ath.cx>  
wrote:

> Argh... this is really annoying. So I tried all sorts of combinations of
> function pointer syntax in order to get the correct type for a ref
> function that returns const(T), but couldn't. So I decided to let the
> language tell me itself what the type is:
>
> 	import std.stdio;
> 	struct S {}
> 	ref const(S) func() { ... }
> 	void main() {
> 		auto fp = &func;
> 		writeln(typeid(fp));
> 	}
>
> This program outputs:
>
> 	const(test.S)()*

This is a bug in druntime.  It should *never* print shit like this, we got  
rid of C-style function pointers.

try this instead when printing a type instead of typeid:

writeln(typeof(fp).stringof);

This can even be used at compile time:

pragma(msg, typeof(fp).stringof);

Result is:

const(S) function() ref

So let's try it!

void main()
{
    const(S) function() ref fp = &func;
}

typeidbug.d(6): no identifier for declarator const(S) function()
typeidbug.d(6): semicolon expected, not 'ref'

bleh, tried this too:

alias const(S) function() ref T;
...
T fp = &func;

typeidbug.d(4): found 'ref' when expecting '('
typeidbug.d(4): found ';' when expecting ')'
typeidbug.d(5): no identifier for declarator const(S) function(T)
typeidbug.d(5): semicolon expected to close alias declaration

this does work though:

alias typeof(&func) T;

So this type cannot be manually written, due to parsing ambiguities for  
ref.

So we got two bugs here:

1. you cannot manually write the type of such a function.
2. typeid(&func).toString() returns crap.

-Steve


More information about the Digitalmars-d mailing list