The daily D riddle

Jonathan M Davis newsgroup.d at jmdavisprog.com
Sun Jan 28 07:04:42 UTC 2018


On Saturday, January 27, 2018 23:44:40 Jonathan M Davis via Digitalmars-d 
wrote:
> On Sunday, January 28, 2018 08:25:51 Shachar Shemesh via Digitalmars-d
>
> wrote:
> > What will the following code print? Do not use the compiler:
> >
> > import std.stdio;
> >
> > struct A {
> >
> >   int a = 1;
> >
> >   void initialize() {
> >
> >       a = a.init;
> >
> >   }
> >
> > }
> >
> > void main() {
> >
> >   A a;
> >   a.initialize();
> >
> >   writeln(a.a);
> >
> > }
> >
> > I find this behavior unexpected.
>
> 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.

- Jonathan M Davis



More information about the Digitalmars-d mailing list