Class: TypeProf::Core::Type

Inherits:
Object
  • Object
show all
Defined in:
lib/typeprof/core/type.rb

Defined Under Namespace

Classes: Array, Bot, Hash, Instance, Method, Proc, Record, Singleton, Symbol, Var

Class Method Summary collapse

Class Method Details

.collect_hash_value_types(type) ⇒ Object

Returns an array of value type strings from hash-like types



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/typeprof/core/type.rb', line 28

def self.collect_hash_value_types(type)
  case type
  when RBS::Types::Record
    # Extract value types from record fields
    # all_fields returns { key => [type, required] }
    type.all_fields.values.map { |t, _required| t.to_s }
  when RBS::Types::ClassInstance
    # Handle Hash[K, V]
    if type.name.name == :Hash && type.args.size == 2
      [type.args[1].to_s]
    else
      [type.to_s]
    end
  when RBS::Types::Union
    # Handle union of types - extract value types from all hash-like types
    type.types.flat_map { |t| collect_hash_value_types(t) }
  else
    [type.to_s]
  end
end

.default_param_map(genv, ty) ⇒ Object



49
50
51
52
53
54
55
56
57
58
# File 'lib/typeprof/core/type.rb', line 49

def self.default_param_map(genv, ty)
  ty = ty.base_type(genv)
  instance_ty = ty.is_a?(Type::Instance) ? ty : Type::Instance.new(genv, ty.mod, []) # TODO: type params
  singleton_ty = ty.is_a?(Type::Instance) ? Type::Singleton.new(genv, ty.mod) : ty
  {
    "*self": Source.new(ty),
    "*instance": Source.new(instance_ty),
    "*class": Source.new(singleton_ty),
  }
end

.extract_hash_value_type(s) ⇒ Object



18
19
20
21
22
23
24
25
# File 'lib/typeprof/core/type.rb', line 18

def self.extract_hash_value_type(s)
  begin
    type = RBS::Parser.parse_type(s)
    collect_hash_value_types(type).uniq.join(" | ")
  rescue
    s
  end
end

.new(genv, *args) ⇒ Object

This new method does memoize creation of types : (GlobalEnv, *untyped) -> instance



5
6
7
# File 'lib/typeprof/core/type.rb', line 5

def self.new(genv, *args)
  genv.type_table[[self] + args] ||= super(genv, *args)
end

.strip_array(s) ⇒ Object



14
15
16
# File 'lib/typeprof/core/type.rb', line 14

def self.strip_array(s)
  s.start_with?("Array[") && s.end_with?("]") ? s[6..-2] || raise : s
end

.strip_parens(s) ⇒ Object



9
10
11
12
# File 'lib/typeprof/core/type.rb', line 9

def self.strip_parens(s)
  #s =~ /\A\((.*)\)\z/ ? $1 : s
  s.start_with?("(") && s.end_with?(")") ? s[1..-2] || raise : s
end