Static function template
Daniel Kozak via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Thu May 7 04:15:00 PDT 2015
On Thursday, 7 May 2015 at 11:08:50 UTC, Daniel Kozák wrote:
>
> On Thu, 07 May 2015 10:46:19 +0000
> Lemonfiend via Digitalmars-d-learn
> <digitalmars-d-learn at puremagic.com>
> wrote:
>
>> On Thursday, 7 May 2015 at 10:43:28 UTC, Daniel Kozak wrote:
>> > On Thursday, 7 May 2015 at 10:39:09 UTC, Daniel Kozák wrote:
>> >>
>> >> On Thu, 07 May 2015 10:33:44 +0000
>> >> Vadim Lopatin via Digitalmars-d-learn
>> >> <digitalmars-d-learn at puremagic.com> wrote:
>> >>
>> >>> struct S
>> >>> {
>> >>> int i;
>> >>>
>> >>> auto foo2(T)(int j) {
>> >>> i=j;
>> >>> }
>> >>>
>> >>> static S foo(T)(int j) {
>> >>> S s;
>> >>> s.foo2!T(j);
>> >>> return s;
>> >>> }
>> >>> }
>> >>>
>> >>> void main()
>> >>> {
>> >>> auto s = S.foo!bool(1);
>> >>> }
>> >>
>> >> As I said, it is not bug. It is OK. There is no way how you
>> >> can
>> >> distinguish between static and non static methods or even
>> >> field in some
>> >> cases.
>> >
>> > e.g.:
>> >
>> > import std.stdio;
>> >
>> > struct S
>> > {
>> > string foo = "Please select me?";
>> > string foo() { return ("No, select me?"); };
>> > static string foo() { return ("I am better than the otters
>> > :D?"); };
>> > }
>> >
>> > void main()
>> > {
>> > auto s = S();
>> > writeln(s.foo);
>> > }
>>
>> Well it's clear to me now why it shouldn't work.
>>
>> However, the error msg is not clear on the problem. Imo it
>> should give a conflict error like in your previous example.
>> That would make it clear what's happened/allowed.
>>
>
> Yep, I think you are right even this example make useless and
> wrong error message:
>
> struct S
> {
> static S foo(T)(int j) {
> S s;
> return s;
> }
> static S foo(T)(int j) {
> S s;
> return s;
> }
> }
>
> void main()
> {
> auto s = S.foo!bool(1);
> }
>
>
> test.d(15): Error: need 'this' for 'foo' of type '(int j)' //
> WTF?
btw. it is a regresion from 2.067 on 2.066 and before it makes
much
better error. Even for OP code when is modified (static must be
declared before non static one) it have a better error msg.
test.d(6): Error: test.S.foo called with argument types (int)
matches both:
test.d(4): test.S.foo!bool.foo(int j)
and:
test.d(10): test.S.foo!bool.foo(int j)
test.d(18): Error: template instance test.S.foo!bool error
instantiating
But only when static one is declared before non static one, so
even on 2.066 it was not ideal. So I think we should open two
issues, probably one for regression and one for enhancment
More information about the Digitalmars-d-learn
mailing list