Class: Steep::Interface::Function

Inherits:
Object
  • Object
show all
Defined in:
lib/steep/interface/function.rb

Defined Under Namespace

Classes: Params

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params:, return_type:, location:) ⇒ Function

Returns a new instance of Function.



992
993
994
995
996
# File 'lib/steep/interface/function.rb', line 992

def initialize(params:, return_type:, location:)
  @params = params
  @return_type = return_type
  @location = location
end

Instance Attribute Details

#locationObject (readonly)

Returns the value of attribute location.



990
991
992
# File 'lib/steep/interface/function.rb', line 990

def location
  @location
end

#paramsObject (readonly)

Returns the value of attribute params.



988
989
990
# File 'lib/steep/interface/function.rb', line 988

def params
  @params
end

#return_typeObject (readonly)

Returns the value of attribute return_type.



989
990
991
# File 'lib/steep/interface/function.rb', line 989

def return_type
  @return_type
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



998
999
1000
# File 'lib/steep/interface/function.rb', line 998

def ==(other)
  other.is_a?(Function) && other.params == params && other.return_type == return_type
end

#accept_one_arg?Boolean

Returns:

  • (Boolean)


1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
# File 'lib/steep/interface/function.rb', line 1060

def accept_one_arg?
  return false unless params
  return false unless params.keyword_params.requireds.empty?
  head = params.positional_params or return false

  case head.head
  when Params::PositionalParams::Required
    !head.tail.is_a?(Params::PositionalParams::Required)
  else
    true
  end
end

#closed?Boolean

Returns:

  • (Boolean)


1081
1082
1083
1084
1085
1086
1087
# File 'lib/steep/interface/function.rb', line 1081

def closed?
  if params
    params.closed? && return_type.free_variables.empty?
  else
    return_type.free_variables.empty?
  end
end

#each_type(&block) ⇒ Object Also known as: each_child



1033
1034
1035
1036
1037
1038
1039
1040
# File 'lib/steep/interface/function.rb', line 1033

def each_type(&block)
  if block
    params&.each_type(&block)
    yield return_type
  else
    enum_for :each_type
  end
end

#free_variablesObject



1008
1009
1010
1011
1012
1013
1014
# File 'lib/steep/interface/function.rb', line 1008

def free_variables
  @fvs ||= Set[].tap do |fvs|
    # @type var fvs: Set[AST::Types::variable]
    fvs.merge(params.free_variables) if params
    fvs.merge(return_type.free_variables)
  end
end

#hashObject



1004
1005
1006
# File 'lib/steep/interface/function.rb', line 1004

def hash
  self.class.hash ^ params.hash ^ return_type.hash
end

#map_type(&block) ⇒ Object



1044
1045
1046
1047
1048
1049
1050
# File 'lib/steep/interface/function.rb', line 1044

def map_type(&block)
  Function.new(
    params: params&.map_type(&block),
    return_type: yield(return_type),
    location: location
  )
end

#subst(s) ⇒ Object



1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
# File 'lib/steep/interface/function.rb', line 1016

def subst(s)
  return self if s.empty?

  ps = params.subst(s) if params
  ret = return_type.subst(s)

  if ps == params && ret == return_type
    self
  else
    Function.new(
      params: ps,
      return_type: ret,
      location: location
    )
  end
end

#to_sObject



1073
1074
1075
1076
1077
1078
1079
# File 'lib/steep/interface/function.rb', line 1073

def to_s
  if params
    "#{params} -> #{return_type}"
  else
    "(?) -> #{return_type}"
  end
end

#with(params: self.params, return_type: self.return_type) ⇒ Object



1052
1053
1054
1055
1056
1057
1058
# File 'lib/steep/interface/function.rb', line 1052

def with(params: self.params, return_type: self.return_type)
  Function.new(
    params: params,
    return_type: return_type,
    location: location
  )
end