Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Partial type inference #4664

Closed
nventuro opened this issue Mar 27, 2024 · 1 comment
Closed

Partial type inference #4664

nventuro opened this issue Mar 27, 2024 · 1 comment
Labels
enhancement New feature or request

Comments

@nventuro
Copy link
Contributor

nventuro commented Mar 27, 2024

Problem

For the most part type inference works fantastically in Noir, but there are some scenarios in which the type must be indicated explicitly. An example I keep running into is reading from public storage:

struct MyStruct {
  x: Field,
}

impl Deserialize for MyStruct {... }

fn read<T, N>(storage_slot: Field) -> T where T: Deserialize<N> {
    T::deserialize(storage_read(storage_slot))
}

let foo = read(5);
bar(foo.x); // The type of foo is uknown and `x` cannot be accessed

Interestingly I think I run into #4653 while trying out alternatives, but failed to realize it was an issue distinct from #4502.

The above can be fixed by annotating foo: MyStruct. This is (I think?) similar to Rust's collect() function, which may return multiple types (vectors etc). Unlike Rust however, we can't annotate just the missing part (i.e. the Vec), we must instead annotate the full type.

In the example above, if MyStruct was generic over a number of types, those would need to be also explicitly laid out, which I found ends up quickly polluting the sorrounding code and requiring extra type annotations in additional places.

Happy Case

It'd be great if we could have partial type inference, so that I only need to list the types that are actually ambiguous. Rust uses _ to indicate types that remain implicit, e.g. let foo: Vec<_> = bar.collect().

Project Impact

Nice-to-have

@nventuro nventuro added the enhancement New feature or request label Mar 27, 2024
@nventuro
Copy link
Contributor Author

Closed via #5275

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
Status: Done
Development

No branches or pull requests

1 participant