Would this be a useful construct to add to D? auto for constructor call.

Jonathan M Davis newsgroup.d at jmdavisprog.com
Tue Jan 23 06:30:08 UTC 2024


On Monday, January 22, 2024 11:05:28 PM MST Chris Katko via Digitalmars-d-
announce wrote:
> ```D
> class dataGridLayerView{
>     int t;
>     this(int _t){
>        t = _t;
>        }
>     }
>
> class myClass{
>     dataGridLayerView dataGrid;
>
>     this()
>        {
>        dataGrid = new auto(15); // <--- new
>        // instead of
>        dataGrid = new dataGridLayerView(15);
>        }
>     }
> ```
>
> Because it seems, conceptually, the compiler should know all the
> details required here to simply insert the right constructor
> name. Basically just the reverse of:
>
> ```D
> auto t = sqrt(15);
> ```
>
> So intuitively it makes sense to me.
>
> It might even be possible to write a mixin to do this, I'm not
> sure.
>
> I'm no D wizard, but I don't see any obvious name or lexing
> conflicts/ambiguity.
>
> Cheers,
> --Chris

This is the wrong forum for questions. This is for announcements.

https://forum.dlang.org/group/general is for general discussions on D.

https://forum.dlang.org/group/learn is for asking questions about using D.

In any case, as far as your question goes, it is unlikely that a feature
like that would be implemented, because expressions in D do not typically
get their type from what they're assigned to. The type of the expression is
determined separately from where it is used, and then it's checked to see
whether it works where it's being used. There are a few exceptions (mostly
having to do with initializing variables from array literals), so I don't
know that the chances of adding a feature like this are zero, but I don't
think that they're high.

That being said, I expect that it would be pretty easy to write a mixin to
do something like that if you really wanted to. Also, if you're simply
looking to not have to name the type, you could do

dataGrid = new typeof(datagrid)(15);

- Jonathan M Davis





More information about the Digitalmars-d-announce mailing list