On Sunday, 6 April 2014 at 01:33:36 UTC, Nick Sabalausky wrote:
On 4/5/2014 9:26 PM, Nick Sabalausky wrote:
It would be *fantastic* if D recognized the disambiguation of
using
incompatible types and permitted this:
interface Foo {}
interface Bar {}
class FooBar : Foo, Bar {}
void func(Foo foo=someFoo, Bar bar=someBar) {...}
func(myFoo); // Unambiguous, OK
func(myBar); // Unambiguous, OK
func(myFooBar); // Ambiguous, ERROR
Actually, that last line should be:
func(myFooBar); // Unambiguous, this is still interpreted
as the first parameter (with the second parameter left as
default) just as it is right now.
I think D is actually one of the better languages around in terms
of factoring this sort of thing out. Here's an example argument
parser that lets you define pairs of optional arguments:
http://pastebin.com/RaNfwH6X. It can be extended to allow
n-tuples of optional arguments.
Argument parsing is encapsulated so writers only have to declare
one function template, with a constraint that clearly expresses
intent:
void func(Args...)(Args args)
if (Pattern!(bool, AnyOf!(int, string)).matches!Args)
{
alias Parser = Pattern!(bool, AnyOf!(int, string));
auto parsedArgs = Parser.parse(tuple(args), tuple(false, 0,
"0"));
writeln(parsedArgs);
}
void main()
{
func(true); // true, 0, "0"
func(true, 1); // true, 1, "0"
func(true, "1"); // true, 0, "1"
func(true, 1, "1"); // true, 1, "1"
}