Class: Kumi::Core::Functions::OverloadResolver
- Inherits:
-
Object
- Object
- Kumi::Core::Functions::OverloadResolver
- Defined in:
- lib/kumi/core/functions/overload_resolver.rb
Overview
OverloadResolver handles type-aware function overload resolution Given a function alias/id and argument types, finds the best matching function
Responsibilities:
-
Track all function overloads per alias
-
Match argument types against parameter constraints
-
Provide clear error messages when resolution fails
Defined Under Namespace
Classes: ResolutionError
Instance Method Summary collapse
-
#exists?(id) ⇒ Boolean
Check if a function exists.
-
#function(id) ⇒ Object
Get function object by ID (already resolved).
-
#initialize(functions_by_id) ⇒ OverloadResolver
constructor
A new instance of OverloadResolver.
-
#resolve(alias_or_id, arg_types) ⇒ String
Resolve a function alias or ID to a specific function ID based on argument types.
Constructor Details
#initialize(functions_by_id) ⇒ OverloadResolver
14 15 16 17 18 |
# File 'lib/kumi/core/functions/overload_resolver.rb', line 14 def initialize(functions_by_id) @functions = functions_by_id # "core.mul" => Function @by_id = functions_by_id # Direct lookup @alias_overloads = build_alias_overloads(functions_by_id) end |
Instance Method Details
#exists?(id) ⇒ Boolean
Check if a function exists
84 85 86 |
# File 'lib/kumi/core/functions/overload_resolver.rb', line 84 def exists?(id) @functions.key?(id.to_s) end |
#function(id) ⇒ Object
Get function object by ID (already resolved)
77 78 79 80 81 |
# File 'lib/kumi/core/functions/overload_resolver.rb', line 77 def function(id) @functions.fetch(id) do raise ResolutionError, "unknown function #{id}" end end |
#resolve(alias_or_id, arg_types) ⇒ String
Resolve a function alias or ID to a specific function ID based on argument types
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/kumi/core/functions/overload_resolver.rb', line 26 def resolve(alias_or_id, arg_types) s = alias_or_id.to_s # If it's already a full function ID, validate arity and type constraints if @functions.key?(s) validate_arity!(s, arg_types) fn = @functions[s] score = match_score(fn.params, arg_types) return s if score > 0 # Type constraints failed raise ResolutionError, "#{alias_or_id}(#{format_types(arg_types)}) - type mismatch" end # Get all candidate overloads for this alias candidates = @alias_overloads[s] raise ResolutionError, "unknown function #{alias_or_id}" if candidates.nil? # Single overload - validate type constraints too if candidates.size == 1 fn_id = candidates.first validate_arity!(fn_id, arg_types) fn = @functions[fn_id] score = match_score(fn.params, arg_types) return fn_id if score > 0 # Type constraints failed for the only overload raise ResolutionError, "#{alias_or_id}(#{format_types(arg_types)}) - type mismatch" end # Multiple overloads - find best match by type constraints (prefer exact matches) candidates_with_scores = candidates.map do |fn_id| fn = @functions[fn_id] score = match_score(fn.params, arg_types) [fn_id, score] end best_match, score = candidates_with_scores.max_by { |_, s| s } return best_match if score > 0 # No match found - provide helpful error raise ResolutionError, "#{alias_or_id}(#{format_types(arg_types)}) - type mismatch" end |