Thread overview | ||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
September 12, 2020 Adding the ?. operator to D | ||||
---|---|---|---|---|
| ||||
Has there been any discussions on adding the ?. operator to simplify structure matching on class trees typically found in compilers such as dmd. Basically, implemented via lowering of C ?. EXPR to C ? C.EXPR : EXPR.init when EXPR has type class or pointer and for the other cases either C ? C.EXPR : EXPR.init or perhaps even C ? C.EXPR : nullable(EXPR).init if nullable is made part of the runtime or even builtin. Given that `C` is a class or pointer to an aggregate. C# supports has this [1]. [1] https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/member-access-operators#null-conditional-operators--and- |
September 12, 2020 Re: Adding the ?. operator to D | ||||
---|---|---|---|---|
| ||||
Posted in reply to Per Nordlöw | On Saturday, 12 September 2020 at 16:51:20 UTC, Per Nordlöw wrote: > Has there been any discussions on adding the ?. operator to simplify structure matching on class trees typically found in compilers such as dmd. The package 'optional' on dub has a library version of this: https://aliak00.github.io/optional/optional/oc/oc.html |
September 12, 2020 Re: Adding the ?. operator to D | ||||
---|---|---|---|---|
| ||||
Posted in reply to Per Nordlöw | On Saturday, 12 September 2020 at 16:51:20 UTC, Per Nordlöw wrote:
> Has there been any discussions on adding the ?. operator to simplify structure matching on class trees typically found in compilers such as dmd.
Hmm the problem with .? making implicit null checks is that it encourages
null checks.
Rather than encouraging a state in which existence and logic are statically asserted.
|
September 13, 2020 Re: Adding the ?. operator to D | ||||
---|---|---|---|---|
| ||||
Posted in reply to Stefan Koch | On Saturday, 12 September 2020 at 19:26:30 UTC, Stefan Koch wrote:
> On Saturday, 12 September 2020 at 16:51:20 UTC, Per Nordlöw wrote:
>> Has there been any discussions on adding the ?. operator to simplify structure matching on class trees typically found in compilers such as dmd.
>
> Hmm the problem with .? making implicit null checks is that it encourages
> null checks.
> Rather than encouraging a state in which existence and logic are statically asserted.
However, its been implemented in most places due its common use in code.
|
September 13, 2020 Re: Adding the ?. operator to D | ||||
---|---|---|---|---|
| ||||
Posted in reply to Stefan Koch | On Saturday, 12 September 2020 at 19:26:30 UTC, Stefan Koch wrote:
> Rather than encouraging a state in which existence and logic are statically asserted.
Are you referring to non-nullable classes? Which has been discussed at older DConfs.
|
September 16, 2020 Re: Adding the ?. operator to D | ||||
---|---|---|---|---|
| ||||
Posted in reply to Per Nordlöw | On 2020-09-12 18:51, Per Nordlöw wrote: > Has there been any discussions on adding the ?. operator to simplify structure matching on class trees typically found in compilers such as dmd. > > Basically, implemented via lowering of > > C ?. EXPR > > to > > C ? C.EXPR : EXPR.init > > when EXPR has type class or pointer and for the other cases either > > C ? C.EXPR : EXPR.init > > or perhaps even > > C ? C.EXPR : nullable(EXPR).init > > if nullable is made part of the runtime or even builtin. > > Given that `C` is a class or pointer to an aggregate. I would like that. It should work for an Optional/Nullable type as well. It's a good idea to add an `??` operator at the same time as well: auto a = A ?? B; if `A` is not null, assign `A` to `a`. Otherwise assign `B` to `a`. -- /Jacob Carlborg |
September 16, 2020 Re: Adding the ?. operator to D | ||||
---|---|---|---|---|
| ||||
Posted in reply to Per Nordlöw | On Saturday, 12 September 2020 at 16:51:20 UTC, Per Nordlöw wrote:
> Has there been any discussions on adding the ?. operator
?:
Try to search word "Elvis" on this forum
|
September 16, 2020 Re: Adding the ?. operator to D | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jacob Carlborg | On Wednesday, 16 September 2020 at 19:01:28 UTC, Jacob Carlborg wrote:
>> C ? C.EXPR : nullable(EXPR).init
>>
> I would like that. It should work for an Optional/Nullable type as well.
You're saying you want the variant with `nullable` and make `nullable` builtin?
A interesting alternative would be to return `&C.EXPR` on success and null otherwise. Kind of like the `in` operator does.
|
September 17, 2020 Re: Adding the ?. operator to D | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jacob Carlborg | On Wednesday, 16 September 2020 at 19:01:28 UTC, Jacob Carlborg wrote:
>
> I would like that. It should work for an Optional/Nullable type as well. It's a good idea to add an `??` operator at the same time as well:
>
> auto a = A ?? B;
>
> if `A` is not null, assign `A` to `a`. Otherwise assign `B` to `a`.
Last time this was discussed, someone posted the following library version:
T orElse(T)(T value, lazy T alternative)
{
return value ? value : alternative;
}
auto a = A.orElse(B);
|
September 17, 2020 Re: Adding the ?. operator to D | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jacob Carlborg Attachments:
| On Wed, 2020-09-16 at 21:01 +0200, Jacob Carlborg via Digitalmars-d wrote: > It's a good idea to add an `??` operator at the same time as well: > > auto a = A ?? B; > > if `A` is not null, assign `A` to `a`. Otherwise assign `B` to `a`. Groovy 1.5 introduced the Elvis Operator, ?:, for this, and most other JVM languages have since followed suit. It seems though GNU C and C++ got there first: https://en.wikipedia.org/wiki/Elvis_operator a = x ?: y is a = x ? x : y with the semantics "if x does not have Boolean evaluation false then x else y". -- Russel. =========================================== Dr Russel Winder t: +44 20 7585 2200 41 Buckmaster Road m: +44 7770 465 077 London SW11 1EN, UK w: www.russel.org.uk |
Copyright © 1999-2021 by the D Language Foundation