On Sun, 29 Nov 2009 11:44:56 +0100, biozic <[email protected]> wrote:
Le 29/11/09 00:36, Walter Bright a écrit :
And here it is (called opDispatch, Michel Fortin's suggestion):
http://www.dsource.org/projects/dmd/changeset?new=trunk%2f...@268&old=trunk%2f...@267
Seems interesting, but for now the error message when no opDispatch
template can be instantiated looks confusing when trying to use a class
with an opDispatch implemented, and making e.g. a typo error:
=============================================
module lib;
class Test
{
string opDispatch(string name)()
{
static if (name == "foo")
return "foo";
}
}
=============================================
module main;
import lib;
import std.stdio;
void main()
{
auto test = new Test;
writeln(test.foo); // OK
writeln(test.fooo); // Error
}
=============================================
Error is: """
lib.d(5): Error: function lib.Test.opDispatch!("fooo").opDispatch
expected to return a value of type string
lib.d(9): Error: template instance lib.Test.opDispatch!("fooo") error
instantiating
"""
nicolas
That is because your opDispatch is instantiated no matter what the name
is, but only does something sensible if it's foo. Try this:
string opDispatch( string name )( ) {
static if ( name == "foo" ) {
return "foo";
} else {
static assert( false, "Invalid member name." );
}
}
--
Simen