Comments (4)
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.
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.
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.
Closing due to this: #1269
from language-ext.
Related Issues (20)
- Creating a custom Either type with a custom fixed Left type. HOT 1
- Conflict between LangugeExt Extension methods and LINQ Extension methods HOT 2
- AffRxExtensions.Consume blocks the waiting thread on IObservable's completion
- How to make LanguageExt.CodeGen work on Unity? HOT 2
- Seq<EitherAsync<A, B>>.SequenceParallel does not preserve order HOT 11
- FileIO bug for Test when opening new file for writing
- Directory IO bug for Test when enumerating files
- Simple Kickstart Project available? CodeGen tool is outdated? dotnet8 HOT 1
- Implicit conversion of int? to Option<int> HOT 2
- SequenceParallel performance degradation in 4.4.8 HOT 1
- Refit.ApiException: 'An error occured deserializing the response.'
- Constructor not found for LanguageExt.LongRange HOT 1
- Alternative to inherit from Fin and other monads
- EitherAsync is missing HOT 4
- Add `DoAsync` to Either types (and maybe others) HOT 2
- Release build fails for a big application HOT 5
- v5 resource tracking issue? HOT 6
- Latest v5 alpha IEnumerable<T> ToSeq() extension is missing HOT 4
- v5 await more than a single forked IO, similar to Task.WhenAll HOT 2
- Higher-Kinded Types in Algebraic Free Monads with language-ext?
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from language-ext.