Olaf Gottschalk
07/02/2025, 12:49 PM_:
because my "top level" functions do not directly care about the parameter name, they do not call anything from those context values. All they do is call lower-level functions, usually bridge functions I'd say, that specifically need the parameter and give it an explicit name.
Now, seeing that my most used character in modern Kotlin will be the _
, I wonder whether from a language design perspective it would be more beneficial to allow all context parameters where the name is irrelevant to be simply stated just like context receivers where before: without any name spec in front of the type. Just like even now a type alias works. Type aliases, interestingly did not change at all coming from context receivers!
I think it might be done like this because right now coming from context receivers it is not a good idea to allow the same syntax to mean different things, but on the long run:
would it not be possible to omit this unnecessary, cumbersome piece of _:
everywhere?Alejandro Serrano.Mena
07/02/2025, 12:51 PM_
once the transition from context receivers is done
on the other hand, having a name in front of the context parameter may allow us to provide better syntax for function with context parameters, for example, allowing to provide the context as additional (named) parametersAlejandro Serrano.Mena
07/02/2025, 12:52 PM_:
is linked to those who used context receivers in the past, or also happens with first-time users of context parametersOlaf Gottschalk
07/02/2025, 1:19 PMcontext(_: Raise<E>)
and then call bridge functions. This is currently the most used part in my code base after the transition. And it looks awful. Just the way Kotlin never wanted to look like. 😉
If you have small functions that compose bigger functionality, there will always be a level of higher level functions that simply call other functions, and all they need is a context parameter without a name.
But hey... I am just a user, not a programming language design expert... 😉Olaf Gottschalk
07/02/2025, 1:21 PMraise.ensure(...)
- at least that's what I think...Olaf Gottschalk
07/02/2025, 1:22 PMAlejandro Serrano.Mena
07/02/2025, 1:32 PMraise(...)
without qualification. However, many other cases of context parameters may involve some sort of service, in which you often want to calls functions from that service:
context(users: UserService)
fun UserId.getFriendNames() {
val friends = users.getById(this)
}
Olaf Gottschalk
07/02/2025, 1:48 PMcontext(Raise<E>)
reading "to be used only in the presence of a Raise context". It's tiny, but I really felt annoyed by putting so many useless _:
into my code. This was the very first time in my Kotlin experience for years where I felt I needed to add boilerplate stuff.Olaf Gottschalk
07/02/2025, 1:52 PMcontext(Raise<Error>, users: UserService)
fun UserId.getFriendsNames() {
val friends = users.getById(this)
}
...
interface UserService {
context(Raise<Error>)
fun getById(userId: UserId): User
}
Olaf Gottschalk
07/02/2025, 1:54 PMAlejandro Serrano.Mena
07/02/2025, 1:55 PMOlaf Gottschalk
07/02/2025, 1:58 PM