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

Tomasz Krakowiak

04/22/2021, 9:51 AM
Anyone has idea if Kotlin crew already considered implementation of type functions?
e

elizarov

04/26/2021, 7:59 AM
We don't have near-term plans due to the lack of compelling use-cases to counter-balance the added complexity. If you have a specific use-case in mind, then please share it here or file an issue to https://kotl.in/issue
t

Tomasz Krakowiak

04/26/2021, 3:39 PM
@elizarov I do : ) I am working on generic store API:
Copy code
public interface Store<in Query, out QueryResult<Q : Query>, in Mutation> {
	fun <Q : Query>query(q : Q) : QueryResult<Q>
	// ...
}

public class PropertyStore<Value>(
    private var value: Value
) : Store<GetValue<Value>, <GetValue<Value>> => Value, SetValue<Value>> {
    // ...
}

public class ListStore<Element>(/*...*/) : Store<ListQuery<Any?>, <ListQuery<Result>> => Result<Element>, ListMutation<Element>> {
    // ...
}

sealed class ListQuery<out Result<E : Element>>

object ListSize : ListQuery<Int>

class ListGet : ListQuery<<E>=>E>(val index : Int)
another alternative for me to model it properly would be https://youtrack.jetbrains.com/issue/KT-33262 - and creating base class for
Query<Result>
.
And a neat trick with nullability. We often have functions that result's nullability depends on parameter nullability - This can be expressed with type functions:
Copy code
typealias Converter<In, Out, Nullability<X>:X?> = (Nullability<In>) : Nullability<Out>
typealias NullableConverter<In, Out> = Converter<In, Out, <X>=>X?>
typealias NonNullConverter<In, Out> = Converter<In, Out, <X>=>X>
3 Views