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.