Class Result
Represents the result of expression evaluation.
public abstract class Result
- Inheritance
-
Result
- Derived
- Inherited Members
Examples
Get result by using the TryGetNumber(out NumberValue?) method:
var processor = new Processor();
var result = processor.Solve("2 + 2");
if (result.TryGetNumber(out var number))
Console.WriteLine(number); // outputs '4'
Get result by using the Number property:
var processor = new Processor();
var result = processor.Solve("2 + 2");
var number = result.Number; // equals '4'
Get result by using the ToString() implementation:
var processor = new Processor();
var result = processor.Solve("2 + 2");
Console.WriteLine(result); // outputs '4'
Get result by using the pattern matching:
var processor = new Processor();
var result = processor.Solve("2 + 2");
if (result is Result.NumberResult numberResult)
Console.WriteLine(numberResult.Number); // outputs '4'
Remarks
It is a hand-made implementation of the discriminated union. The Result class provides the abstraction (root class) for DU, whereas implementation for each possible return type is dedicated to the appropriate nested result class.
Properties
Angle
Gets the angle value.
public virtual AngleValue Angle { get; }
Property Value
Exceptions
- InvalidCastException
Thrown when the current instance is not Result.AngleResult.
Area
Gets the area value.
public virtual AreaValue Area { get; }
Property Value
Exceptions
- InvalidCastException
Thrown when the current instance is not Result.AreaResult.
Bool
Gets the boolean value.
public virtual bool Bool { get; }
Property Value
Exceptions
- InvalidCastException
Thrown when the current instance is not Result.BooleanResult.
ComplexNumber
Gets the complex number value.
public virtual Complex ComplexNumber { get; }
Property Value
Exceptions
- InvalidCastException
Thrown when the current instance is not Result.ComplexNumberResult.
Lambda
Gets the lambda value.
public virtual Lambda Lambda { get; }
Property Value
Exceptions
- InvalidCastException
Thrown when the current instance is not Result.LambdaResult.
Length
Gets the length value.
public virtual LengthValue Length { get; }
Property Value
Exceptions
- InvalidCastException
Thrown when the current instance is not Result.LengthResult.
Mass
Gets the mass value.
public virtual MassValue Mass { get; }
Property Value
Exceptions
- InvalidCastException
Thrown when the current instance is not Result.MassResult.
Matrix
Gets the matrix value.
public virtual MatrixValue Matrix { get; }
Property Value
Exceptions
- InvalidCastException
Thrown when the current instance is not Result.MatrixResult.
Number
Gets the number value.
public virtual NumberValue Number { get; }
Property Value
Exceptions
- InvalidCastException
Thrown when the current instance is not Result.NumberResult.
Power
Gets the power value.
public virtual PowerValue Power { get; }
Property Value
Exceptions
- InvalidCastException
Thrown when the current instance is not Result.PowerResult.
Rational
Gets the rational value.
public virtual RationalValue Rational { get; }
Property Value
Exceptions
- InvalidCastException
Thrown when the current instance is not Result.RationalResult.
String
Gets the string value.
public virtual string String { get; }
Property Value
Exceptions
- InvalidCastException
Thrown when the current instance is not Result.StringResult.
Temperature
Gets the temperature value.
public virtual TemperatureValue Temperature { get; }
Property Value
Exceptions
- InvalidCastException
Thrown when the current instance is not Result.TemperatureResult.
Time
Gets the time value.
public virtual TimeValue Time { get; }
Property Value
Exceptions
- InvalidCastException
Thrown when the current instance is not Result.TimeResult.
Vector
Gets the vector value.
public virtual VectorValue Vector { get; }
Property Value
Exceptions
- InvalidCastException
Thrown when the current instance is not Result.VectorResult.
Volume
Gets the volume value.
public virtual VolumeValue Volume { get; }
Property Value
Exceptions
- InvalidCastException
Thrown when the current instance is not Result.VolumeResult.
Methods
Create(object)
Wraps the result of expression evaluation into Result.
public static Result Create(object result)
Parameters
resultobjectThe result of expression evaluation.
Returns
- Result
The result.
Exceptions
- InvalidResultException
The type of
resultis not supported.
TryGetAngle(out AngleValue?)
Gets the angle value.
public bool TryGetAngle(out AngleValue? angleValue)
Parameters
angleValueAngleValue?The angle value.
Returns
- bool
trueif the current instance contains AngleValue, otherwisefalse.
TryGetArea(out AreaValue?)
Gets the area value.
public bool TryGetArea(out AreaValue? areaValue)
Parameters
areaValueAreaValue?The area value.
Returns
TryGetBoolean(out bool?)
Gets the boolean value.
public bool TryGetBoolean(out bool? boolValue)
Parameters
boolValuebool?The boolean value.
Returns
TryGetComplexNumber(out Complex?)
Gets the complex number.
public bool TryGetComplexNumber(out Complex? complexNumberValue)
Parameters
complexNumberValueComplex?The complex number.
Returns
TryGetLambda(out Lambda?)
Gets the lambda.
public bool TryGetLambda(out Lambda? lambda)
Parameters
lambdaLambda?The lambda.
Returns
TryGetLength(out LengthValue?)
Gets the length value.
public bool TryGetLength(out LengthValue? lengthValue)
Parameters
lengthValueLengthValue?The length value.
Returns
- bool
trueif the current instance contains LengthValue, otherwisefalse.
TryGetMass(out MassValue?)
Gets the mass value.
public bool TryGetMass(out MassValue? massValue)
Parameters
massValueMassValue?The mass value.
Returns
TryGetMatrix(out MatrixValue?)
Gets the matrix value.
public bool TryGetMatrix(out MatrixValue? matrixValue)
Parameters
matrixValueMatrixValue?The matrix value.
Returns
- bool
trueif the current instance contains MatrixValue, otherwisefalse.
TryGetNumber(out NumberValue?)
Gets the number value.
public bool TryGetNumber(out NumberValue? number)
Parameters
numberNumberValue?The number value.
Returns
- bool
trueif the current instance contains NumberValue, otherwisefalse.
TryGetPower(out PowerValue?)
Gets the power value.
public bool TryGetPower(out PowerValue? powerValue)
Parameters
powerValuePowerValue?The power value.
Returns
- bool
trueif the current instance contains PowerValue, otherwisefalse.
TryGetRational(out RationalValue?)
Gets the rational value.
public bool TryGetRational(out RationalValue? rationalValue)
Parameters
rationalValueRationalValue?The rational value.
Returns
- bool
trueif the current instance contains RationalValue, otherwisefalse.
TryGetString(out string?)
Gets the string.
public bool TryGetString(out string? stringValue)
Parameters
stringValuestringThe string value.
Returns
TryGetTemperature(out TemperatureValue?)
Gets the temperature value.
public bool TryGetTemperature(out TemperatureValue? temperatureValue)
Parameters
temperatureValueTemperatureValue?The temperature value.
Returns
- bool
trueif the current instance contains TemperatureValue, otherwisefalse.
TryGetTime(out TimeValue?)
Gets the time value.
public bool TryGetTime(out TimeValue? timeValue)
Parameters
timeValueTimeValue?The time value.
Returns
TryGetVector(out VectorValue?)
Gets the vector value.
public bool TryGetVector(out VectorValue? vectorValue)
Parameters
vectorValueVectorValue?The vector value.
Returns
- bool
trueif the current instance contains VectorValue, otherwisefalse.
TryGetVolume(out VolumeValue?)
Gets the volume value.
public bool TryGetVolume(out VolumeValue? volumeValue)
Parameters
volumeValueVolumeValue?The volume value.
Returns
- bool
trueif the current instance contains VolumeValue, otherwisefalse.
Operators
implicit operator Result(bool)
Converts bool to Result.BooleanResult.
public static implicit operator Result(bool boolean)
Parameters
booleanboolThe boolean value.
Returns
- Result
The boolean result.
implicit operator Result(Complex)
Converts Complex to Result.ComplexNumberResult.
public static implicit operator Result(Complex complexNumber)
Parameters
complexNumberComplexThe complex number.
Returns
- Result
The complex number result.
implicit operator Result(string)
Converts string to Result.StringResult.
public static implicit operator Result(string str)
Parameters
strstringThe string.
Returns
- Result
The string result.
implicit operator Result(Lambda)
Converts Lambda to Result.LambdaResult.
public static implicit operator Result(Lambda lambda)
Parameters
lambdaLambdaThe lambda.
Returns
- Result
The lambda result.
implicit operator Result(MatrixValue)
Converts MatrixValue to Result.MatrixResult.
public static implicit operator Result(MatrixValue matrix)
Parameters
matrixMatrixValueThe matrix value.
Returns
- Result
The matrix result.
implicit operator Result(VectorValue)
Converts VectorValue to Result.VectorResult.
public static implicit operator Result(VectorValue vector)
Parameters
vectorVectorValueThe vector value.
Returns
- Result
The vector result.
implicit operator Result(NumberValue)
Converts NumberValue to Result.NumberResult.
public static implicit operator Result(NumberValue numberValue)
Parameters
numberValueNumberValueThe number value.
Returns
- Result
The number result.
implicit operator Result(RationalValue)
Converts RationalValue to Result.RationalResult.
public static implicit operator Result(RationalValue rational)
Parameters
rationalRationalValueThe rational value.
Returns
- Result
The rational result.
implicit operator Result(AngleValue)
Converts AngleValue to Result.AngleResult.
public static implicit operator Result(AngleValue angle)
Parameters
angleAngleValueThe angle value.
Returns
- Result
The angle result.
implicit operator Result(AreaValue)
Converts AreaValue to Result.AreaResult.
public static implicit operator Result(AreaValue area)
Parameters
areaAreaValueThe area value.
Returns
- Result
The area result.
implicit operator Result(LengthValue)
Converts LengthValue to Result.LengthResult.
public static implicit operator Result(LengthValue length)
Parameters
lengthLengthValueThe length value.
Returns
- Result
The length result.
implicit operator Result(MassValue)
Converts MassValue to Result.MassResult.
public static implicit operator Result(MassValue mass)
Parameters
massMassValueThe mass value.
Returns
- Result
The mass result.
implicit operator Result(PowerValue)
Converts PowerValue to Result.PowerResult.
public static implicit operator Result(PowerValue power)
Parameters
powerPowerValueThe power value.
Returns
- Result
The power result.
implicit operator Result(TemperatureValue)
Converts TemperatureValue to Result.TemperatureResult.
public static implicit operator Result(TemperatureValue temperature)
Parameters
temperatureTemperatureValueThe temperature value.
Returns
- Result
The temperature result.
implicit operator Result(TimeValue)
Converts TimeValue to Result.TimeResult.
public static implicit operator Result(TimeValue time)
Parameters
timeTimeValueThe time value.
Returns
- Result
The time result.
implicit operator Result(VolumeValue)
Converts VolumeValue to Result.VolumeResult.
public static implicit operator Result(VolumeValue volume)
Parameters
volumeVolumeValueThe volume value.
Returns
- Result
The volume result.