I’m not sure what you intended to do, but to make it work you should write:
interface Floor {
fun <T : Comparable<T>> maxOf(a: T, b: T): T
}
class FloorImpl : Floor {
override fun <T : Comparable<T>> maxOf(a: T, b: T): T = if (a >= b) a else b
}
In the note, maxOf() functions are already a part of standard library
internal interface Floor {
fun <T: Any> maxValOf (oldVal: T, newVal: T): Any
}
by that i imply that for some ClassA maxValOf should be of type where T could be say MutableList
then
class FloorImpl : Floor {
override fun <T : MutableList<Int>> maxValOf(
oldVal: MutableList<Int>,
newVal: MutableList<Int>): Int {
// return maxOf(oldVal.max(), newVal.max() )
return 20
}
}
Which means an abstract interface method that can take in two args of type and spew out Anything, Any here is Int. in some other ClassB It could be String.
But compiler here says: ‘maxValOf’ overrides nothing
No the problem is ClassZ may implement it NOT as a collectible. rather like:
class Floor2Impl : Floor2 {
override fun <T : Int> maxOf(a: Int b: Int): String? {
return "I am a String here"
}
}
I mean whats the point of defining a Generic Interface function if I know firsthand what the other developer knows he is going to implement in some ClassFuture.
So I want to take in Anything, only known fact is that the Args are of same type
Function-level generics have a very limited extensibility. You can’t specialize their types in subclasses. For that you need to use type-level generics.
understood. this clears up things. However you may want Interfaces with multiple functions, in some rare cases perhaps, where each fun <T: Type> could be different so we couldn’t define that Interface by <T: Type> commonly. I know we can break into multiple interfaces. but what if an interface had several fun boundaries? and classA to Z used that interfaces’ functions for their own implementations?
Trying that out here in your example:
interface ABC {
fun<T: SomeType/Any?> ()
}
gives Declaration member missing if you just swap the type to fun instead. and remove it from the Class: ABC block
You can add any number of generic parameters to an interface:
interface Foo<A, B, C> {
fun a(): A
fun b(): B
fun c(): C
}
Type-level generics are useful because they can be specialized in subclasses. However, type-level generics are fixed for each instance of the type. Function-level generics don’t have this restriction, so neither of them is clearly superior. For example:
class Foo() {
fun<T> generic(param: T) = param!!
}
val foo = Foo()
foo.generic(5)
foo.generic("you can use any type")
class Bar<T>() {
fun generic(param: T) = param!!
}
val bar = Bar<String>()
bar.generic("you can only use strings here")
It’s not clear what’s your use case, but generics are generally (ha!) used in one of two ways:
to handle any possible type that meets specific constraints:
fun <T: Comparable<T>> Iterable<T>.max(): T? {
var max: T? = null
for (item in this) {
if (max == null || item > max) {
max = item
}
}
return max
}
//NOTE: this is already in the stdlib!
Notice how we only require that T, whatever actual class it ends up being, must implement Comparable<T> otherwise item > max won’t compile!
to bind a set of functionality to a “placeholder” type:
interface Collection<T> : Iterable<T> {
val size: Int
fun isEmpty(): Boolean
fun contains(element: T): Boolean
fun containsAll(elements: Collection<T>): Boolean
}
//NOTE: this is already in the stdlib!
Notice how in this case we don’t need to set any limitation on what T can be, but we can still be certain that each instance implementing this interface will have to specify one type, so all functionality will be consistent.