Skip to content

Commit

Permalink
Merge pull request fable-compiler#3739 from ncave/result
Browse files Browse the repository at this point in the history
Separate Result from Choice
  • Loading branch information
MangelMaxime authored Feb 6, 2024
2 parents 472a86c + aa143cf commit c8d7724
Show file tree
Hide file tree
Showing 17 changed files with 352 additions and 323 deletions.
6 changes: 3 additions & 3 deletions src/Fable.Transforms/Dart/Replacements.fs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ let coreModFor =
| BclTimeSpan -> "TimeSpan"
| FSharpSet _ -> "Set"
| FSharpMap _ -> "Map"
| FSharpResult _ -> "Choice"
| FSharpResult _ -> "Result"
| FSharpChoice _ -> "Choice"
| FSharpReference _ -> "Types"
| BclHashSet _ -> "MutableSet"
Expand Down Expand Up @@ -768,7 +768,7 @@ let tryReplacedEntityRef (com: Compiler) entFullName =
| BuiltinDefinition BclTimeSpan -> makeIdentExpr "Duration" |> Some
| BuiltinDefinition BclTimer -> makeImportLib com MetaType "default" "Timer" |> Some
| BuiltinDefinition(FSharpReference _) -> makeImportLib com MetaType "FSharpRef" "Types" |> Some
| BuiltinDefinition(FSharpResult _) -> makeImportLib com MetaType "FSharpResult$2" "Choice" |> Some
| BuiltinDefinition(FSharpResult _) -> makeImportLib com MetaType "FSharpResult$2" "Result" |> Some
| BuiltinDefinition(FSharpChoice genArgs) ->
let membName = $"FSharpChoice${List.length genArgs}"
makeImportLib com MetaType membName "Choice" |> Some
Expand Down Expand Up @@ -2365,7 +2365,7 @@ let results (com: ICompiler) (ctx: Context) r (t: Type) (i: CallInfo) (_: Expr o
| "ToValueOption" as meth -> Some("Result_" + meth)
| _ -> None
|> Option.map (fun meth ->
Helper.LibCall(com, "Choice", meth, t, args, i.SignatureArgTypes, genArgs = i.GenericArgs, ?loc = r)
Helper.LibCall(com, "Result", meth, t, args, i.SignatureArgTypes, genArgs = i.GenericArgs, ?loc = r)
)

let nullables (com: ICompiler) (_: Context) r (t: Type) (i: CallInfo) (thisArg: Expr option) (args: Expr list) =
Expand Down
2 changes: 1 addition & 1 deletion src/Fable.Transforms/Fable2Babel.fs
Original file line number Diff line number Diff line change
Expand Up @@ -800,7 +800,7 @@ module Annotation =
ok
err
]
"Choice"
"Result"
| Replacements.Util.FSharpChoice genArgs ->
$"FSharpChoice${List.length genArgs}{Util.UnionHelpers.UNION_SUFFIX}"
|> makeFableLibImportTypeAnnotation com ctx genArgs "Choice"
Expand Down
4 changes: 2 additions & 2 deletions src/Fable.Transforms/Python/Fable2Python.fs
Original file line number Diff line number Diff line change
Expand Up @@ -914,7 +914,7 @@ module Annotation =
| Types.result, _ ->
let resolved, stmts = resolveGenerics com ctx genArgs repeatedGenerics

fableModuleAnnotation com ctx "choice" "FSharpResult_2" resolved, stmts
fableModuleAnnotation com ctx "result" "FSharpResult_2" resolved, stmts
| Replacements.Util.BuiltinEntity _kind -> stdlibModuleTypeHint com ctx "typing" "Any" []
(*
| Replacements.Util.BclGuid
Expand Down Expand Up @@ -1080,7 +1080,7 @@ module Annotation =
]
repeatedGenerics

fableModuleAnnotation com ctx "choice" "FSharpResult_2" resolved, stmts
fableModuleAnnotation com ctx "result" "FSharpResult_2" resolved, stmts
| _ -> stdlibModuleTypeHint com ctx "typing" "Any" []

