kierans777
06/29/2021, 4:05 AMfun doSomething(): ((Class<T>) -> T) -> T
However because lambda expressions can't have generic types, this doesn't compile for me.Scott Christopher
06/29/2021, 5:37 AMdoSomething
too. e.g.
fun <T> doSomething(): (Class<T>) -> T
kierans777
06/29/2021, 5:38 AMdoSomething
to be a reusable factory functionScott Christopher
06/29/2021, 5:39 AMScott Christopher
06/29/2021, 5:39 AMScott Christopher
06/29/2021, 5:40 AMT
as determined by the call-sitekierans777
06/29/2021, 5:42 AMIs there something that prevents that from being reused?Because I couldn't do
val something = doSomething()
val makeString = { cls: Class<String> -> "Hello World" }
val makeInt = { cls: Class<Int> -> 0 }
something(makeString(String.javaClass))
something(makeInt(Int.javaClass))
All my FP experience has been in dynamically typed languages, so I'm having to rethink how to write my code in Kotlin.Scott Christopher
06/29/2021, 5:44 AMScott Christopher
06/29/2021, 5:45 AMfun <T> doSomething(): ((Class<T>) -> T) -> ...
kierans777
06/29/2021, 5:45 AMScott Christopher
06/29/2021, 5:46 AMkierans777
06/29/2021, 5:47 AMkierans777
06/29/2021, 5:48 AMScott Christopher
06/29/2021, 5:49 AMkierans777
06/29/2021, 5:49 AMkierans777
06/29/2021, 5:49 AMdoSomething
) should not care about the type of T
Scott Christopher
06/29/2021, 5:49 AMkierans777
06/29/2021, 5:54 AMdoSomething
is invokedScott Christopher
06/29/2021, 5:55 AMsomething(makeString(String.javaClass))
something(makeInt(Int.javaClass))
kierans777
06/29/2021, 5:57 AMval s: String = something(makeString(String.javaClass))
val n: Int = something(makeInt(Int.javaClass))
kierans777
06/29/2021, 5:57 AMScott Christopher
06/29/2021, 5:58 AMsomething
doesn't need to know anything about the Class<T>
correct? As it is provided by `makeString`/`makeInt`kierans777
06/29/2021, 5:59 AMdoSomething
returns a function that takes a function of Class<T> -> T
kierans777
06/29/2021, 5:59 AMScott Christopher
06/29/2021, 6:01 AMsomething(makeString(String.javaClass))
something doesn't know anything about the Class<String>
, only makeString
needs toScott Christopher
06/29/2021, 6:01 AMsomething
function doesn't know about Class<String>
at allMitchell Skaggs
06/29/2021, 6:02 AMdoSomething
return a class that has operator fun <T> invoke(p1: (Class<T>) -> T): T
implemented.kierans777
06/29/2021, 6:02 AMsomething
takes a function and returns a String. something
takes another function and returns an Int.Mitchell Skaggs
06/29/2021, 6:02 AMsomething
with any generic type T
at the call siteMitchell Skaggs
06/29/2021, 6:02 AMkierans777
06/29/2021, 6:03 AMMitchell Skaggs
06/29/2021, 6:04 AMkierans777
06/29/2021, 6:05 AMMitchell Skaggs
06/29/2021, 6:06 AMsomething
can return that type and you can return different subtypes. The standard Function
type doesn't support function-scoped generic type parameters.Mitchell Skaggs
06/29/2021, 6:06 AMfun <T>
is, a function-scoped generic type parameter. Java has them as well, just in a slightly different location.)kierans777
06/29/2021, 6:07 AMdoSomething
then @Mitchell Skaggs?Mitchell Skaggs
06/29/2021, 6:08 AMinterface Thing {
operator fun <T> invoke(p1: (Class<T>) -> T): T
}
kierans777
06/29/2021, 6:10 AMfun doSomething(): Thing
?Mitchell Skaggs
06/29/2021, 6:10 AMinterface Thing {
operator fun <T> invoke(p1: (Class<T>) -> T): T
}
fun doSomething(): Thing {
return object : Thing {
override fun <T> invoke(p1: (Class<T>) -> T): T {
TODO("Not yet implemented")
}
}
}
val something: Thing = doSomething()
val s: String = something { c: Class<String> -> "" }
Mitchell Skaggs
06/29/2021, 6:10 AMobject : Thing
syntax is just creating an anonymous class implementing Thing
kierans777
06/29/2021, 6:11 AMkierans777
06/29/2021, 6:14 AMreified
version that means you don't need the Class
arg? 🤔Mitchell Skaggs
06/29/2021, 6:31 AMThing
into a class and implement the invoke
function there. reified
requires an inline
function, which can't be virtual (overrides another method, runtime polymorphism), so you can only have one implementation.Mitchell Skaggs
06/29/2021, 6:34 AMreified
.kierans777
06/29/2021, 6:52 AM