Struct: TResult

template <typename T, typename E>
struct TResult;

Result type that can wrap either T value or E error data.

Useful for return types in functions that can return either valid value or some error. Given implementation tries to match TOptional API as much as possible.

Example

UENUM()
enum class EGuessError : uint8
{
	TooHigh,
	TooLow,
};

USTRUCT()
struct FShiaTheSecretKeeper
{
	GENERATED_BODY()

public:
	FShiaTheSecretKeeper() : Secret(), Password()
	{
	}

	FShiaTheSecretKeeper(FString InSecret, int InPassword) : Secret(InSecret), Password(InPassword)
	{
	}

	TResult<FString, EGuessError> TryGuess(int GuessPassword)
	{
		if (GuessPassword == this->Password)
		{
			return TResult<FString, EGuessError>(this->Secret);
		}
		else if (GuessPassword < this->Password)
		{
			return TResult<FString, EGuessError>(EGuessError::TooLow);
		}
		else
		{
			return TResult<FString, EGuessError>(EGuessError::TooHigh);
		}
	}

private:
	FString Secret = {};
	int Password = {};
};

int Main()
{
	const auto Shia = FShiaTheSecretKeeper("Just do it!", 42);
	const auto Result = Shia.TryGuess(42);
	if (Result.IsOk())
	{
		UE_LOG(LogTemp, Info, TEXT("Guessed! Secret: %s"), *Result.GetOk());
	}
	else
	{
		switch (Result.GetError())
		{
			case EGuessError::TooHigh:
				UE_LOG(LogTemp, Error, TEXT("Too high!"));
				break;

			case EGuessError::TooLow:
				UE_LOG(LogTemp, Error, TEXT("Too low!"));
				break;
		}
	}
}

Methods

  • AsError

    public:
    TOptional<E> AsError() const;
    

    Returns option with cloned error data.

    In case of result wrapping value data, it returns none.

  • AsOk

    public:
    TOptional<T> AsOk() const;
    

    Returns option with cloned value data.

    In case of result wrapping error data, it returns none.

  • GetError

    public:
    E& GetError();
    

    Returns reference to internal error.

    Note

    It panics if result wraps value data.

  • GetError

    public:
    const E& GetError() const;
    

    Returns reference to internal error.

    Note

    It panics if result wraps value data.

  • GetOk

    public:
    T& GetOk();
    

    Returns reference to internal value.

    Note

    It panics if result wraps error data.

  • GetOk

    public:
    const T& GetOk() const;
    

    Returns reference to internal value.

    Note

    It panics if result wraps error data.

  • IsError

    public:
    bool IsError() const;
    

    Tells if result wraps error data.

  • IsOk

    public:
    bool IsOk() const;
    

    Tells if result wraps value data.

  • SetError

    public:
    void SetError(
        const E& Error
    );
    

    Replaces internal data with cloned error data.


    Arguments

    • Error

      const E& Error
      

      Error to copy from.

  • SetError

    public:
    void SetError(
        E&& Error
    );
    

    Replaces internal data with moved error data.


    Arguments

    • Error

      E&& Error
      

      Error to consume.

  • SetOk

    public:
    void SetOk(
        const T& Value
    );
    

    Replaces internal data with cloned value data.


    Arguments

    • Value

      const T& Value
      

      Value to copy from.

  • SetOk

    public:
    void SetOk(
        T&& Value
    );
    

    Replaces internal data with moved value data.


    Arguments

    • Value

      T&& Value
      

      Value to consume.

  • TResult

    public:
    TResult(
        const T& Value
    );
    

    Copy constructor.


    Arguments

    • Value

      const T& Value
      

      Value data to copy from.

  • TResult

    public:
    TResult(
        T&& Value
    );
    

    Move constructor.


    Arguments

    • Value

      T&& Value
      

      Value to consume.

  • TResult

    public:
    TResult(
        const E& Error
    );
    

    Copy constructor.


    Arguments

    • Error

      const E& Error
      

      Error to copy from.

  • TResult

    public:
    TResult(
        E&& Error
    );
    

    Move constructor.


    Arguments

    • Error

      E&& Error
      

      Error to consume.

  • TResult

    public:
    TResult(
        const TResult& Other
    );
    

    Copy constructor.


    Arguments

    • Other

      const TResult& Other
      

      Result to copy from.

  • TResult

    public:
    TResult(
        TResult&& Other
    );
    

    Move constructor.


    Arguments

    • Other

      TResult&& Other
      

      Result to consume.

  • operator bool

    public:
    explicit operator bool() const;
    

    Cast to boolean.

    Handy shortcut for TResult::IsOk. Useful when used in if statement condition.

  • operator!=

    public:
    bool operator!=(
        const TResult& Lhs,
        const TResult& Rhs
    );
    

    Tells if two results aren't equal.

    Two results must both wrap either value or error, and if they do then their data is compared.


    Arguments

    • Lhs

      const TResult& Lhs
      
    • Rhs

      const TResult& Rhs
      
  • operator<<

    public:
    FArchive& operator<<(
        FArchive& Ar,
        TResult& Result
    );
    

    Serializes given result.


    Arguments

    • Ar

      FArchive& Ar
      
    • Result

      TResult& Result
      
  • operator=

    public:
    TResult& operator=(
        const TResult& Other
    );
    

    Copies other result.


    Arguments

    • Other

      const TResult& Other
      

      Result to copy from.

  • operator=

    public:
    TResult& operator=(
        TResult&& Other
    );
    

    Consumes other result.


    Arguments

    • Other

      TResult&& Other
      

      Result to consume.

  • operator=

    public:
    TResult& operator=(
        const T& Value
    );
    

    Replaces internal data with cloned value data.


    Arguments

    • Value

      const T& Value
      

      Value to copy from.

  • operator=

    public:
    TResult& operator=(
        T&& Value
    );
    

    Replaces internal data with moved value data.


    Arguments

    • Value

      T&& Value
      

      Value to consume.

  • operator=

    public:
    TResult& operator=(
        const E& Error
    );
    

    Replaces internal data with cloned error data.


    Arguments

    • Error

      const E& Error
      

      Error to copy from.

  • operator=

    public:
    TResult& operator=(
        E&& Error
    );
    

    Replaces internal data with cloned error data.


    Arguments

    • Error

      E&& Error
      

      Error to consume.

  • operator==

    public:
    bool operator==(
        const TResult& Lhs,
        const TResult& Rhs
    );
    

    Tells if two results are equal.

    Two results must both wrap either value or error, and if they do then their data is compared.


    Arguments

    • Lhs

      const TResult& Lhs
      
    • Rhs

      const TResult& Rhs
      

Documentation built with Unreal-Doc v1.0.8 tool by PsichiX