let transformFunctionWithAnnotations (com: IPythonCompiler) ctx name (args: Fable.Ident list) (body: Fable.Expr) =
Expand Down
6 changes: 3 additions & 3 deletions src/Fable.Transforms/Python/Replacements.fs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ let coreModFor =
| BclTimeSpan -> "time_span"
| FSharpSet _ -> "set"
| FSharpMap _ -> "map"
| FSharpResult _ -> "choice"
| FSharpResult _ -> "result"
| FSharpChoice _ -> "choice"
| FSharpReference _ -> "types"
| BclHashSet _ -> "mutable_set"
Expand Down Expand Up @@ -1166,7 +1166,7 @@ let tryEntityIdent (com: Compiler) entFullName =
| BuiltinDefinition BclDateTimeOffset -> makeIdentExpr "Date" |> Some
| BuiltinDefinition BclTimer -> makeImportLib com Any "default" "Timer" |> Some
| BuiltinDefinition(FSharpReference _) -> makeImportLib com Any "FSharpRef" "Types" |> Some
| BuiltinDefinition(FSharpResult _) -> makeImportLib com Any "FSharpResult_2" "Choice" |> Some
| BuiltinDefinition(FSharpResult _) -> makeImportLib com Any "FSharpResult_2" "Result" |> Some
| BuiltinDefinition(FSharpChoice genArgs) ->
let membName = $"FSharpChoice_{List.length genArgs}"
makeImportLib com Any membName "Choice" |> Some
Expand Down Expand Up @@ -2608,7 +2608,7 @@ let results (com: ICompiler) (ctx: Context) r (t: Type) (i: CallInfo) (_: Expr o
| "ToOption"
| "ToValueOption" as meth -> Some("Result_" + meth)
| _ -> None
|> Option.map (fun meth -> Helper.LibCall(com, "choice", meth, t, args, i.SignatureArgTypes, ?loc = r))
|> Option.map (fun meth -> Helper.LibCall(com, "result", meth, t, args, i.SignatureArgTypes, ?loc = r))

let nullables (com: ICompiler) (_: Context) r (t: Type) (i: CallInfo) (thisArg: Expr option) (args: Expr list) =
match i.CompiledName, thisArg with
Expand Down
4 changes: 2 additions & 2 deletions src/Fable.Transforms/Replacements.Util.fs
Original file line number Diff line number Diff line change
Expand Up @@ -309,8 +309,8 @@ type BuiltinType =
| BclKeyValuePair of key: Type * value: Type
| FSharpSet of Type
| FSharpMap of key: Type * value: Type
| FSharpChoice of Type list
| FSharpResult of arg1: Type * arg2: Type
| FSharpChoice of genArgs: Type list
| FSharpResult of ok: Type * err: Type
| FSharpReference of Type

let (|BuiltinDefinition|_|) =
Expand Down
6 changes: 3 additions & 3 deletions src/Fable.Transforms/Replacements.fs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ let coreModFor =
| BclTimeSpan -> "TimeSpan"
| FSharpSet _ -> "Set"
| FSharpMap _ -> "Map"
| FSharpResult _ -> "Choice"
| FSharpResult _ -> "Result"
| FSharpChoice _ -> "Choice"
| FSharpReference _ -> "Types"
| BclHashSet _ -> "MutableSet"
Expand Down Expand Up @@ -1127,7 +1127,7 @@ let tryEntityIdent (com: Compiler) entFullName =
| BuiltinDefinition BclDateTimeOffset -> makeIdentExpr "Date" |> Some
| BuiltinDefinition BclTimer -> makeImportLib com Any "default" "Timer" |> Some
| BuiltinDefinition(FSharpReference _) -> makeImportLib com Any "FSharpRef" "Types" |> Some
| BuiltinDefinition(FSharpResult _) -> makeImportLib com Any "FSharpResult$2" "Choice" |> Some
| BuiltinDefinition(FSharpResult _) -> makeImportLib com Any "FSharpResult$2" "Result" |> Some
| BuiltinDefinition(FSharpChoice genArgs) ->
let membName = $"FSharpChoice${List.length genArgs}"
makeImportLib com Any membName "Choice" |> Some
Expand Down Expand Up @@ -2733,7 +2733,7 @@ let results (com: ICompiler) (ctx: Context) r (t: Type) (i: CallInfo) (_: Expr o
| "ToValueOption" as meth -> Some("Result_" + meth)
| _ -> None
|> Option.map (fun meth ->
Helper.LibCall(com, "Choice", meth, t, args, i.SignatureArgTypes, genArgs = i.GenericArgs, ?loc = r)
Helper.LibCall(com, "Result", meth, t, args, i.SignatureArgTypes, genArgs = i.GenericArgs, ?loc = r)
)

let nullables (com: ICompiler) (_: Context) r (t: Type) (i: CallInfo) (thisArg: Expr option) (args: Expr list) =
Expand Down
114 changes: 0 additions & 114 deletions src/fable-library-dart/Choice.fs
Original file line number Diff line number Diff line change
@@ -1,119 +1,5 @@
namespace FSharp.Core

[<CompiledName("FSharpResult`2")>]
type Result<'T, 'TError> =
| Ok of ResultValue: 'T
| Error of ErrorValue: 'TError

module Result =
[<CompiledName("Map")>]
let map mapping result =
match result with
| Error e -> Error e
| Ok x -> Ok(mapping x)

[<CompiledName("MapError")>]
let mapError mapping result =
match result with
| Error e -> Error(mapping e)
| Ok x -> Ok x

[<CompiledName("Bind")>]
let bind binder result =
match result with
| Error e -> Error e
| Ok x -> binder x

[<CompiledName("IsOk")>]
let isOk (result: Result<'a, 'b>) : bool =
match result with
| Error _ -> false
| Ok _ -> true

[<CompiledName("IsError")>]
let isError (result: Result<'a, 'b>) : bool =
match result with
| Error _ -> true
| Ok _ -> false

[<CompiledName("Contains")>]
let contains (value: 'a) (result: Result<'a, 'b>) : bool =
match result with
| Error _ -> false
| Ok x -> x = value

[<CompiledName("Count")>]
let count (result: Result<'a, 'b>) : int =
match result with
| Error _ -> 0
| Ok _ -> 1

[<CompiledName("DefaultValue")>]
let defaultValue (defaultValue: 'a) (result: Result<'a, 'b>) : 'a =
match result with
| Error _ -> defaultValue
| Ok x -> x

[<CompiledName("DefaultWith")>]
let defaultWith (defThunk: 'b -> 'a) (result: Result<'a, 'b>) : 'a =
match result with
| Error e -> defThunk e
| Ok x -> x

[<CompiledName("Exists")>]
let exists (predicate: 'a -> bool) (result: Result<'a, 'b>) : bool =
match result with
| Error _ -> false
| Ok x -> predicate x

[<CompiledName("Fold")>]
let fold<'a, 'b, 's> (folder: 's -> 'a -> 's) (state: 's) (result: Result<'a, 'b>) : 's =
match result with
| Error _ -> state
| Ok x -> folder state x

[<CompiledName("FoldBack")>]
let foldBack<'a, 'b, 's> (folder: 'a -> 's -> 's) (result: Result<'a, 'b>) (state: 's) : 's =
match result with
| Error _ -> state
| Ok x -> folder x state

[<CompiledName("ForAll")>]
let forall (predicate: 'a -> bool) (result: Result<'a, 'b>) : bool =
match result with
| Error _ -> true
| Ok x -> predicate x

[<CompiledName("Iterate")>]
let iterate (action: 'a -> unit) (result: Result<'a, 'b>) : unit =
match result with
| Error _ -> ()
| Ok x -> action x

[<CompiledName("ToArray")>]
let toArray (result: Result<'a, 'b>) : 'a[] =
match result with
| Error _ -> [||]
| Ok x -> [| x |]

[<CompiledName("ToList")>]
let toList (result: Result<'a, 'b>) : 'a list =
match result with
| Error _ -> []
| Ok x -> [ x ]

[<CompiledName("ToOption")>]
let toOption (result: Result<'a, 'b>) : Option<'a> =
match result with
| Error _ -> None
| Ok x -> Some x

[<CompiledName("ToValueOption")>]
let toValueOption (result: Result<'a, 'b>) : ValueOption<'a> =
match result with
| Error _ -> ValueNone
| Ok x -> ValueSome x

[<CompiledName("FSharpChoice`2")>]
type Choice<'T1, 'T2> =
| Choice1Of2 of 'T1
Expand Down
1 change: 1 addition & 0 deletions src/fable-library-dart/Fable.Library.Dart.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<Compile Include="Global.fs" />
<Compile Include="FSharp.Core.fs" />
<Compile Include="Option.fs" />
<Compile Include="Result.fs" />
<Compile Include="Choice.fs" />
<Compile Include="Seq.fs" />
<Compile Include="Seq2.fs" />
Expand Down
115 changes: 115 additions & 0 deletions src/fable-library-dart/Result.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
namespace FSharp.Core

[<CompiledName("FSharpResult`2")>]
type Result<'T, 'TError> =
| Ok of ResultValue: 'T
| Error of ErrorValue: 'TError

module Result =
[<CompiledName("Map")>]
let map mapping result =
match result with
| Error e -> Error e
| Ok x -> Ok(mapping x)

[<CompiledName("MapError")>]
let mapError mapping result =
match result with
| Error e -> Error(mapping e)
| Ok x -> Ok x

[<CompiledName("Bind")>]
let bind binder result =
match result with
| Error e -> Error e
| Ok x -> binder x

[<CompiledName("IsOk")>]
let isOk (result: Result<'a, 'b>) : bool =
match result with
| Error _ -> false
| Ok _ -> true

[<CompiledName("IsError")>]
let isError (result: Result<'a, 'b>) : bool =
match result with
| Error _ -> true
| Ok _ -> false

[<CompiledName("Contains")>]
let contains (value: 'a) (result: Result<'a, 'b>) : bool =
match result with
| Error _ -> false
| Ok x -> x = value

[<CompiledName("Count")>]
let count (result: Result<'a, 'b>) : int =
match result with
| Error _ -> 0
| Ok _ -> 1

[<CompiledName("DefaultValue")>]
let defaultValue (defaultValue: 'a) (result: Result<'a, 'b>) : 'a =
match result with
| Error _ -> defaultValue
| Ok x -> x

[<CompiledName("DefaultWith")>]
let defaultWith (defThunk: 'b -> 'a) (result: Result<'a, 'b>) : 'a =
match result with
| Error e -> defThunk e
| Ok x -> x

[<CompiledName("Exists")>]
let exists (predicate: 'a -> bool) (result: Result<'a, 'b>) : bool =
match result with
| Error _ -> false
| Ok x -> predicate x

[<CompiledName("Fold")>]
let fold<'a, 'b, 's> (folder: 's -> 'a -> 's) (state: 's) (result: Result<'a, 'b>) : 's =
match result with
| Error _ -> state
| Ok x -> folder state x

[<CompiledName("FoldBack")>]
let foldBack<'a, 'b, 's> (folder: 'a -> 's -> 's) (result: Result<'a, 'b>) (state: 's) : 's =
match result with
| Error _ -> state
| Ok x -> folder x state

[<CompiledName("ForAll")>]
let forall (predicate: 'a -> bool) (result: Result<'a, 'b>) : bool =
match result with
| Error _ -> true
| Ok x -> predicate x

[<CompiledName("Iterate")>]
let iterate (action: 'a -> unit) (result: Result<'a, 'b>) : unit =
match result with
| Error _ -> ()
| Ok x -> action x

[<CompiledName("ToArray")>]
let toArray (result: Result<'a, 'b>) : 'a[] =
match result with
| Error _ -> [||]
| Ok x -> [| x |]

[<CompiledName("ToList")>]
let toList (result: Result<'a, 'b>) : 'a list =
match result with
| Error _ -> []
| Ok x -> [ x ]

[<CompiledName("ToOption")>]
let toOption (result: Result<'a, 'b>) : Option<'a> =
match result with
| Error _ -> None
| Ok x -> Some x

[<CompiledName("ToValueOption")>]
let toValueOption (result: Result<'a, 'b>) : ValueOption<'a> =
match result with
| Error _ -> ValueNone
| Ok x -> ValueSome x
1 change: 1 addition & 0 deletions src/fable-library-py/fable_library/Fable.Library.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
<Compile Include="Native.fs" />
<Compile Include="../../fable-library/MutableMap.fs" Link="MutableMap.fs" />
<Compile Include="../../fable-library/MutableSet.fs" Link="MutableSet.fs" />
<Compile Include="../../fable-library/Result.fs" Link="Result.fs" />
<Compile Include="../../fable-library/Choice.fs" Link="Choice.fs" />
<Compile Include="Array.fs" />
<Compile Include="List.fs" />
Expand Down
Loading

0 comments on commit c8d7724

Please sign in to comment.