struct

PointClickEngine::Core::Result(T, E)

Inherits Struct < Value < Object

Result type for operations that can succeed or fail

This provides a type-safe way to handle operations that might fail without relying on exceptions or nil returns. Inspired by Rust's Result type and functional programming error handling patterns.

Usage

def load_texture(path : String) : Result(Texture, AssetError)
  if File.exists?(path)
    texture = RL.load_texture(path)
    Result(T, E).success(texture)
  else
    Result(T, E).failure(AssetError.new("File not found", path))
  end
end

result = load_texture("sprite.png")
case result
when .success?
  texture = result.value
  # Use texture
when .failure?
  puts "Error: #{result.error.message}"
end

Constructors

new(value : T | Nil, error : E | Nil, success : Bool)
Source

Class methods

failure(error : E) : Result(T, E) forall T, E

Create a failed result

Source
success(value : T) : Result(T, E) forall T, E

Create a successful result

Source

Instance methods

and_then

Chain operations that return Results

Source
error

Get the error (raises if success)

Source
failure?

Check if the result is a failure

Source
map

Transform the success value if present

Source
map_error

Transform the error if present

Source
success?

Check if the result is successful

Source
value

Get the success value (raises if failure)

Source
value_or(default : T) : T

Get the success value or a default

Source