Class: Kumi::Core::Functions::OverloadResolver

Inherits:
Object
  • Object
show all
Defined in:
lib/kumi/core/functions/overload_resolver.rb

Overview

Type-aware function overload resolution. Given a function alias/id and the inferred argument types, picks the overload whose parameter constraints best match. All constraint matching goes through Types::System, so the set of accepted kinds (and any per-target policy) lives in one place.

Defined Under Namespace

Classes: ResolutionError

Instance Method Summary collapse

Constructor Details

#initialize(functions_by_id, type_system: Kumi::Core::Types::System.default) ⇒ OverloadResolver

Returns a new instance of OverloadResolver.



11
12
13
14
15
# File 'lib/kumi/core/functions/overload_resolver.rb', line 11

def initialize(functions_by_id, type_system: Kumi::Core::Types::System.default)
  @functions = functions_by_id
  @alias_overloads = build_alias_overloads(functions_by_id)
  @types = type_system
end

Instance Method Details

#exists?(id) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/kumi/core/functions/overload_resolver.rb', line 36

def exists?(id)
  @functions.key?(id.to_s)
end

#function(id) ⇒ Object



32
33
34
# File 'lib/kumi/core/functions/overload_resolver.rb', line 32

def function(id)
  @functions.fetch(id) { raise ResolutionError, "unknown function #{id}" }
end

#resolve(alias_or_id, arg_types) ⇒ Object

Resolve an alias or id to a concrete function id based on arg types. Raises ResolutionError (with a precise, argument-level message) on an arity or type mismatch.

Raises:



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/kumi/core/functions/overload_resolver.rb', line 20

def resolve(alias_or_id, arg_types)
  id = alias_or_id.to_s
  return resolve_single(alias_or_id, id, arg_types) if @functions.key?(id)

  candidates = @alias_overloads[id]
  raise ResolutionError, "unknown function #{alias_or_id}" if candidates.nil?

  return resolve_single(alias_or_id, candidates.first, arg_types) if candidates.size == 1

  resolve_overloaded(alias_or_id, candidates, arg_types)
end