Skip to content

Type inference insufficient around trait parameterized functions #3156

Closed
@msullivan

Description

@msullivan

This is related to #912.

The following code doesn't work:

fn main() {
    for iter::eachi(some({a: 0})) |i, a| { 
        #debug["%u %d", i, a.a];
    }
}

It fails with

nubs/closure-trait-infer.rs:3:27: 3:30 error: the type of this value must be known in this context
nubs/closure-trait-infer.rs:3         #debug["%u %d", i, a.a];

which is really unfortunate.

eachi is declared with the prototype fn eachi<A,IA:base_iter<A>>(vec: IA, blk: fn(uint, A) -> bool). When called in the above code, IA is instantiated with the type option<{a: int}>, and a note is made that option<{a: int}> must implement the trait base_iter<A>. However, without knowing the details of the implementation, there is no way to know that the only A such that option<{a: int}> implements base_iter<A> is {a: int}.

I can think of a couple of possible solutions, varying wildly in feasibility:

  1. Do some sort of opportunistic impl search during typechecking, probably in some super ad-hoc place such as during function argument typechecking, after checking the non-closure arguments but before checking closure arguments. This is not particularly principled but might be pretty feasible.

  2. Introduce proper higher kinded type variables and traits over them. Then traits wouldn't be parameterized over type variables but would instead be implemented for higher kinds. The signature of eachi would be fn eachi<A,IA:base_iter>(vec: IA<A>, blk: fn(uint, A) -> bool) and IA<A> would be unified with option<{a: int}> right off the bat.

  3. Fix issue Block type-inference sometimes fails #912 so that the type isn't needed while typechecking the closure.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-type-systemArea: Type systemC-enhancementCategory: An issue proposing an enhancement or a PR with one.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions