non-opaque results

This commit is contained in:
Vyacheslav Matyukhin 2022-08-25 17:47:41 +04:00
parent 7dd9a5bb77
commit 1f22d1f837
No known key found for this signature in database
GPG Key ID: 3D2A774C5489F96C
10 changed files with 15 additions and 63 deletions

View File

@ -1,4 +1,3 @@
type result<'a, 'e> = ForTS_Result.result<'a, 'e> // Use opaque result type
type functionCallInfo = DistributionTypes.DistributionOperation.genericFunctionCallInfo
type genericDist = DistributionTypes.genericDist
type error = DistributionTypes.error

View File

@ -1,4 +1,3 @@
type result<'a, 'e> = ForTS_Result.result<'a, 'e> // Use opaque result type
@genType
let defaultEnv: GenericDist.env

View File

@ -1,5 +1,4 @@
//TODO: multimodal, add interface, test somehow, track performance, refactor sampleSet, refactor ASTEvaluator.res.
type result<'a, 'e> = ForTS_Result.result<'a, 'e> // Use opaque result type
type t = DistributionTypes.genericDist
type error = DistributionTypes.error

View File

@ -1,5 +1,3 @@
type result<'a, 'e> = ForTS_Result.result<'a, 'e> // Use opaque result type
type t = DistributionTypes.genericDist
type error = DistributionTypes.error
type toPointSetFn = t => result<PointSetTypes.pointSetDist, error>

View File

@ -1,6 +1,5 @@
@genType type reducerProject = ReducerProject_T.t //re-export
type result<'a, 'e> = ForTS_Result.result<'a, 'e> // Use opaque result type
type reducerErrorValue = ForTS_Reducer_ErrorValue.reducerErrorValue //use
type squiggleValue = ForTS_SquiggleValue.squiggleValue //use

View File

@ -1,44 +0,0 @@
@genType.opaque type result_<'a, 'e> = result<'a, 'e>
@genType type result<'a, 'e> = result_<'a, 'e> // alias and re-export
@genType let isError = (r: result_<'a, 'e>): bool => Belt.Result.isError(r)
@genType let isOk = (r: result_<'a, 'e>): bool => Belt.Result.isOk(r)
@genType
let getError = (r: result_<'a, 'e>): option<'e> =>
switch r {
| Ok(_) => None
| Error(e) => Some(e)
}
@genType
let getValue = (r: result<'a, 'e>): option<'a> =>
switch r {
| Ok(v) => Some(v)
| Error(_) => None
}
@module("./ForTS_Result_tag") @scope("resultTag")
external rtOk_: int = "RtOk"
@module("./ForTS_Result_tag") @scope("resultTag")
external rtError_: int = "RtError"
@genType.import("./ForTS_Result_tag")
type resultTag
external castEnum: int => resultTag = "%identity"
@genType
let getTag = (r: result<'a, 'e>): resultTag =>
switch r {
| Ok(_) => rtOk_->castEnum
| Error(_) => rtError_->castEnum
}
@genType
let fmap = (r: result<'a, 'e>, f: 'a => 'b): result<'b, 'e> =>
switch r {
| Ok(v) => Ok(f(v))
| Error(e) => Error(e)
}

View File

@ -1,4 +1,9 @@
export enum resultTag {
RtOk,
RtError,
}
export type result<a, b> =
| {
tag: "Ok";
value: a;
}
| {
tag: "Error";
value: b;
};

View File

@ -1,5 +1,4 @@
@genType type squiggleValue = ReducerInterface_InternalExpressionValue.t //re-export
type result_<'a, 'e> = ForTS_Result.result_<'a, 'e> //use
type reducerErrorValue = ForTS_Reducer_ErrorValue.reducerErrorValue //use
@genType type squiggleValue_Array = ReducerInterface_InternalExpressionValue.squiggleArray //re-export recursive type
@ -97,7 +96,7 @@ let toString = (variant: squiggleValue) =>
// This is a useful method for unit tests.
// Convert the result along with the error message to a string.
@genType
let toStringResult = (variantResult: result_<squiggleValue, reducerErrorValue>) =>
let toStringResult = (variantResult: result<squiggleValue, reducerErrorValue>) =>
ReducerInterface_InternalExpressionValue.toStringResult(variantResult)
@genType

View File

@ -1,5 +1,3 @@
@genType type result_<'a, 'e> = ForTS_Result.result_<'a, 'e> //re-export
@genType type result<'a, 'e> = ForTS_Result.result<'a, 'e> //re-export
@genType type reducerErrorValue = ForTS_Reducer_ErrorValue.reducerErrorValue //re-export
@genType type syntaxErrorLocation = ForTS_Reducer_ErrorValue.syntaxErrorLocation //re-export

View File

@ -15,12 +15,12 @@ The below few seem to work fine. In the future there's definitely more work to d
@genType type genericDist = squiggleValue_Distribution //alternative
@genType type sampleSetDist = sampleSetDistribution //alternative
@genType type symbolicDist = symbolicDistribution //alternative
@genType type resultDist = result_<distribution, distributionError> //alternative
@genType type resultFloat = result_<float, distributionError> //alternative
@genType type resultString = result_<string, distributionError> //alternative
@genType type resultDist = result<distribution, distributionError> //alternative
@genType type resultFloat = result<float, distributionError> //alternative
@genType type resultString = result<string, distributionError> //alternative
@genType
let makeSampleSetDist: array<float> => result_<
let makeSampleSetDist: array<float> => result<
sampleSetDist,
SampleSetDist.sampleSetError,
> = SampleSetDist.make
@ -33,7 +33,7 @@ let toPointSet: (
~sampleCount: int,
~xSelection: DistributionTypes.DistributionOperation.pointsetXSelection=?,
unit,
) => result_<PointSetTypes.pointSetDist, distributionError> = GenericDist.toPointSet
) => result<PointSetTypes.pointSetDist, distributionError> = GenericDist.toPointSet
@genType
type mixedShape = PointSetTypes.mixedShape