-
Notifications
You must be signed in to change notification settings - Fork 167
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
Record property vs dot functions #177
Comments
Ah, the interesting thing for Koka is that there are no field selectors as such; for a struct like:
Koka generates a primitive function Moreover, there is no "field assignment" either, Koka just generates a primitive copy function for data types with parameters the each have a default value set to the current record field:
Now, we can write ps. The good part of this design is that it keeps the minimal but general principle: everything is just functions! the drawback is that it "pollutes" the namespace with field selector functions but since Koka has static overloading this works out reasonably well in practice -- wish to find a better solution to this though. |
Thanks! I see, so record accessors are just functions like in Haskell. So x.reverse could either mean “call the function from the struct” or “I wrote a function elsewhere, call that” and there is an overloading mechanism for functions. How does the overloading work? 🤔 D uses the ad-hoc method (pick the struct field over a global function—leads to incoherence), Idris uses TDNR, which has bad inference. Rust picks one like D, but you can say Trait::method(obj) to be explicit. I also have adopted x.foo() as syntactic sugar for foo(x) in my own pure FP language, but had to solve this namespacing issue before I could buy into it. I have my own solution, but am interested in others. The copying example is interesting! I was debating whether to do simple updates or bake lenses in. Undecided as yet. |
Also wondering about this. Is there a solution for the ambiguous identifier problem? Right now I'm getting around this by manually making the identifiers non-ambiguous by eg. prepending the module name (see #290). This is extra boilerplate I'd rather not do. |
@chrisdone Overloading now works by looking up the name in a map of names to fully qualified names. If it is ambiguous it tries to find an unambiguous one based on the type. See the latest In response to the original question: Type inference tries to find the function first, but if it is ambiguous then it tries to infer the types of the arguments to narrow the options. So in this case, it will first see if there is a single function called reverse. If there is multiple then it will infer the subexpression @tom-sherman I believe the error messages and disambiguation should work better now, but please give the new release a try and create new discussions / issues for feedback! |
I was wondering how you support record field access vs method calls in Koka.
If person.name is usually record access, and Koka has person.name.reverse for reverse(person.name), that seems to conflict. What if an object has a function called reverse as a field?
D chooses the object’s field first if it has one.
The text was updated successfully, but these errors were encountered: