Pidgin

Class Result<TToken, T>

Represents the result of parsing. A parse result may be successful (Success == true), in which case it contains a value, or it may be a failure, in which case it contains an error.

Inheritance
Declaration
public class Result<TToken, T> : Object
Type Parameters
Name Description

TToken

The type of the tokens in the parser's input stream.

T

The type of the value returned by the parser.

Properties

Error

The parse error.

Declaration
public ParseError<TToken> Error { get; }
Property Value
Type Description

ParseError<TToken>

Exceptions
Type Condition

InvalidOperationException

Thrown when the result was a successful one.

Success

Did the parse succeed?.

Declaration
public bool Success { get; }
Property Value
Type Description

Boolean

A value indicating whether the parse was successful.

Value

The parser's return value.

Declaration
public T Value { get; }
Property Value
Type Description

T

Exceptions
Type Condition

InvalidOperationException

Thrown when the result was not a successful one.

Methods

Cast<U>()

Cast the value contained in the result to the specified output type.

Declaration
public Result<TToken, U> Cast<U>()
Returns
Type Description

Result<TToken, U>

A result containing this result's value casted to U.

Type Parameters
Name Description

U

The type to cast the contained value to.

Exceptions
Type Condition

InvalidCastException

Thrown when the contained value is not an instance of U.

GetValueOrDefault()

Get the value, or return a default value.

Declaration
public T GetValueOrDefault()
Returns
Type Description

T

The value if Success is true, or a default value.

GetValueOrDefault(T)

Get the value, or return the specified default value.

Declaration
public T GetValueOrDefault(T default)
Parameters
Type Name Description

T

default

The default value.

Returns
Type Description

T

The value if Success is true, or the specified default value.

GetValueOrDefault(Func<T>)

Get the value, or return the result of calling the specified function.

Declaration
public T GetValueOrDefault(Func<T> value)
Parameters
Type Name Description

Func<T>

value

A function which computes a default value.

Returns
Type Description

T

The value if Success is true, or the result of calling the specified function.

Match<U>(Func<T, U>, Func<ParseError<TToken>, U>)

Tear down this parse result using a function for the two possible outcomes. If Success == true, success will be called. Otherwise, failure will be called.

Declaration
public U Match<U>(Func<T, U> success, Func<ParseError<TToken>, U> failure)
Parameters
Type Name Description

Func<T, U>

success

Called when the result has a value.

Func<ParseError<TToken>, U>

failure

Called when the result does not have a value.

Returns
Type Description

U

The result of calling the success or failure function.

Type Parameters
Name Description

U

The return type.

Or(Result<TToken, T>)

Choose the first successful result.

Declaration
public Result<TToken, T> Or(Result<TToken, T> result)
Parameters
Type Name Description

Result<TToken, T>

result

A fallback result if this one has an error.

Returns
Type Description

Result<TToken, T>

This result, if Success == true, or result.

Or(Func<Result<TToken, T>>)

Choose the first successful result.

Declaration
public Result<TToken, T> Or(Func<Result<TToken, T>> result)
Parameters
Type Name Description

Func<Result<TToken, T>>

result

A fallback result if this one has an error.

Returns
Type Description

Result<TToken, T>

This result, if Success == true, or the result of calling result.

Select<U>(Func<T, U>)

Project the value contained in the result.

Declaration
public Result<TToken, U> Select<U>(Func<T, U> selector)
Parameters
Type Name Description

Func<T, U>

selector

A transformation function to apply to the contained value.

Returns
Type Description

Result<TToken, U>

The result of applying the transformation function to the contained value.

Type Parameters
Name Description

U

The type of the resulting value.

SelectMany<U>(Func<T, Result<TToken, U>>)

Projects the value of the result into a result, and flattens the resulting value into a single result.

Declaration
public Result<TToken, U> SelectMany<U>(Func<T, Result<TToken, U>> selector)
Parameters
Type Name Description

Func<T, Result<TToken, U>>

selector

A transformation function to apply to the contained value.

Returns
Type Description

Result<TToken, U>

The final result.

Type Parameters
Name Description

U

The type of the resulting possibly-absent value.

SelectMany<U, R>(Func<T, Result<TToken, U>>, Func<T, U, R>)

Projects the value of the result into a result, and flattens the resulting value into a single result, applying a result selector function to the two values.

Declaration
public Result<TToken, R> SelectMany<U, R>(Func<T, Result<TToken, U>> selector, Func<T, U, R> result)
Parameters
Type Name Description

Func<T, Result<TToken, U>>

selector

A transformation function to apply to the contained value.

Func<T, U, R>

result

A transformation function to apply to the contained value and the value contained in the selected Maybe<T>.

Returns
Type Description

Result<TToken, R>

The result of applying selector to the contained value and result to the intermediate values.

Type Parameters
Name Description

U

The type of the value to select.

R

The type of the resulting possibly-absent value.