[OT] C# 6.0 ?. null propagation operator

Atila Neves via Digitalmars-d digitalmars-d at puremagic.com
Tue Apr 21 06:15:01 PDT 2015


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 = ?

I was trying to write a Maybe monad in D, not make it easy to use.
An easy to use solution would probably make use of opDispatch. 
But...:

auto roleName = userManager.bind!(a => a.CurrentUser.bind!(b => 
b.GetRole.bind!(c => c.Name);
//roleName's type is Maybe!string.

The only reason monads are readable in Haskell is due to "do" 
syntax sugar.


More information about the Digitalmars-d mailing list