czuckie
03/19/2025, 9:21 AMprivate fun String.asDomainSpecificObject(): DomainSpecificObject = ...
I would prefer a function to handle such things that explicitly took a string.
I thought the overwhelming view/opinion was that extension functions and properties were right when they extended the abstraction of the class that was being given an extension.
Beyond initial confusion and my own opinion, is there any reason to avoid such use of extension functions/properties, or should I embrace it?hho
03/19/2025, 10:09 AMUse extension functions liberally.And that leads to people doing everything in an extension. 😢
czuckie
03/19/2025, 10:55 AMRobert Williams
03/19/2025, 12:36 PMstringValue?.asDomainSpecificObject?.process()
vs
stringValue?.let { asDomainSpecificObject(it) }?.process()
I think it’s something you get used to but definitely some codebases will use it more extensively than othersCLOVIS
03/19/2025, 1:19 PMCLOVIS
03/19/2025, 1:20 PMprivate fun String.asDomainSpecificObject(): DomainSpecificObject = ...
> I would prefer a function to handle such things that explicitly took a string.
This is explicitly considered a bad idea by core library authors. For example, see String.toInstant
that was deprecated in KotlinX.Datetime, and replaced by Instant.parse(String)
It's a bad idea because extension functions should apply to all values of the type, not just specific ones that happen to be formatted like you wantczuckie
03/19/2025, 1:53 PMIt's a bad idea because extension functions should apply to all values of the type, not just specific ones that happen to be formatted like you want (edited)I really like this distinction!
CLOVIS
03/19/2025, 1:56 PMCLOVIS
03/19/2025, 1:57 PM"123".toInt()
is a kind of weird case, because according to that rule it shouldn't exist, but it's sooo convenientczuckie
03/19/2025, 1:58 PMint
is part of the language thoughRobert Williams
03/19/2025, 2:00 PMString.toInstant
is that it’s in the public/ global namespace (also as mentioned there, the fact that there was already a scoped version).Robert Williams
03/19/2025, 2:01 PM