dfinity / motoko

Simple high-level language for writing Internet Computer canisters
Apache License 2.0
517 stars 97 forks source link

Type Inference: Unexpected implicit instantiation at `None` #1639

Closed crusso closed 3 years ago

crusso commented 4 years ago

Christoph says:

Oh, I don't like that omitting a type parameter defaults it to None... chasing that one down was a pain Eg.

  public func toArray<A>(xs : Iter<A>) : [A] {
    let buffer = Buffer.Buf<A>(8);
    apply(xs, func(x : A, ix : Nat) { buffer.add(x) });
    return buffer.toArray()
  };

If you omit the <A> on the buffer constructor you get a failure about mismatch between A and None at the buffer.add call.

crusso commented 4 years ago

Claudio: I'm not really sure I can better using just local inference though...

Christoph Hegemann : Could this be worth special casing like "applying a non-function type"? Hinting the user that omitting type parameters on constructors defaults them to None?

rossberg commented 4 years ago

Maybe the simple solution is to just warn if inference defaults a type argument to None? In the rare case where it's actually intended, devs can shut up the warning by making the type explicit (which probably helps readability as well).

crusso commented 4 years ago

Actually we might be able to do better. I think the problem here I that A in Buf invariant so any underdetermined choice is not minimal/principal thus bad. Will need to look at the algorithm again.

nomeata commented 4 years ago

Is this still a problem? Care to commit a test case that exhibits the bad error message, so that it’s clear whether/when this is fixed?

kritzcreek commented 4 years ago

Does https://github.com/dfinity/motoko/pull/2149 look good?

nomeata commented 4 years ago

Perfect, thanks