Language feature to support Fluent Syntax for Static Method Calls

Amejonah Izoo amejonah at gmail.com
Tue Mar 28 22:21:53 UTC 2023


On 28.03.23 22:15, Tagir Valeev wrote:
> If you are not attentive enough you won't realize that toOtherType() 
> is an extension method that was created in a completely unrelated 
> module in a very specific context to solve a very specific problem, 
> and this method has poor contract and is not applicable generally. 
> Probably, it was declared as public by accident (partially because 
> Kotlin is public-by-default) and was never intended to be used from 
> outside. With a static method, you normally see which class the method 
> belongs to, so such a problem doesn't happen. However, with extension 
> methods, you just have a new entry in the imports list, which is far 
> away from the use-site, so it's hard to notice what you are actually 
> depending on.

I would like to point to
https://youtrack.jetbrains.com/issue/KT-29227/Provide-package-private-visibility-modifier-or-another-scope-reducing-mechanism
for this critic. The problem you mentioned has nothing to do with 
extension functions per se but rather with visibility/scoping of members 
in general.

The value of these functions is to allow adding (static/final) methods 
based also on generic constrains like a sum() function for 
Iterable<Int>. In Java you would need either nest or to convert to 
IntStream first which is just a identity map (mapToInt(i -> i)).
I recall a discussion here about a feature to restrain certain methods 
to specific generic types. This is one of the main advantages of 
extensions besides flexibility and... well.. the ability to extend 
foreign types with utilities in your project (you find a missing method? 
just add it. Also... no one has added toPersonGroup() method to 
List<Person> which can be quite useful in some cases).

Well... if you (@Kristofer Karlsson <krka at spotify.com>) really want to 
use Java, for some reason instead of switching to another language, 
because Java will never add such features anyway (and if so, it doesn't 
matter, we will be dead at that point), there is still the possibility 
to use 
https://github.com/manifold-systems/manifold/tree/master/manifold-deps-parent/manifold-ext



More information about the amber-dev mailing list