Re: Overloading free functions & run-time dispatch based on parameter types

2016-02-07 Thread Robert M. Münch via Digitalmars-d-learn
On 2016-02-06 14:33:57 +, Marc Schütz said: I don't see why this wouldn't work, if you've in fact covered all combinations. It works, the problem was that castSwitch returns something and I didn't "catch" it. It's similar to how castSwitch is implemented, though the double casts are in

Re: Overloading free functions & run-time dispatch based on parameter types

2016-02-06 Thread Marc Schütz via Digitalmars-d-learn
On Friday, 5 February 2016 at 19:48:45 UTC, Robert M. Münch wrote: I thought about it too, but I need it to work with more then one parameter, so I tried this which doesn't work: Value nativePlus(Value a, Value b){ // @@ not working, runtime exception castSwitch!( (IntV a) { castS

Re: Overloading free functions & run-time dispatch based on parameter types

2016-02-05 Thread Robert M. Münch via Digitalmars-d-learn
On 2016-02-05 15:23:53 +, Marc Schütz said: Does the following help? ... I thought about it too, but I need it to work with more then one parameter, so I tried this which doesn't work: Value nativePlus(Value a, Value b){ // @@ not working, runtime exception castSwitch!( (IntV a)

Re: Overloading free functions & run-time dispatch based on parameter types

2016-02-05 Thread Marc Schütz via Digitalmars-d-learn
Does the following help? import std.algorithm.comparison : castSwitch; import std.stdio; class A { } class B : A { } class C : A { } auto foo_impl(B b) { writeln("called foo(B)"); } auto foo_impl(C c) { writeln("called foo(C)"); } auto foo(A a) { return a.castSwitch!( (B b)

Re: Overloading free functions & run-time dispatch based on parameter types

2016-02-05 Thread Robert M. Münch via Digitalmars-d-learn
On 2016-02-05 11:10:36 +, Nicholas Wilson said: sounds like foo should just be a method in the class rather than a free function In my particular case I want to keep some stuff outside of claases. -- Robert M. Münch http://www.saphirion.com smarter | better | faster

Re: Overloading free functions & run-time dispatch based on parameter types

2016-02-05 Thread Nicholas Wilson via Digitalmars-d-learn
On Friday, 5 February 2016 at 10:54:27 UTC, Robert M. Münch wrote: From the docs: class A { } class B : A { } class C : B { } void foo(A); void foo(B); [...] sounds like foo should just be a method in the class rather than a free function

Overloading free functions & run-time dispatch based on parameter types

2016-02-05 Thread Robert M. Münch via Digitalmars-d-learn
From the docs: class A { } class B : A { } class C : B { } void foo(A); void foo(B); void test() { C c; foo(c); // calls foo(B) } I need the other way around. So, at runtime I get an A and depending on it's dynamic type, I would like to get the correct foo() called. class A { } class