Thread overview | |||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
April 20, 2015 [OT] C# 6.0 ?. null propagation operator | ||||
---|---|---|---|---|
| ||||
http://www.reddit.com/r/programming/comments/335b1s/the_new_operator_in_c_6/ of interesting note was the nim sample on how to implement the same thing in nim in 2 lines of code template `?.`(a, b): expr = if a != nil: a.b else: nil template `??`(a, b): expr = if a != nil: a else: b |
April 20, 2015 Re: [OT] C# 6.0 ?. null propagation operator | ||||
---|---|---|---|---|
| ||||
Posted in reply to weaselcat | On Monday, 20 April 2015 at 08:24:08 UTC, weaselcat wrote: > http://www.reddit.com/r/programming/comments/335b1s/the_new_operator_in_c_6/ > > of interesting note was the nim sample on how to implement the same thing in nim in 2 lines of code > > template `?.`(a, b): expr = > if a != nil: a.b else: nil > > template `??`(a, b): expr = > if a != nil: a else: b This is what I came up with for D: https://gist.github.com/JakobOvrum/7e3a7bc130ab7db28de3 Meh. |
April 20, 2015 Re: [OT] C# 6.0 ?. null propagation operator | ||||
---|---|---|---|---|
| ||||
Posted in reply to weaselcat | On Monday, 20 April 2015 at 08:24:08 UTC, weaselcat wrote:
> http://www.reddit.com/r/programming/comments/335b1s/the_new_operator_in_c_6/
>
> of interesting note was the nim sample on how to implement the same thing in nim in 2 lines of code
>
> template `?.`(a, b): expr =
> if a != nil: a.b else: nil
>
> template `??`(a, b): expr =
> if a != nil: a else: b
HS Teoh came up with a beautiful solution using opDispatch, but I can't find the thread right now. That also reminds me that I meant to make a PR for it and completely forgot.
|
April 20, 2015 Re: [OT] C# 6.0 ?. null propagation operator | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jakob Ovrum | On Monday, 20 April 2015 at 12:16:21 UTC, Jakob Ovrum wrote: > On Monday, 20 April 2015 at 08:24:08 UTC, weaselcat wrote: >> http://www.reddit.com/r/programming/comments/335b1s/the_new_operator_in_c_6/ >> >> of interesting note was the nim sample on how to implement the same thing in nim in 2 lines of code >> >> template `?.`(a, b): expr = >> if a != nil: a.b else: nil >> >> template `??`(a, b): expr = >> if a != nil: a else: b > > This is what I came up with for D: > > https://gist.github.com/JakobOvrum/7e3a7bc130ab7db28de3 > > Meh. Here's mine: https://gist.github.com/atilaneves/727d63f0a7029032d7ac |
April 20, 2015 Re: [OT] C# 6.0 ?. null propagation operator | ||||
---|---|---|---|---|
| ||||
Posted in reply to Atila Neves | On Monday, 20 April 2015 at 15:37:02 UTC, Atila Neves wrote: > On Monday, 20 April 2015 at 12:16:21 UTC, Jakob Ovrum wrote: >> On Monday, 20 April 2015 at 08:24:08 UTC, weaselcat wrote: >>> http://www.reddit.com/r/programming/comments/335b1s/the_new_operator_in_c_6/ >>> >>> of interesting note was the nim sample on how to implement the same thing in nim in 2 lines of code >>> >>> template `?.`(a, b): expr = >>> if a != nil: a.b else: nil >>> >>> template `??`(a, b): expr = >>> if a != nil: a else: b >> >> This is what I came up with for D: >> >> https://gist.github.com/JakobOvrum/7e3a7bc130ab7db28de3 >> >> Meh. > > Here's mine: > > https://gist.github.com/atilaneves/727d63f0a7029032d7ac I fail to understand Atila example. Just to be sure: C#: var roleName = userManager.CurrentUser?.GetRole()?.Name; D (Jakob): auto roleName = userManager.getOrNull!("CurrentUser", "GetRole", "Name"); D (Atila): auto roleName = ? |
April 20, 2015 Re: [OT] C# 6.0 ?. null propagation operator | ||||
---|---|---|---|---|
| ||||
Posted in reply to Meta | On Monday, 20 April 2015 at 14:19:06 UTC, Meta wrote: > On Monday, 20 April 2015 at 08:24:08 UTC, weaselcat wrote: >> http://www.reddit.com/r/programming/comments/335b1s/the_new_operator_in_c_6/ >> >> of interesting note was the nim sample on how to implement the same thing in nim in 2 lines of code >> >> template `?.`(a, b): expr = >> if a != nil: a.b else: nil >> >> template `??`(a, b): expr = >> if a != nil: a else: b > > HS Teoh came up with a beautiful solution using opDispatch, but I can't find the thread right now. That also reminds me that I meant to make a PR for it and completely forgot. http://forum.dlang.org/thread/lnsc0c$1sip$1@digitalmars.com?page=6#post-mailman.2584.1403213951.2907.digitalmars-d:40puremagic.com |
April 20, 2015 Re: [OT] C# 6.0 ?. null propagation operator | ||||
---|---|---|---|---|
| ||||
Posted in reply to rumbu | On Monday, 20 April 2015 at 20:09:59 UTC, rumbu wrote: > On Monday, 20 April 2015 at 15:37:02 UTC, Atila Neves wrote: >> On Monday, 20 April 2015 at 12:16:21 UTC, Jakob Ovrum wrote: >>> On Monday, 20 April 2015 at 08:24:08 UTC, weaselcat wrote: >>>> http://www.reddit.com/r/programming/comments/335b1s/the_new_operator_in_c_6/ >>>> >>>> of interesting note was the nim sample on how to implement the same thing in nim in 2 lines of code >>>> >>>> template `?.`(a, b): expr = >>>> if a != nil: a.b else: nil >>>> >>>> template `??`(a, b): expr = >>>> if a != nil: a else: b >>> >>> This is what I came up with for D: >>> >>> https://gist.github.com/JakobOvrum/7e3a7bc130ab7db28de3 >>> >>> Meh. >> >> Here's mine: >> >> https://gist.github.com/atilaneves/727d63f0a7029032d7ac > > > I fail to understand Atila example. Just to be sure: > > C#: > var roleName = userManager.CurrentUser?.GetRole()?.Name; > > D (Jakob): > auto roleName = userManager.getOrNull!("CurrentUser", "GetRole", "Name"); > > D (Atila): > auto roleName = ? These API look overly complex. Why not simply: maybe(userManager).CurrentUser.GetRole().Name ? Also, CAPITAL LETTER ARE THE BEST ! |
April 20, 2015 Re: [OT] C# 6.0 ?. null propagation operator | ||||
---|---|---|---|---|
| ||||
Posted in reply to deadalnix | On Monday, 20 April 2015 at 21:22:53 UTC, deadalnix wrote: > On Monday, 20 April 2015 at 20:09:59 UTC, rumbu wrote: .. >> >> >> I fail to understand Atila example. Just to be sure: >> >> C#: >> var roleName = userManager.CurrentUser?.GetRole()?.Name; >> >> D (Jakob): >> auto roleName = userManager.getOrNull!("CurrentUser", "GetRole", "Name"); >> >> D (Atila): >> auto roleName = ? > > These API look overly complex. Why not simply: > > maybe(userManager).CurrentUser.GetRole().Name ? > > Also, CAPITAL LETTER ARE THE BEST ! Because CurrentUser, GetRole() and Name can also return null in this scenario. |
April 20, 2015 Re: [OT] C# 6.0 ?. null propagation operator | ||||
---|---|---|---|---|
| ||||
Posted in reply to rumbu | On 04/20/2015 03:18 PM, rumbu wrote: > On Monday, 20 April 2015 at 21:22:53 UTC, deadalnix wrote: >> On Monday, 20 April 2015 at 20:09:59 UTC, rumbu wrote: > .. >>> >>> >>> I fail to understand Atila example. Just to be sure: >>> >>> C#: >>> var roleName = userManager.CurrentUser?.GetRole()?.Name; >>> >>> D (Jakob): >>> auto roleName = userManager.getOrNull!("CurrentUser", "GetRole", >>> "Name"); >>> >>> D (Atila): >>> auto roleName = ? >> >> These API look overly complex. Why not simply: >> >> maybe(userManager).CurrentUser.GetRole().Name ? >> >> Also, CAPITAL LETTER ARE THE BEST ! > > Because CurrentUser, GetRole() and Name can also return null in this > scenario. > The solution linked elsewhere in this thread solves that issue as well: http://forum.dlang.org/thread/lnsc0c$1sip$1@digitalmars.com?page=6#post-mailman.2584.1403213951.2907.digitalmars-d:40puremagic.com Ali |
April 20, 2015 Re: [OT] C# 6.0 ?. null propagation operator | ||||
---|---|---|---|---|
| ||||
Posted in reply to rumbu | On Monday, 20 April 2015 at 22:18:31 UTC, rumbu wrote:
> On Monday, 20 April 2015 at 21:22:53 UTC, deadalnix wrote:
>> On Monday, 20 April 2015 at 20:09:59 UTC, rumbu wrote:
> ..
>>>
>>>
>>> I fail to understand Atila example. Just to be sure:
>>>
>>> C#:
>>> var roleName = userManager.CurrentUser?.GetRole()?.Name;
>>>
>>> D (Jakob):
>>> auto roleName = userManager.getOrNull!("CurrentUser", "GetRole", "Name");
>>>
>>> D (Atila):
>>> auto roleName = ?
>>
>> These API look overly complex. Why not simply:
>>
>> maybe(userManager).CurrentUser.GetRole().Name ?
>>
>> Also, CAPITAL LETTER ARE THE BEST !
>
> Because CurrentUser, GetRole() and Name can also return null in this scenario.
No, they all return a maybe, that is the nature of maybe monad.
|
Copyright © 1999-2021 by the D Language Foundation