Skip to content

Function values in a variable have one type #318

Open

Description

@timfennis

A function value stored in a variable has one type, like any variable, so calling it with different argument types is an error:

let id = fn(x) => x;
id(1);
id("a");   // error: id holds a function that doesn't accept String

let f = len;
f("abc");
f([1]);    // error: f holds a function that doesn't accept List<Int>

An overloaded name used as a value is picked by the type expected where it's used (#246, #217), so a list of overloaded functions of different types can't be written either: [len, abs] has to say which abs is meant.

Today, declaring the function with fn (top-level fns are generic) or annotating the variable Any works around it.

Possible directions:

  • Generic let lambdas: a let lambda that's never reassigned is generic like a top-level fn (ML-style let-generalisation), reusing the generic-function instances.
  • Overload sets as values: a value that keeps all of a name's overloads and picks one at each call, statically where the call's types are known. This would also let [len, abs] and let f = len work, but needs a new kind of value in the VM and the type system, and an answer to what xs.map(f) passes when f is a set.

Found in the review of the type checker (#291).

🤖 Generated with Claude Code

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions