class Carbon::Concrete::Type::Part

A single part of the type. This only contains the name of the part, and the generics for the part. For most parts, the generics are ignored (or even stripped; see {#strip}).

@note

**This class is frozen upon initialization.**  This means that any
attempt to modify it will result in an error.  In most cases, the
attributes on this class will also be frozen, as well.

Attributes

generics[R]

The generics of the part. This is normally an empty array unless it is both the last part of a name, and contain generic information from the original source text.

@api public @example

type = Carbon::Type("Carbon::Pointer<T>")
type.name.first.generics # => []
type.name.last.generics # => [#<Carbon::Concrete::Type::Generic T>]

@return [<Type::Generic>] The generics.

location[R]

The location of the type. This is used for interfacing with other programs that require a location of some sort.

@api semiprivate @return [Object]

value[R]

The value of the part. This is normally the lexeme or the string associated with the part.

@api public @example

type = Carbon::Type("Carbon::Pointer<T>")
part = type.name.last
part.value # => "Pointer"

@return [::String] The value.

Public Class Methods

new(value, generics, location: nil) click to toggle source

Initialize the part with the given value and generics.

@see value @see generics @param value [::String] The value. @param generics [<Type::Generic>] The generics.

# File lib/carbon/concrete/type/part.rb, line 52
def initialize(value, generics, location: nil)
  @value = value
  @generics = generics
  @location = location
  deep_freeze!
end

Public Instance Methods

intern() click to toggle source

Returns the interned value of this part. This contains no generic information, and as such, is the same as the value of the part.

@api public @example

type = Carbon::Type("Carbon::Pointer<T>")
type.name.last.intern # => "Pointer"

@note See {Item::Base#intern} For more information about interned

names.

@return [::String] The interned value of this part.

# File lib/carbon/concrete/type/part.rb, line 85
def intern
  value
end
strip() click to toggle source

Strips the part. This returns a new part with all of the generic information removed. During parsing, this is applied to all parts except the last part of the name, since the generics for the parts except the last are ignored.

@api public @example

type = Carbon::Type("Carbon::Pointer<T>")
part = type.name.last
part.generics.map(&:to_s) # => ["T"]
part.strip.generics.map(&:to_s) # => []

@return [Part] The new part.

# File lib/carbon/concrete/type/part.rb, line 71
def strip
  Part.new(value, [])
end
to_s() click to toggle source

Returns a string representation of this part. This contains all of the generic information, if it exists; otherwise, it is the same as {#intern}.

@api public @example

type = Carbon::Type("Carbon::Pointer<T>")
type.name.last.to_s # => "Pointer<T>"

@return [::String] The string representation of the part.

# File lib/carbon/concrete/type/part.rb, line 98
def to_s
  if @generics.any?
    "#{@value}<#{@generics.map(&:to_s).join(', ')}>".freeze
  else
    @value
  end
end