Sha256: 7462d1dd8c2182221b5e5be4eeb1811279917a081f52f53d051defe5fb9a1c69

Contents?: true

Size: 1.42 KB

Versions: 3

Compression:

Stored size: 1.42 KB

Contents

module Qrb
  #
  # A BuiltinType generator allows capuring an information type to a type of
  # the host language, here a Ruby class. For instance,
  #
  #     Int := BuiltinType(ruby.Fixnum)
  #
  # The set of values captured by the information type is the same set of
  # values that can be represented by the host type. In the example, `Int`
  # captures the same set of numbers as ruby's Fixnum.
  #
  # The ruby class is used as concrete representation of the information type.
  # In the example:
  #
  #     R(Int) = Fixnum
  #
  # Accordingly, the `dress` transformation function has the following signature:
  #
  #     dress :: Alpha  -> Int    throws TypeError
  #     dress :: Object -> Fixnum throws TypeError
  #
  class BuiltinType < Type

    def initialize(ruby_type, name = nil)
      super(name)
      @ruby_type = ruby_type
    end
    attr_reader :ruby_type

    def default_name
      @ruby_type.name.to_s
    end

    def include?(value)
      ruby_type === value
    end

    # Check that `value` is a valid instance of `ruby_type` through `===` or
    # raise an error.
    def dress(value, handler = DressHelper.new)
      handler.failed!(self, value) unless ruby_type===value
      value
    end

    def ==(other)
      return false unless other.is_a?(BuiltinType)
      other.ruby_type==ruby_type
    end
    alias :eql? :==

    def hash
      self.class.hash ^ ruby_type.hash
    end

  end # class BuiltinType
end # module Qrb

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
qrb-0.3.0 lib/qrb/type/builtin_type.rb
qrb-0.2.0 lib/qrb/type/builtin_type.rb
qrb-0.1.0 lib/qrb/type/builtin_type.rb