-
Notifications
You must be signed in to change notification settings - Fork 170
Infer the type of a lambda parameter from a UDT copy-and-update expression #1365
Comments
The first issue with the array update is caused by the type inference applying the The second issue with the UDT update is a symptom of a more fundamental issue. Without knowing the type of The least obtrusive solution that I can think of for the UDT issue is to change copy-and-update to rely only on lexical information to determine whether it's an array update or a UDT update, rather than on type information. The rule could be:
But what I would strongly recommend from a language design standpoint, is to make UDT updates and array updates syntactically distinct. For example, when you update a record in F# using |
#1377 covers the first issue with the array index. I renamed this issue to focus on the UDT type inference problem. Since this situation was out of scope for the current implementation of type inference due to the language design, this is asking for additional functionality, so I think this is more like an enhancement than a bug. |
Discussed during a language design meeting today, and we decided to go with the heuristic above:
In the future we might revisit the syntax, but for now we want to avoid breakage, and this heuristic is expected to have high compatibility with existing code. |
There's a potential (future) issue if you imagine Q# supporting global constants. Imagine you have // speculating on syntax for constants
const X : Int = 3;
function Foo(xs : Bool[]) : Bool[] {
return xs w/ X <- 3; // error
} Since newtype Vec2 = (X : Int, Y : Int);
function Foo(v : Vec2) -> Vec2 {
return v w/ X <- 3; // works
} At a later time, someone (even a library!) adds a constant For the moment this is not a problem because Q# does not support global constants, only callables. Will Q# never support global constants? Or will the syntax be different? Or will we require |
Describe the bug
The type inference fails for the following code
The compiler generates an error on the
i
in the lambda body that states: "Expecting an expression of type Int or Range. Got an expression of type 'b". This error in particular persists even after adding the lineAlso copy-and-update expressions for user defined types seems to not be working in the body of a lambda, and produce cryptic errors. For a type
newtype Foo = (A : Int, B : Int);
the compiler produces the following error:Expected behavior
The compiler should be able to infer that
i
needs to be ofInt
with or without the second line.The text was updated successfully, but these errors were encountered: