question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Is there a need for some syntactic suger? Or am i too blind to find it?

See original GitHub issue

Hi, thank you for this nice extensions. Currently i using a similar extension, because of inactivity i assume to change to this one. I used your Maybe<T> functionality extensively to keep the nullables away from me. I came from the scala-world, so in this project am missing some syntactic sugar around the Maybe<T>. In scala it is a little part of pattern matching: e.g.

public static TOut ResolveOr<TIn, TOut>(this Maybe<TIn> source, Func<TIn, TOut> resolver,
            Func<TOut> alternative) => !source.HasValue ? alternative.Invoke() : resolver.Invoke(source.Value);

to realize something like this (resolve the Maybe<T> to a TOut):

Maybe<T> value = 
IEnumerable
.Select(some things)
.Where(some filter)
.TryFirst();

And then:

var result = value.ResolveOr(
resolver => {do a lot of stuff with resolver and give back a value},
() => {when TryFirst will give back nothing, here you can go an alternative way and give back a value}
)

result is then of the result-type of your resolver/alternative method.

Is there a need to put this generic sugar to your extension or exists those things in your extension but i´m too blind to find it (missing reference or else)?

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
vkhorikovcommented, Jun 14, 2021

Is there a Wrapper for nullable (or the counterpart of unwrap)?

There’s an implicit conversion that does that. You can just do this and it will automatically wrap the null into a Maybe:

Maybe<string> MyMethod()
{
    return null;
}

OK, for the Values method there is a Choose method in your project. That seems nearly that what i was looking for. But its usage is a little bit more trickier as i need only a filter for the none-types withoud a Func<T,U>.

We can add an overload for this method that would omit the selector.

0reactions
LaszloLueckcommented, Jun 9, 2021

OK, for the Values method there is a Choose method in your project. That seems nearly that what i was looking for. But its usage is a little bit more trickier as i need only a filter for the none-types withoud a Func<T,U>.

As an example:

var l = new List<Maybe<int>>() {Maybe<int>.None, Maybe<int>.From(2), Maybe<int>.None, Maybe<int>.From(4)};

Your method needs a Func<T, U> selector as parameter. If you only need the filter you must write too much.

var result1 = l.Choose(k => k);

My signature filters only the None-Values and return the rest, so it´s easier to read IMHO.

var result2 = l.Values();

Regards, Laszlo

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why is Syntactic Sugar sometimes considered a bad thing?
It is difficult to reason about syntactic sugar if the reasoning takes place without reference to a context. There are lots of examples...
Read more >
Teaching syntactic sugar
While I agree with this answer that the conditional expression is more than just sugar, I think the following consideration is important in...
Read more >
c# - When to use / not use syntactic sugar
Generally this means avoiding syntactic sugar. It might also mean avoiding pattern matching, list comprehensions, and the like, if those are not ...
Read more >
Are macros really useful?
Moreover, thinking too much about the syntactic sugar aspect make them blind to others and more important aspects of macros: in particular, the...
Read more >
Is syntactic sugar in programming languages bad?
No, syntactic sugar is not inherently bad. It typically provides an alternative way of expressing something, perhaps in a more convenient, compact, intuitive, ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found