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
result
objectThe result of expression evaluation.
Returns
- Result
The result.
Exceptions
- InvalidResultException
The type of
result
is not supported.
TryGetAngle(out AngleValue?)
Gets the angle value.
public bool TryGetAngle(out AngleValue? angleValue)
Parameters
angleValue
AngleValue?The angle value.
Returns
- bool
true
if the current instance contains AngleValue, otherwisefalse
.
TryGetArea(out AreaValue?)
Gets the area value.
public bool TryGetArea(out AreaValue? areaValue)
Parameters
areaValue
AreaValue?The area value.
Returns
TryGetBoolean(out bool?)
Gets the boolean value.
public bool TryGetBoolean(out bool? boolValue)
Parameters
boolValue
bool?The boolean value.
Returns
TryGetComplexNumber(out Complex?)
Gets the complex number.
public bool TryGetComplexNumber(out Complex? complexNumberValue)
Parameters
complexNumberValue
Complex?The complex number.
Returns
TryGetLambda(out Lambda?)
Gets the lambda.
public bool TryGetLambda(out Lambda? lambda)
Parameters
lambda
Lambda?The lambda.
Returns
TryGetLength(out LengthValue?)
Gets the length value.
public bool TryGetLength(out LengthValue? lengthValue)
Parameters
lengthValue
LengthValue?The length value.
Returns
- bool
true
if the current instance contains LengthValue, otherwisefalse
.
TryGetMass(out MassValue?)
Gets the mass value.
public bool TryGetMass(out MassValue? massValue)
Parameters
massValue
MassValue?The mass value.
Returns
TryGetMatrix(out MatrixValue?)
Gets the matrix value.
public bool TryGetMatrix(out MatrixValue? matrixValue)
Parameters
matrixValue
MatrixValue?The matrix value.
Returns
- bool
true
if the current instance contains MatrixValue, otherwisefalse
.
TryGetNumber(out NumberValue?)
Gets the number value.
public bool TryGetNumber(out NumberValue? number)
Parameters
number
NumberValue?The number value.
Returns
- bool
true
if the current instance contains NumberValue, otherwisefalse
.
TryGetPower(out PowerValue?)
Gets the power value.
public bool TryGetPower(out PowerValue? powerValue)
Parameters
powerValue
PowerValue?The power value.
Returns
- bool
true
if the current instance contains PowerValue, otherwisefalse
.
TryGetRational(out RationalValue?)
Gets the rational value.
public bool TryGetRational(out RationalValue? rationalValue)
Parameters
rationalValue
RationalValue?The rational value.
Returns
- bool
true
if the current instance contains RationalValue, otherwisefalse
.
TryGetString(out string?)
Gets the string.
public bool TryGetString(out string? stringValue)
Parameters
stringValue
stringThe string value.
Returns
TryGetTemperature(out TemperatureValue?)
Gets the temperature value.
public bool TryGetTemperature(out TemperatureValue? temperatureValue)
Parameters
temperatureValue
TemperatureValue?The temperature value.
Returns
- bool
true
if the current instance contains TemperatureValue, otherwisefalse
.
TryGetTime(out TimeValue?)
Gets the time value.
public bool TryGetTime(out TimeValue? timeValue)
Parameters
timeValue
TimeValue?The time value.
Returns
TryGetVector(out VectorValue?)
Gets the vector value.
public bool TryGetVector(out VectorValue? vectorValue)
Parameters
vectorValue
VectorValue?The vector value.
Returns
- bool
true
if the current instance contains VectorValue, otherwisefalse
.
TryGetVolume(out VolumeValue?)
Gets the volume value.
public bool TryGetVolume(out VolumeValue? volumeValue)
Parameters
volumeValue
VolumeValue?The volume value.
Returns
- bool
true
if the current instance contains VolumeValue, otherwisefalse
.
Operators
implicit operator Result(bool)
Converts bool to Result.BooleanResult.
public static implicit operator Result(bool boolean)
Parameters
boolean
boolThe boolean value.
Returns
- Result
The boolean result.
implicit operator Result(Complex)
Converts Complex to Result.ComplexNumberResult.
public static implicit operator Result(Complex complexNumber)
Parameters
complexNumber
ComplexThe 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
str
stringThe string.
Returns
- Result
The string result.
implicit operator Result(Lambda)
Converts Lambda to Result.LambdaResult.
public static implicit operator Result(Lambda lambda)
Parameters
lambda
LambdaThe lambda.
Returns
- Result
The lambda result.
implicit operator Result(MatrixValue)
Converts MatrixValue to Result.MatrixResult.
public static implicit operator Result(MatrixValue matrix)
Parameters
matrix
MatrixValueThe matrix value.
Returns
- Result
The matrix result.
implicit operator Result(VectorValue)
Converts VectorValue to Result.VectorResult.
public static implicit operator Result(VectorValue vector)
Parameters
vector
VectorValueThe vector value.
Returns
- Result
The vector result.
implicit operator Result(NumberValue)
Converts NumberValue to Result.NumberResult.
public static implicit operator Result(NumberValue numberValue)
Parameters
numberValue
NumberValueThe number value.
Returns
- Result
The number result.
implicit operator Result(RationalValue)
Converts RationalValue to Result.RationalResult.
public static implicit operator Result(RationalValue rational)
Parameters
rational
RationalValueThe rational value.
Returns
- Result
The rational result.
implicit operator Result(AngleValue)
Converts AngleValue to Result.AngleResult.
public static implicit operator Result(AngleValue angle)
Parameters
angle
AngleValueThe angle value.
Returns
- Result
The angle result.
implicit operator Result(AreaValue)
Converts AreaValue to Result.AreaResult.
public static implicit operator Result(AreaValue area)
Parameters
area
AreaValueThe area value.
Returns
- Result
The area result.
implicit operator Result(LengthValue)
Converts LengthValue to Result.LengthResult.
public static implicit operator Result(LengthValue length)
Parameters
length
LengthValueThe length value.
Returns
- Result
The length result.
implicit operator Result(MassValue)
Converts MassValue to Result.MassResult.
public static implicit operator Result(MassValue mass)
Parameters
mass
MassValueThe mass value.
Returns
- Result
The mass result.
implicit operator Result(PowerValue)
Converts PowerValue to Result.PowerResult.
public static implicit operator Result(PowerValue power)
Parameters
power
PowerValueThe power value.
Returns
- Result
The power result.
implicit operator Result(TemperatureValue)
Converts TemperatureValue to Result.TemperatureResult.
public static implicit operator Result(TemperatureValue temperature)
Parameters
temperature
TemperatureValueThe temperature value.
Returns
- Result
The temperature result.
implicit operator Result(TimeValue)
Converts TimeValue to Result.TimeResult.
public static implicit operator Result(TimeValue time)
Parameters
time
TimeValueThe time value.
Returns
- Result
The time result.
implicit operator Result(VolumeValue)
Converts VolumeValue to Result.VolumeResult.
public static implicit operator Result(VolumeValue volume)
Parameters
volume
VolumeValueThe volume value.
Returns
- Result
The volume result.