Sha256: 679c7879c867bf9f3d3c065c9fdbfc03bf9c8b35108f2d82366df43804c0861b

Contents?: true

Size: 1.45 KB

Versions: 130

Compression:

Stored size: 1.45 KB

Contents

# frozen_string_literal: true
# typed: true

class T::InexactStruct
  include T::Props
  include T::Props::Serializable
  include T::Props::Constructor
end

class T::Struct < T::InexactStruct
  def self.inherited(subclass)
    super(subclass)
    T::Private::ClassUtils.replace_method(subclass.singleton_class, :inherited, true) do |s|
      super(s)
      raise "#{self.name} is a subclass of T::Struct and cannot be subclassed"
    end
  end
end

class T::ImmutableStruct < T::InexactStruct
  extend T::Sig

  def self.inherited(subclass)
    super(subclass)

    T::Private::ClassUtils.replace_method(subclass.singleton_class, :inherited, true) do |s|
      super(s)
      raise "#{self.name} is a subclass of T::ImmutableStruct and cannot be subclassed"
    end
  end

  # Matches the one in WeakConstructor, but freezes the object
  sig {params(hash: T::Hash[Symbol, T.untyped]).void.checked(:never)}
  def initialize(hash={})
    super

    freeze
  end

  # Matches the signature in Props, but raises since this is an immutable struct and only const is allowed
  sig {params(name: Symbol, cls: T.untyped, rules: T.untyped).void}
  def self.prop(name, cls, **rules)
    return super if (cls.is_a?(Hash) && cls[:immutable]) || rules[:immutable]

    raise "Cannot use `prop` in #{self.name} because it is an immutable struct. Use `const` instead"
  end

  def with(changed_props)
    raise "Cannot use `with` in #{self.class.name} because it is an immutable struct"
  end
end

Version data entries

130 entries across 130 versions & 1 rubygems

Version Path
sorbet-runtime-0.5.11834 lib/types/struct.rb
sorbet-runtime-0.5.11829 lib/types/struct.rb
sorbet-runtime-0.5.11826 lib/types/struct.rb
sorbet-runtime-0.5.11823 lib/types/struct.rb
sorbet-runtime-0.5.11820 lib/types/struct.rb
sorbet-runtime-0.5.11813 lib/types/struct.rb
sorbet-runtime-0.5.11812 lib/types/struct.rb
sorbet-runtime-0.5.11810 lib/types/struct.rb
sorbet-runtime-0.5.11805 lib/types/struct.rb
sorbet-runtime-0.5.11802 lib/types/struct.rb
sorbet-runtime-0.5.11801 lib/types/struct.rb
sorbet-runtime-0.5.11798 lib/types/struct.rb
sorbet-runtime-0.5.11796 lib/types/struct.rb
sorbet-runtime-0.5.11789 lib/types/struct.rb
sorbet-runtime-0.5.11787 lib/types/struct.rb
sorbet-runtime-0.5.11784 lib/types/struct.rb
sorbet-runtime-0.5.11781 lib/types/struct.rb
sorbet-runtime-0.5.11780 lib/types/struct.rb
sorbet-runtime-0.5.11778 lib/types/struct.rb
sorbet-runtime-0.5.11770 lib/types/struct.rb