Is there a way to remove the requirement for parenthesis?

Denis Koroskin 2korden at gmail.com
Wed Jan 21 15:20:23 PST 2009


On Wed, 21 Jan 2009 20:26:08 +0300, Charles Hixson <charleshixsn at earthlink.net> wrote:

> P.S.:  This is Digital Mars D Compiler v2.023 running on Linux
>
> 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?

No, there isn't. It leads to ambiguity and here is why:

class Foo
{
    Foo opCall() { return new Foo(); }
}

void main() {
    Foo bar = new Foo();
    auto ambiguous = bar; // is it 'bar' or 'bar()'?
}

One more case where empty pair of parens is mandatory.

To Walter & Co: Please, oh *PLEASE* drop this feature and give us real properties!



More information about the Digitalmars-d-learn mailing list