Can call static method with null reference
monarch_dodra
monarchdodra at gmail.com
Sun Jun 23 03:48:15 PDT 2013
On Sunday, 23 June 2013 at 10:09:39 UTC, Jonathan M Davis wrote:
> On Sunday, June 23, 2013 12:02:42 Namespace wrote:
>> > I don't see what's so terrible about it
>>
>> It's bug prone.
>>
>> class Foo {
>> public:
>> static void test1() { }
>> void test2() { }
>> }
>>
>> Foo f;
>> f.test1(); /// Oh nice, that works, f is not null.
>> f.test2(); /// WTF? f is null?
>
> I fail to see what's bug-prone about that. It's confusing, but
> it's not
> causing any bugs.
>
>> Also I don't know why I should call static methods from an
>> instance. What's the purpose?
>
> It's stupid and pointless as far as I can tell, but I believe
> that C++, Java,
> C#, and D all do it, so as stupid as it is, it's a common
> stupidity. I
> certainly wish that we could change it, but I wouldn't expect
> Walter to agree
> to the change, since it would break at least some existing
> code, and I suspect
> that he doesn't consider the fact that you can call static
> functions on
> instances to be a problem. That's not the sort of thing that he
> generally
> seems to think is an issue. It's almost always stuff that
> causes actual bugs
> that he agrees to change and not things that are aesthetically
> displeasing or
> which could theoretically cause bugs.
>
> - Jonathan M Davis
C++ doesn't allow it. I don't know about the rest.
If anything, I find overloading static non static could make
sense:
A.print(); //"I'm an A!"
a.print(); //"I'm an A called foo!"
With this in mind, it can mean that a struct can first define the
static function, and in the future, add extra logic to handle
information from a specific instance, yet without having to
caller code.
More information about the Digitalmars-d-learn
mailing list