Sha256: a3a4e0211423af6dc098d7631c18f50f819badb85a12f0d7b7d8fc63d7d6de69

Contents?: true

Size: 1.3 KB

Versions: 1

Compression:

Stored size: 1.3 KB

Contents

# typed: strict
# frozen_string_literal: true

module Typed
  # Represents a successful result. Contains a payload and no error information.
  class Success < Result
    extend T::Sig
    extend T::Generic

    Payload = type_member
    Error = type_member { { fixed: T.noreturn } }

    sig { override.returns(Payload) }
    attr_reader :payload

    sig do
      type_parameters(:T)
        .params(payload: T.type_parameter(:T))
        .returns(Typed::Success[T.type_parameter(:T)])
    end
    def self.new(payload)
      super(payload)
    end

    sig { returns(Typed::Success[NilClass]) }
    def self.blank
      new(nil)
    end

    sig { params(payload: Payload).void }
    def initialize(payload)
      @payload = payload
      super()
    end

    sig { override.returns(T::Boolean) }
    def success?
      true
    end

    sig { override.returns(T::Boolean) }
    def failure?
      false
    end

    sig { override.returns(T.noreturn) }
    def error
      raise NoErrorOnSuccessError
    end

    sig do
      override
        .type_parameters(:U, :T)
        .params(block: T.proc.params(arg0: Payload).returns(Result[T.type_parameter(:U), T.type_parameter(:T)]))
        .returns(Result[T.type_parameter(:U), T.type_parameter(:T)])
    end
    def and_then(&block)
      block.call(payload)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
sorbet-result-0.3.0 lib/typed/success.rb