https://kotlinlang.org logo
#language-evolution
Title
# language-evolution
p

Pablichjenkov

10/18/2023, 10:41 PM
Just freeing my imagination. Is there any language that features in its design something like function inheritance or function composition. I have a couple of Composable functions that I repeatedly call in other Composables. Eg: A 'BackPressHandler' to handle back press in each screen. Other common TopBars and such. Instead of copy and paste the same functions call block in every screen, is there any way to tell the compiler to inline other function calls automatically in the order we pass. Let's say Fun1() { ... } :
uses/inline/copy
::Fun2, ::Fun3 This kind of design. The result something like:
Copy code
Fun1(){
  Fun2()
  Fun3()
  ...
}
Perhaps a feature in the Compose plugin 🤔
y

Youssef Shoaib [MOD]

10/18/2023, 11:21 PM
Could you clarify more what syntax you're sort of thinking of and what the expected behaviour would be like?
p

Pablichjenkov

10/19/2023, 1:03 AM
I would say something that looks like:
Copy code
Fun1 call ::Fun2, ::Fun3 () { fun1-body-here }
call
could be combined with
inline
Seen it twice I think is better just write it as a regular function in current syntax 😒
y

Youssef Shoaib [MOD]

10/19/2023, 11:18 AM
I mean, if you really want to, one can do:
Copy code
fun call(vararg blocks: () -> Unit) { for(block in blocks) block() }

//elsewhere
Fun1() {
  call(::Fun2, ::Fun3)
  ...
}
p

Pablichjenkov

10/19/2023, 12:22 PM
Yeah I've realized that similar stuff can be achieved with existing language features. Like what you mentioned or creating helper Composable functions that encapsulates the reusable code and slot Composable to fill in.
Copy code
Fun1WithReusableStuff(@Composable Slot...) {
  Fun2()
  Slot()
  Fun3()
}
Then use it like:
Copy code
Fun1WithReusableStuff {
  ... // Composable stuff
}
I LL post the actual problem that I want to solve in the compose channel later