Giter Club home page Giter Club logo

Comments (4)

louthy avatar louthy commented on June 2, 2024

Yep, that's a part of the library that's not been converted over to use nullable markup yet. I'll get to that eventually.

In the meantime there's also:

TryOptionAsync<A>(Option<A> v)
TryOptionAsync<A>(Task<Option<A>> v)

Which allows you to construct TryOptionAsync from Option, so convert your string? to an Option first using Prelude.Optional.

I'd also recommend, (depending on how deep your usage of the Try* monads are), to instead use Eff<A> and Aff<A> - they're much more featureful than Try* and are the primary focus of future development; the Try* types should be seen as legacy now (although I will keep supporting them, and will update them to nullable, they aren't going to be the future focus).

from language-ext.

WhatzGames avatar WhatzGames commented on June 2, 2024

Alright, thanks for the tip.

So I'd assume that (considering the nullability is fixed) I'd have to change

TryOptionAsync(SomethingReturningStringAsync())

To

Aff(async () => Prelude.Optional(await SomethingReturningStringAsync())

Or if desired

Aff<Option<string>>(async () => await SomethingReturningStringAsync())

from language-ext.

louthy avatar louthy commented on June 2, 2024

This:

TryOptionAsync(SomethingReturningStringAsync())

... is only the right way to go if you never need the execution of the SomethingReturningStringAsync wrapped inside the Try*. If you think about it the function SomethingReturningStringAsync could throw an exception and it not be caught, because you're only putting the result of SomethingReturningStringAsync() inside a Try* after it's run.

Which is why Try* uses delegates. This is the correct way to lift a function into a Try*:

// NOTE: no invocation of the function
var operation = TryOptionAsync(SomethingReturningStringAsync);  

The same goes for Aff (as it also catches exceptions). The equivalent lifting functions are Aff and AffMaybe. But in your case, because you want to get rid of the nullable-reference, you can add some additional conversion functions:

public static class YourPrelude
{
    public static Aff<A> OptionalAff<A>(this Func<Task<A?>> task) =>
        OptionalAff(task, Errors.None);
    
    public static Aff<A> OptionalAff<A>(this Func<Task<A?>> task, Error fail) =>
        AffMaybe(async () => 
            await task() switch
            {
                null => fail,
                var x => FinSucc(x)
            });
    
    public static Aff<A> OptionalAff<A>(this Func<ValueTask<A?>> task) =>
        OptionalAff(task, Errors.None);
    
    public static Aff<A> OptionalAff<A>(this Func<ValueTask<A?>> task, Error fail) =>
        AffMaybe(async () => 
            await task() switch
            {
                null => fail,
                var x => FinSucc(x)
            });
}

These make sure the Task is run inside of the Aff (so exceptions are caught properly), but also pattern-matches on null to turn it into the Error value provided.

You can then write:

    Aff<string> aff = OptionalAff(SomethingReturningStringAsync);

from language-ext.

louthy avatar louthy commented on June 2, 2024

Closing due to this: #1269

from language-ext.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.