Sha256: 4b0159d1a3683d59d966ba93919bcd7622b7641b5eef671643f8755b087461c4

Contents?: true

Size: 1.43 KB

Versions: 2

Compression:

Stored size: 1.43 KB

Contents

# encoding: utf-8

module Mixture
  module Types
    # Used by certain types to create subtypes of those types.  This
    # is useful in collections and hashes, wherein the collection
    # members and hash keys/values all have types as well (and need
    # to be coerced to them).
    module Access
      # Creates a subtype with the given member types.  Any number of
      # subtypes may be used.  If a class hasn't been created with the
      # subtypes, it creates a new one.
      #
      # @see #create
      # @param subs [Object] The subtypes to use.
      # @return [Class] The new subtype.
      def [](*subs)
        options[:types].fetch([self, subs]) do
          create(subs)
        end
      end

      private

      # Actually creates the subtype.  This should never be called
      # outside of {.[]}.  If `:noinfer` is set in the supertype's
      # options, it doesn't infer the type of each subtype; otherwise,
      # it does.
      #
      # @param subs [Array<Object>] The subtypes.
      # @return [Class] The new subtype.
      def create(subs)
        subtype = ::Class.new(self)
        members = if options[:noinfer]
                    subs
                  else
                    subs.map { |sub| Types.infer(sub) }
                  end
        name    = "#{inspect}[#{members.join(', ')}]"
        subtype.options.merge!(members: members, name: name)
        options[:types][[self, subs]] = subtype
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
mixture-0.3.1 lib/mixture/types/access.rb
mixture-0.3.0 lib/mixture/types/access.rb