Is there a way to remove the requirement for parenthesis?

Charles Hixson charleshixsn at earthlink.net
Tue Jan 27 20:16:21 PST 2009


Sergey Gromov wrote:
> Wed, 21 Jan 2009 09:24:01 -0800, Charles Hixson wrote:
> 
>> In this test I'm trying to emulate how I want a typedef to act, but I 
>> run into a problem:
>>
>> import   std.stdio;
>>
>> struct   BlockNum
>> {  uint   value;
>>
>>     uint   opCast()   {   return   value;   }
>>     void   opAssign (uint val)   {   value = val;   }
>>     uint   opCall()   {   return   value;   }
>> }
>>
>> void   main()
>> {  BlockNum   test;
>>     test   =   42;
>>     uint   tst2   =   test();  // <<== if I don't have the parenthesis I
>>                                //    get a compiler error (cast
>>                                //    required).
>>            //  kfile.d(15): Error: cannot implicitly convert expression
>>            //          (test) of type BlockNum to uint
>>
>>     writef ("tst2 = %d\n", tst2);
>> }
>>
>> It seemed to me as if the parens shouldn't be required here, but I seem 
>> mistaken.  Which leads to ugly code.  Is there a way around this?
> 
> test is an expression of type BlockNum.  opCall() is called when you use
> parentheses syntax on it.  opCast() is called when you use cast() syntax
> for it.  Otherwise it stays BlockNum and therefore is not convertible to
> uint.
I think that means "No, there isn't a way around it."
OK.  I'll just ...
well, I'm not totally sure.  Either give up type safety or ... something 
else.


More information about the Digitalmars-d-learn mailing list