Sha256: c5da280c7910b86a89e7f204e1b606d80b6f0701293acd330062ed0451d5453b

Contents?: true

Size: 1.56 KB

Versions: 20

Compression:

Stored size: 1.56 KB

Contents

module Steep
  module Interface
    class Block
      attr_reader :type
      attr_reader :optional

      def initialize(type:, optional:)
        @type = type
        @optional = optional
      end

      def optional?
        @optional
      end

      def required?
        !optional?
      end

      def to_optional
        self.class.new(
          type: type,
          optional: true
        )
      end

      def ==(other)
        other.is_a?(self.class) && other.type == type && other.optional == optional
      end

      alias eql? ==

      def hash
        type.hash ^ optional.hash
      end

      def closed?
        type.closed?
      end

      def subst(s)
        ty = type.subst(s)
        if ty == type
          self
        else
          self.class.new(
            type: ty,
            optional: optional
          )
        end
      end

      def free_variables()
        @fvs ||= type.free_variables
      end

      def to_s
        "#{optional? ? "?" : ""}{ #{type.params} -> #{type.return_type} }"
      end

      def map_type(&block)
        self.class.new(
          type: type.map_type(&block),
          optional: optional
        )
      end

      def +(other)
        optional = self.optional? || other.optional?
        type = Function.new(
          params: self.type.params + other.type.params,
          return_type: AST::Types::Union.build(types: [self.type.return_type, other.type.return_type]),
          location: nil
        )

        self.class.new(
          type: type,
          optional: optional
        )
      end
    end
  end
end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
steep-1.1.1 lib/steep/interface/block.rb
steep-1.1.0 lib/steep/interface/block.rb
steep-1.1.0.pre.1 lib/steep/interface/block.rb
steep-1.0.2 lib/steep/interface/block.rb
steep-1.0.1 lib/steep/interface/block.rb
steep-1.0.0 lib/steep/interface/block.rb
steep-0.52.2 lib/steep/interface/block.rb
steep-0.52.1 lib/steep/interface/block.rb
steep-0.52.0 lib/steep/interface/block.rb
steep-0.51.0 lib/steep/interface/block.rb
steep-0.50.0 lib/steep/interface/block.rb
steep-0.49.1 lib/steep/interface/block.rb
steep-0.49.0 lib/steep/interface/block.rb
steep-0.48.0 lib/steep/interface/block.rb
steep-0.47.1 lib/steep/interface/block.rb
steep-0.47.0 lib/steep/interface/block.rb
steep-0.46.0 lib/steep/interface/block.rb
steep-0.45.0 lib/steep/interface/block.rb
steep-0.44.1 lib/steep/interface/block.rb
steep-0.44.0 lib/steep/interface/block.rb