class Carbon::Concrete::Type::Function

The function part of a type. This comes after the module that it is defined in, and contains three pieces of information: the name of the function, the generics associated with the function, and the parameters of the function.

@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.

@note

This class does not include comparison methods; however, if they
were to ever be needed, {#to_s} should be used for the basis of
comparison.

Attributes

generics[R]

The generics used in the function definition.

@api public @example

type = Carbon::Type("A.+<T>(A, T)")
func = type.function
func.generics
  # => [#<Carbon::Concrete::Type::Generic T>]

@return [::Array<Type>]

name[R]

The name of the function. This is normally a bland string. Functions can have a variety of names to override default operations in the language; even more so than ruby.

@api public @example

type = Carbon::Type("A.+(A, B)")
func = type.function
func.name # => "+"

@return [::String] The name of the function.

parameters[R]

The parameters for the function. These are the arguments that are passed to the function when it's called. This is included in the definition for the name of the function to allow overloading.

@api public @example

type = Carbon::Type("A.+(A, B)")
func = type.function
func.parameters
  # => [#<Carbon::Concrete::Type A>,
  #   #<Carbon::Concrete::Type B>]

@return [::Array<Type>]

Public Class Methods

new(name, params, generics = []) click to toggle source

Initialize the function.

@see name @see params @param name [::String] The name of the function. @param params [::Array<Type>] The parameters of the function.

This is frozen before stored.
# File lib/carbon/concrete/type/function.rb, line 65
def initialize(name, params, generics = [])
  @name = name.to_s
  @parameters = params
  @generics = generics
  deep_freeze!
end

Public Instance Methods

intern() click to toggle source

Returns the interned version of this function. In contrast to {#to_s}, this does not include generic information.

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

names.

@example

func.name # => "+"
func.parameters # => [#<Carbon::Concrete::Type::Name Test<T>>]
func.intern # => "+(Test)"

@return [::String]

# File lib/carbon/concrete/type/function.rb, line 101
def intern
  "#{@name}(#{@parameters.map(&:intern).join(', ')})".freeze
end
to_s() click to toggle source

Returns a string representation of this function. In contrast to {#intern}, this includes generic information.

@api public @example

func.name # => "+"
func.parameters # => [#<Carbon::Concrete::Type::Name Test<T>>]
func.to_s # => "+(Test<T>)"

@return [::String]

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