Skip to content

Contextual typing / target type inference #1062

@mtdowling

Description

@mtdowling

When designing callback-based APIs, end-users have to reify the type information of the callback, even if that information is already defined in the interface. This is particularly cumbersome when working with generics.

emitter:observe(events.Greeting, function(e: event.Greeting)
    print("Hello " .. e.name)
end)

Ideally, we could just use:

emitter:observe(events.Greeting, function(e)
    print("Hello " .. e.name)
end)

This feature is sometimes called contextual typing or target type inference. It allows the type checker to infer the types of lambda/closure parameters from the expected type at the call site, rather than requiring explicit annotation.

For example, in Java:

list.forEach(item -> System.out.println(item));

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions