Skip to content
This repository was archived by the owner on Sep 28, 2022. It is now read-only.
This repository was archived by the owner on Sep 28, 2022. It is now read-only.

Create a proxy object for ambiguous method references #576

@yloiseau

Description

@yloiseau

We can't currently fully use the function reference literal notation or the Predefined::fun function on an overloaded function. This feature is only usable if the overloaded version don't have the same number of parameters, regardless of the type.
For instance, the given code:

let f = ^java.lang.Character::toLowerCase

raises a org.eclipse.golo.runtime.AmbiguousFunctionReferenceException, since two versions exists: with a char argument and with an int one.
This limits some functional style programming aspects (e.g. HOF, point-free, etc.)
The current workaround is to use a lambda |c| -> java.lang.Character.toLowerCase(c), since in that case, the resolution is done at call time using the actual arguments. A macro could be used to create such lambda directly, but it has several drawbacks:

  • more verbose to use,
  • introduce overhead if not needed, and thus shouldn't be applied systematically,
  • fixes the arity (this could be fixed by using a polyadic lambda with a test on the actual number or arguments, but become ambiguous when the referenced function itself is polyadic).

The idea, as suggested by @artpej in #326 (comment), is to use a proxy object for ambiguous method references, allowing to resolve at call time.
Such object can include an inline cache and be replaced at the call site by the real reference when known.
This would also allow more polymorphic behaviour, since with could dispatch on function with different arities.
It may be a step toward lisp-like multimethods 😉

See also #326, #341

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions