typeof and my crummy code...

Ellery Newcomer ellery-newcomer at utulsa.edu
Fri Nov 6 10:36:44 PST 2009


Bill Baxter wrote:
> On Fri, Nov 6, 2009 at 9:02 AM, Ellery Newcomer
> <ellery-newcomer at utulsa.edu> wrote:
>> Another question: given
>>
>> import std.stdio;
>> void main()
>> {
>>    int i( /*char*/ ){return 1;}
>>    writeln(typeof(i).stringof);
>> }
>>
>>
>> This gives
>>
>> (int())()
> 
> That seems buggy to me.  I would expect it to say "int".
> 
>> and with the char uncommented it errors
> 
> That seems right.  i by itself is an attempt to call i with no
> arguments.  You need to use & with functions if you want to avoid
> that.
> 
>> * typeof doesn't evaluate the expression, according to the spec.
> 
> But it is supposed to figure out what the type would be if it /were/ evaluated.
> 

Yeah, I suppose.

>> * for a function sans params, there would be a semantic ambiguity (in D1
>> land, at least) in typeof(i) (params applied, or no?)
> 
> You need to use & if you're talking about the function itself and not
> what it evaluates to.
> 

>From my perspective in semantic analysis, &i doesn't refer to the
function itself, it refers to a pointer to the function. I reckon that's
kind of a weird nitpick..

>> * and for the case above, why the heck are we mixing expression and type
>> in the string result?
> 
> I think that's a bug.  Does using &i give you the function type as expected?
> 

I didn't exactly expect a delegate, but yeah, that would be right.

okie dokie, semantic rule: functions must always either be applied or
dereferenced in expressions.

except in template alias parameters. which is a special case anyways.

and regular aliases.

and who knows what else.


> --bb


More information about the Digitalmars-d-learn mailing list