2 ms·
I've only seen this proposed as a feature to help with template meta-programming (though it could also make similar sense in any dynamic language). There, it h
by tsimionescu 4y ago
I've only seen this proposed as a feature to help with template meta-programming (though it could also make similar sense in any dynamic language).
There, it helps if a template can say `t.foo()`, and, with UFCS, can use that template with any t for which either `foo(t)` or `t.foo()` exist. In contrast, in C++ today, a template using `t.foo()` limits its own use only to types that have a foo() method, probably unnecessarily (note that writing `foo(t)` in a C++ template is less limiting, as someone who controls neither the template nor the type of t can still define that function).
However, outside of this use case, I think conflating these two is more of a negative than a positive. It means that there are twice as many places where I may need to lookup the definition of foo(), at the very least. So I wouldn't add this to any static language that doesn't support templates or macros.
- gpderetta 4y agoI think not introducing UFCSs originally in C++ is one of Stroustrup biggest regrets.
- xigoi 4y agoWhat do you mean by “twice as many places”? And looking up definitions is a job for the language server.
- tsimionescu 4y agoWithout UFCS, if I see obj.foo(), I know I can lookup the definition of foo() in the definition of obj's type, or in its supertypes. Even for foo(obj), there is often a canonical place where such functions are defined. With UFCS, I need to look in both of these places until I can find the right definition. And sure, the IDE/language server/other tooling can often help, but not always (e.g. if I'm browsing some code on Github). Either way, more ambiguity for no gains is typically not a good idea, even if the downsides are minor (again, I am very much in favor of UFCS where it's directly useful, such as C++ or D).
- xigoi 4y agoIn languages that have UFCS, there's no such thing as classes, so… you simply look for the procedure.
- tsimionescu 4y agoD certainly has classes, and a member function has access to private members, while a free-floating function does not. Nim indeed doesn't seem to have this distinction at all, and only seems to support encapsulation at the module level, not the class level (as far as I could tell from very brief searching - I have never programmed in it).