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
Improve error message for function application on a datatype #442
Comments
Constructors are PascalCase. Try However, in general, the type inference does require lexically scoped variables to unambiguously resolve. This makes it easier to look at a variable and see what the type is by looking at it's definition or parameter type. It gets weird if you use a variable and it looks to have a defined type in the parameter's type signature, but the usage in the body doesn't match that expectation. Implicit parameters participate in higher order overloading, and thus are allowed to be qualified. The syntax |
Whoops, looks like I simplified the repro one step too far. This is the correct example:
Error:
|
This is actually a perfect example why the current behavior is the correct one. What you wrote looks exactly like you are trying to use the copy function of model: Users would be highly confused when reading The recommended solution is to qualify your model function:
You could qualify it with My preferred solution though is to keep variable names short. i.e.
Again, this is just my opinion. Regardless on your opinion about naming, I don't think this is a bug in the current implementation, at least not how Daan and I designed it. But I do recognize that it could use more documentation on the reasons why lexical names do follow regular shadowing principles, but implicit parameters and other identifiers do not. |
To be honest, I didn't figure out from the error message it was the parameter name that was the problem, until I trimmed it al the way down to this minimal repro. Moreovier, previous versions of Koka were very lenient with resolving identifiers based on types, and you could seemingly overload an identifier in any number of ways without causing problems. These two reasons made it look like an unintended regression to me. |
I would actually say that Koka is more lenient with resolving identifiers based on types currently, based on what I know Daan did to support implicits. I don't think it ever supported overloading shadowed locals, and if it did then that was unintentional. Daan has personally told me that he does not want overloaded locals when it came up as we designed implicits because implicits had to be an exception to the rule. The error could probably be improved though like you said. In particular the context should also refer to where the identifier was defined, and I'm not sure why it thinks the expected type should be model. |
The rules about shadowing make sense to me in isolation.
The combination of these two things makes it so that this particular naming conflict, i.e. local variables and functions sharing the same name, arises quite naturally. |
I'm curious why you need a factory function that is named the same as the data type? You can use default expressions on the data type definition if there is some initialization that needs to happen for the data members. If it is for initializing from a different type, or non-standard set of parameters, I would expect the locally qualified identifier solution to be more what you want than renaming to |
That's fair. The following works fine and is readable too
What do you mean by that? |
https://koka-lang.github.io/koka/doc/book.html#sec-structs The example shows initializing realname with name, but it is just any arbitrary expression (provided it doesn't use effects I think? Not sure about that) |
Ah, I had completely overlooked that detail (the |
I just skimmed the discussion so apologies if I am rehashing :-), but:
This is as intended; if we would resolve The solution is indeed to qualify explicitly, either as
and use Finally, with implicit parameters, the local name is locally qualified ,where an implicit parameter btw. This is still experimental so it may still need tuning or changing rules so any examples that help evaluate the design are very welcome. |
Minimal (Contrived) Reproduction
Error message
Expected behavior
I believe there is no ambiguity in the code above, so I think it should be able to compile.
The text was updated successfully, but these errors were encountered: