You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Please add support to use the System.Decimal type (my own use case only requires decimal for parameters but it would be nice to support for parsing as well). To prevent breaking changes to existing code that utilizes xFunc, the support can be made optional so that default behavior is to continue to use double, and decimal-support must be explicitly opted-in for parsing.
Parser Example:
// assuming Parser ctor is given a new overload or an optional parameter:varparser=newParser(parseOptions:newParseOptions{DefaultNumericType=NumberType.Decimal});// or assuming the Parse method is given a new overload or an optional parameter:varexpression=parser.Parse("2.087991 * 3.14159". new ParseOptions {DefaultNumericType=NumberType.Decimal});// maybe add a static property as well:Parser.DefaultParseOptions=newParseOptions{DefaultNumericType=NumberType.Decimal};// if ParseOptions is not specified, use 'new ParseOptions { DefaultNumericType = NumberType.Double }` by default.varparser=newParser();varexpression=parser.Parse("2.087991 * 3.14159");
For parameters, handling can be modified to transparently convert between double/decimal and preserve precision when mathing between the two value types by subclassing (i.e., DecimalNumberValue and DoubleNumberValue with NumberValue refactored to an abstract base) or internally within NumberValue using a variation on the Maybe monad (similar to OneOf, except limited to decimal/double). Because existing code is limited exclusively to double, there should be little risk of breaking changes to existing consumers on update.
Barring explicit support for decimal types, maybe an extension point that the various operation nodes can support to defer evaluation to another object?
Example:
// supporting custom value types for multiplication// marker/functional interfaces defined within xFuncpublicinterfaceICanMath{}publicinterfaceICanMultiply:ICanMath{boolTryMultiplyAsLeftOperand(objectright,outICanMathresult);boolTryMultiplyAsRightOperand(objectleft,outICanMathresult);}// example custom value typepublicrecordDecimalValue(decimalValue):ICanMultiply{publicboolTryMultiplyAsLeftOperand(objectright,outICanMathresult)=>TryMultiply(this,right,outresult);publicboolTryMultiplyAsRightOperand(objectleft,[NotNullWhen(true)]outICanMathresult)=>TryMultiply(left,this,outresult);privatestaticboolTryMultiply(objectleft,objectright,[NotNullWhen(true)]outICanMathresult){result=TryGetDecimal(left,outdecimal?leftOperand)&&TryGetDecimal(right,outdecimal?rightOperand)?newDecimalValue(leftOperand.Value*rightOperand.Value):null;returnresult!=null;}privatestaticboolTryGetDecimal(objectvalue,[NotNullWhen(true)]outdecimal?dec){dec=valueswitch{decimald=>d,doubledbl=>(decimal)dbl,NumberValuenv=>(decimal)nv.Number,DecimalValuedv=>dv.Value,
_ =>null};returndec.HasValue;}publicstaticimplicitoperatorParameterValue(DecimalValuedecimalValue)=>newParameterValue(decimalValue);publicstaticimplicitoperatorDecimalValue(decimalvalue)=>newDecimalValue(value);}// within xFunc.Maths.Expression.Mul.Execute:return(leftResult,rightResult)switch{(NumberValueleft,NumberValueright)=>left*right,
...(ICanMultiplyleft,objectright)whenleft.TryMultiplyAsLeftOperand(right,outICanMathresult)=>result,(objectleft,ICanMultiplyright)whenright.TryMultiplyAsLeftOperand(left,outICanMathresult)=>result,
_ =>throwExecutionException.For(this),};// within ParemeterValuepublicParameterValue(ICanMath value):this(valueasobject)// update internal ctor to recognize ICanMath as valid type{}// parse/execute:varp=newParser();varexpression=p.Parse("2 * myVar");varparameters=newExpressionParameters{["myVar"]=newDecimalValue(3.14159M)};varresult=expression.Execute(parameters);// 'result' is an instance of DecimalValue
The text was updated successfully, but these errors were encountered:
Please add support to use the
System.Decimal
type (my own use case only requiresdecimal
for parameters but it would be nice to support for parsing as well). To prevent breaking changes to existing code that utilizes xFunc, the support can be made optional so that default behavior is to continue to usedouble
, anddecimal
-support must be explicitly opted-in for parsing.Parser Example:
For parameters, handling can be modified to transparently convert between
double
/decimal
and preserve precision when mathing between the two value types by subclassing (i.e.,DecimalNumberValue
andDoubleNumberValue
withNumberValue
refactored to an abstract base) or internally withinNumberValue
using a variation on the Maybe monad (similar to OneOf, except limited todecimal
/double
). Because existing code is limited exclusively todouble
, there should be little risk of breaking changes to existing consumers on update.Barring explicit support for
decimal
types, maybe an extension point that the various operation nodes can support to defer evaluation to another object?Example:
The text was updated successfully, but these errors were encountered: