Comments (7)
yes but for error, there is no escaping using
return! Error exn
i think?let _: Task<Result<int, exn>> = taskResult { try return 1 //or return! Ok 1 with e -> return! Error e // return e will not work }
Thank you for this explanation, was assigning the error to a different cause.
Yes, this is by design.
return
in a taskResult
is only for the happy path. In your case, this is of type int
, so an Error exn
is a type mismatch.
from fstoolkit.errorhandling.
This works for me:
#r "nuget: FsToolkit.ErrorHandling, 4.15.1"
#r "nuget: FsToolkit.ErrorHandling.TaskResult, 4.15.1"
#r "nuget: Npgsql, 8.0.2"
open System.Threading
open System.Threading.Tasks
open FsToolkit.ErrorHandling
open Npgsql
type IDataSource =
abstract member OpenConnectionAsync : ?cancellationToken : CancellationToken -> ValueTask<NpgsqlConnection>
let foo (_dataSource : IDataSource) =
task {
use! conn = _dataSource.OpenConnectionAsync()
()
}
let bar (_dataSource : IDataSource) =
taskResult {
use! conn = _dataSource.OpenConnectionAsync()
()
}
Perhaps you have an old version of a package?
from fstoolkit.errorhandling.
i think not. it's some intricate type problem beyond my current understanding.
What you wrote is right, hence if i simply write a function, it correctly behaves in both cases. Problem is implementing an interface.
while task version works, taskResult not ->
interface IStrategyRepository with
member this.Save(req: BacktestRequest, saved: DateTime) : Task<Result<int, exn>> =
taskResult {
try
use! conn = _dataSource.OpenConnectionAsync()
says
Type constraint mismatch. The type 'TaskResultCode<Result<int,'a>,exn,Result<int,'a>>' is not compatible with type 'TaskResultCode<int,exn,int>'
version placed simply in a class has exact same signature as implementation of interface
member StrategyRepository.Save2: req: BacktestRequest * saved: DateTime -> Task<Result<int,exn>>
while task works. simply placed inside class, they both work (only implementation of interface fails). that is puzzling for me.
from fstoolkit.errorhandling.
Classes also work for me:
#r "nuget: FsToolkit.ErrorHandling, 4.15.1"
#r "nuget: FsToolkit.ErrorHandling.TaskResult, 4.15.1"
#r "nuget: Npgsql, 8.0.2"
open System
open System.Threading
open System.Threading.Tasks
open FsToolkit.ErrorHandling
open Npgsql
type IDataSource =
abstract member OpenConnectionAsync : ?cancellationToken : CancellationToken -> ValueTask<NpgsqlConnection>
type BacktestRequest = class end
type IStrategyRepository =
abstract member Save : req : BacktestRequest * saved : DateTime -> Task<Result<int, exn>>
type StrategyRepository1(_dataSource : IDataSource) =
interface IStrategyRepository with
member this.Save(req : BacktestRequest, saved : DateTime) =
task {
use! conn = _dataSource.OpenConnectionAsync()
return Ok 1
}
type StrategyRepository2(_dataSource : IDataSource) =
interface IStrategyRepository with
member this.Save(req : BacktestRequest, saved : DateTime) =
taskResult {
use! conn = _dataSource.OpenConnectionAsync()
return 1
}
Maybe I'm missing something?
from fstoolkit.errorhandling.
i think i understand it more, maybe educational. I think try complicates it. Notice how with taskResult i need to use return!, and in task normal return is enough.
type StrategyRepository(dataSource: NpgsqlDataSource) =
let _dataSource: NpgsqlDataSource = dataSource
member this.Save2(req: BacktestRequest, saved: DateTime) : Task<Result<int, exn>> =
task {
try
use! conn = _dataSource.OpenConnectionAsync()
return Ok 1
with e ->
return Error e
}
interface IStrategyRepository with
member this.Save(req: BacktestRequest, saved: DateTime) : Task<Result<int, exn>> =
taskResult {
try
use! conn = _dataSource.OpenConnectionAsync()
return! Ok 1
with e ->
return! Error e
}
alternatively, i could just return 1
from taskResult version, but then condtion in return! Error e
is not satisfied (and return Error e
does not work either, nor return e
. so whole time this was a problem, while it showed that first statement inside taskResult was problematic, which it was not). So that was what was a problem, so it's not really 1:1 replacement in some cases.
from fstoolkit.errorhandling.
Inside of a taskResult
, return! Ok 1
is equivalent to return 1
.
These examples might help with understanding:
let _ : Task<Result<int, string>> =
taskResult {
return 1
}
let _ : Task<Result<Result<int, string>, string>> =
taskResult {
return Ok 1
}
let _ : Task<Result<int, string>> =
taskResult {
return! Ok 1
}
from fstoolkit.errorhandling.
yes but for error, there is no escaping using return! Error exn
i think?
let _: Task<Result<int, exn>> =
taskResult {
try
return 1
//or return! Ok 1
with e ->
return! Error e
// return e will not work
}
Thank you for this explanation, was assigning the error to a different cause.
from fstoolkit.errorhandling.
Related Issues (20)
- FSharp.Core version warning (net7.0) HOT 14
- Make Result.defaultWith accept error, or remove it HOT 2
- Overload issues with TaskResult on generic functions HOT 2
- Add Option.ofPair HOT 5
- Add asyncValidation CE, or make asyncResult (and result?) CEs work with 'and!' HOT 4
- Can we reinstate `TaskResult.foldResult` and the like? HOT 4
- Add Light/Dark Theme Toggle to Gitbook Docs
- Support ValueOption in FsToolkit.Errorhandling.asyncOption HOT 3
- Support TaskValidation to match AsyncValidation HOT 2
- Binding Async<'T option * 'T option> to AsyncValidation is having overload errors HOT 1
- Common Language Runtime detected an invalid program HOT 2
- TaskOption/TaskResult defaultValue and defaultWith
- Support requireValueSome and requireValueNone in Result, TaskResult and AsyncResult
- Async/Job/Task wrapped result + require functions helpers
- feat(Seq): Add sequenceResultA HOT 6
- Readme video unavailable HOT 1
- TaskResultOption compatibility with TaskResult and friends HOT 4
- TaskResult `return! Error ()` results in a compiler error: "Type constraint mismatch". The type 'TaskResultCode<,'b,> is not compatible with type 'TaskResultCode<unit,string,'a>' HOT 2
- Consider using other name for Option.filter HOT 3
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 fstoolkit.errorhandling.