Why inheriting from a function in Kotlin can be a ...
# feed
u
Why inheriting from a function in Kotlin can be a good idea and some considerations on invoke operator https://proandroiddev.com/kotlin-pearls-3-its-an-object-it-s-a-function-it-s-an-invokable-bc4bfed2e63f
👍 7
❤️ 1
👏 6
e
I didnt know about the possibility to extend classes from functions..
s
Lambda-types, which you can use as base-classes, are Kotlin's single-method-interfaces. I tend to use typealias with them, not so much as a base-class. Then I can still assign a value that is a simple closure
= { ... }
, not an
= object : xxxx
d
Good blog 🙂 Unfortunately, there is pretty much no navigation (e.g. find usages/subclasses) for Kotlin function types in IntelliJ. For this reason on one of the projects we replaced most of the “functional objects” with interfaces (i.e. nominal types) so we can reliably navigate and refactor the codebase.
(I wonder if there is youtrack issue for navigating functional types.)
u
In Haskell we have Hoogle... maybe it's time to have something similar for Kotlin? 🙂
but to be honest I never had the necessity to look for class that inherited a given function
d
Hm, I should probably remember what we wanted but couldn’t do with navigation and find or create youtrack issue 🤔
u
my secret is that I always use copy-paste, so I don't have to navigate 🙂
Some vegetable curry ;)
😁 1
a
The issue is probably https://youtrack.jetbrains.com/issue/KT-13714 or the related one.
👍 2
s
oh thanks @Uberto Barbini. Now after reading your posts and Kotlin documantaions on medium i have understud how function as apply{} or run{} works in Kotlin This function: fun <T,S> T.runAny(str: T.() -> S): S { return str() }
❤️ 1
d
@Alexey Belkov [JB] yes, and also things like you cannot see hierarchy of subclasses for a functional type. E.g. I’d want to ctrl+alt click on
(String) -> Int
type, see list of classes implementing it and jump to implementation. Or search for all usages of type
(String) -> Int
. Although probably once the issues you mentioned are fixed, everything will start working.
a
I wouldn't be so optimistic about "everything will start working" 🙂 Please file an issue about functional types when you have the time.
👍 1