The daily D riddle

Jonathan M Davis newsgroup.d at jmdavisprog.com
Sun Jan 28 08:05:39 UTC 2018


On Saturday, January 27, 2018 23:40:16 H. S. Teoh via Digitalmars-d wrote:
> On Sun, Jan 28, 2018 at 12:04:42AM -0700, Jonathan M Davis via 
Digitalmars-d wrote:
> > On Saturday, January 27, 2018 23:44:40 Jonathan M Davis via
> > Digitalmars-d
>
> > wrote:
> [...]
>
> > > It does exactly what I'd expect it to do, though honestly, it's the
> > > sort of thing I wish weren't legal, just like I wish that it weren't
> > > legal to call a static member function via a member. Maybe there are
> > > cases where it's useful, but it just seems wrong.
> >
> > via in instance, I mean. IMHO, it should be required to do
> > Type.staticMember rather than var.staticMember. The fact that it's
> > allowed is just messy and is one of the things that we inherited from
> > C++ that we shouldn't have. This case falls in the same camp, except
> > that it's a new mistake, since C++ doesn't have init values.
>
> [...]
>
> Are you sure this came from C++? I'm pretty sure instance.staticMember
> (or instance->staticMember) is not allowed in C++, you have to write
> Class::staticMember. I distinctly remember, having gotten used to the
> distinction in C++, being a little surprised that D was lax in this
> area. In fact, I remember running into problems with my early D code
> where I relied on this distinction, only to quickly find myself drowning
> in overload conflicts / ambiguity errors when I tried invoking the
> methods.

I'm quite sure, but that doesn't mean that I'm right. Checking... Yep. It
works in C++. This code compiles just fine on my FreeBSD system with both
g++ and clang++:

class A
{
public:
    static bool foo() { return false; }
};

int main()
{
    A a;
    bool result = a.foo();
    return 0;
}

As to _why_ it works, I don't know - it seems like a bad idea to me - but it
does.

- Jonathan M Davis



More information about the Digitalmars-d mailing list