Target typed new expressions for D?

user1234 user1234 at 12.de
Fri Apr 30 19:44:24 UTC 2021


On Friday, 30 April 2021 at 16:20:13 UTC, sighoya wrote:
> Relates to the following:
>
> https://devblogs.microsoft.com/dotnet/welcome-to-c-9-0/#target-typed-new-expressions
>
> What do you think about?
>
> I like it more than `auto` inference. You have an explicit 
> invariant, and the constructor takes the invariant into account 
> for value construction.
>

This is not really comparable to `auto`. `auto` works because the 
expression type is known. targeted typing is actually the exact 
opposite.

> I think, however, that using `new` in D isn't possible anymore, 
> maybe some different keyword.
>
> Is this at all possible with templates now or did we suffer 
> from missing return type deduction?

It's totally possible to mimic the feature without templates.

In D a possible implementation would be to add a `Type 
targetedTypingHint` member to `Scope`. The hint would be used 
when encountering an `AutoExpr`.

The AutoExp would be

```ebnf
AutoExp ::= 'auto' '(' Arguments ')'
```

to keep consistent with the uniform construction syntax. So to 
rewrite the C# example:

```d
   Point p = auto(3, 5);
//^hint     ^AutoExp

   Class c = new     auto();
//^hint     ^NewExp ^AutoExp
```

But TBH I dont see much use cases.


More information about the Digitalmars-d mailing list