Hi,<br><br>I have two syntactic "difficulties" when initializing structs:<br><br>1)<br>Say I have a struct StringHash that represents the hash code of a string.<br><br>struct StringHash<br>{<br> this( string str )<br>
{<br> computeHash(str);<br> }<br><br> void computeHash( string str )<br> {<br> hash = 0;<br> foreach(c;str)<br> {<br> hash ^= c;<br> hash *= 0x93;<br> }<br>
}<br> <br> bool opEquals( ref const(StringHash) s )<br> {<br> return hash == s.hash;<br> }<br> <br> bool opEquals( string s )<br> {<br> return hash == StringHash(s).hash;<br> }<br>
<br> uint hash;<br>}<br><br>I would like this structure to be as transparent as possible and be able to write things like:<br><br>StringHash sh = "SomeString"; // ok<br><br>struct Foo<br>{<br> StringHash name;<br>
}<br><br>Foo foo = {<br> name : "SomeString" // Error: cannot implicitly convert expression ("SomeString") of type string to StringHash<br> category : HString( "SomeString" ); // works, but looks less nice IMO.<br>
};<br><br><br>2)<br>It looks like I can't initialize fields of a struct with anonymous functions or delegates (the error is not clear to me though), for example:<br><br>struct Element<br>{<br> void delegate(void) onSomething;<br>
void delegate(void) onSomethingElse;<br>}<br><br>void main()<br>{<br> auto callBack = delegate void(void) { stdout.writeln("callBack"); };<br><br> Element e = {<br> onSomething : callBack, // ok<br>
onSomethingElse : delegate void(void) { stdout.writeln("anonymous"); } // errors (this is line 15, see below)<br> };<br>}<br><br>test.d(15): found ':' when expecting ';' following statement<br>
test.d(16): found '}' when expecting ';' following statement<br>test.d(19): semicolon expected, not 'EOF'<br>test.d(19): found 'EOF' when expecting '}' following compound statement<br>
<br><br>Both of these little problems are not crucial, but they could bring some very nice syntactic sugar on the API I am designing right now.<br>how should I fix it?<br>If the actual behaviors are desired i'd be interested to know the arguments (well, i can imagine motivation for explicit conversion, but for the delegate thing it's less clear to me). <br>
<br>Thanks,<br><br>Nicolas<br>