Thread overview | |||||||||
---|---|---|---|---|---|---|---|---|---|
|
February 14, 2013 Mixin template function | ||||
---|---|---|---|---|
| ||||
Should the following work? import std.traits; mixin template Foo() { void foo(T)(T t) if (isSomeString!T) {} } class A { void foo()(int i){} mixin Foo; } void main() { auto a = new A; a.foo("hello"); } Error: template hello.A.foo does not match any function template declaration. Candidates are: hello.A.foo()(int i) If i give the mixin an identifier (mixin Foo _foo) and call it like a._foo.foo("hello") then it works. I thought it should work without that though. |
February 14, 2013 Re: Mixin template function | ||||
---|---|---|---|---|
| ||||
Posted in reply to cal | And a related question: class A { void foo(int i){} void foo(Tuple!(int) i){} } class B: A { override void foo(int i){} } int main() { auto b = new B; b.foo(tuple(5)); } This fails to compile. Why can't B use A's tuple overload of foo()? If I do this: class B: A { override void foo(int i){} void foo(Tuple!(int) i){} // no override keyword is deprecated } The compiler warns about not using the override keyword, so it must be seeing the function? |
February 14, 2013 Re: Mixin template function | ||||
---|---|---|---|---|
| ||||
Posted in reply to cal | On Thursday, 14 February 2013 at 00:29:51 UTC, cal wrote:
> Should the following work?
>
> import std.traits;
>
> mixin template Foo()
> {
> void foo(T)(T t) if (isSomeString!T) {}
> }
>
> class A
> {
> void foo()(int i){}
> mixin Foo;
> }
>
> void main()
> {
> auto a = new A;
> a.foo("hello");
> }
>
> Error: template hello.A.foo does not match any function template declaration. Candidates are:
> hello.A.foo()(int i)
>
> If i give the mixin an identifier (mixin Foo _foo) and call it like a._foo.foo("hello") then it works. I thought it should work without that though.
AFAIK, this is a bug. It has *probably* already been reported, but you'd have to check for it.
|
February 14, 2013 Re: Mixin template function | ||||
---|---|---|---|---|
| ||||
Posted in reply to cal | On Thursday, 14 February 2013 at 05:49:33 UTC, cal wrote: > And a related question: > > class A > { > void foo(int i){} > void foo(Tuple!(int) i){} > } > > class B: A > { > override void foo(int i){} > } > > > int main() > { > > auto b = new B; > b.foo(tuple(5)); > } > > This fails to compile. Why can't B use A's tuple overload of foo()? If I do this: > > class B: A > { > override void foo(int i){} > void foo(Tuple!(int) i){} // no override keyword is deprecated > } > > The compiler warns about not using the override keyword, so it must be seeing the function? This looks like it comes from C++, and is a built-in protection. If you override a single method, it will shadow all other overloads. This makes sure you don't accidentally call something you didn't want over-ridden. If you know what you are doing, then you can make it explicit. C++ uses the "using" keyword. I don't know how D does it. http://stackoverflow.com/questions/888235/overriding-a-bases-overloaded-function-in-c |
February 14, 2013 Re: Mixin template function | ||||
---|---|---|---|---|
| ||||
Posted in reply to cal | On 2013-02-14 06:49, cal wrote: > And a related question: > > class A > { > void foo(int i){} > void foo(Tuple!(int) i){} > } > > class B: A > { > override void foo(int i){} > } > > > int main() > { > > auto b = new B; > b.foo(tuple(5)); > } > > This fails to compile. Why can't B use A's tuple overload of foo()? If I > do this: > > class B: A > { > override void foo(int i){} > void foo(Tuple!(int) i){} // no override keyword is deprecated > } > > The compiler warns about not using the override keyword, so it must be > seeing the function? The base class and the subclass have different overload sets. You need to bring in "foo" from the base class into the overload set in the subclass. You can do this by using an alias: class B : A { alias A.foo foo; } -- /Jacob Carlborg |
February 14, 2013 Re: Mixin template function | ||||
---|---|---|---|---|
| ||||
Posted in reply to cal | On 2013-02-14 01:29, cal wrote: > Should the following work? > > import std.traits; > > mixin template Foo() > { > void foo(T)(T t) if (isSomeString!T) {} > } > > class A > { > void foo()(int i){} > mixin Foo; > } > > void main() > { > auto a = new A; > a.foo("hello"); > } > > Error: template hello.A.foo does not match any function template > declaration. Candidates are: > hello.A.foo()(int i) > > If i give the mixin an identifier (mixin Foo _foo) and call it like > a._foo.foo("hello") then it works. I thought it should work without that > though. This is by design. Foo and A have different overload sets. Try: alias Foo.foo foo; http://dlang.org/template-mixin.html Search for: "Mixin Scope" and pay attention to: "Alias declarations can be used to overload together functions declared in different mixins". -- /Jacob Carlborg |
February 14, 2013 Re: Mixin template function | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jacob Carlborg | On Thursday, 14 February 2013 at 07:40:58 UTC, Jacob Carlborg wrote:
> This is by design. Foo and A have different overload sets. Try:
>
> alias Foo.foo foo;
>
> http://dlang.org/template-mixin.html
>
> Search for: "Mixin Scope" and pay attention to:
>
> "Alias declarations can be used to overload together functions declared in different mixins".
Ahh this is what I was missing, many thanks.
|
Copyright © 1999-2021 by the D Language Foundation