Last active
October 31, 2023 11:58
-
-
Save MaksimDmitriev/18988c71b582ff9becbf9b0b884f1973 to your computer and use it in GitHub Desktop.
Kotlin out (even with <out T> "? extends" is needed in Java)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface Consumer<in T> { | |
fun put(param: T) | |
} | |
class ConsumerImpl<in T> : Consumer<T> { | |
override fun put(param: T) { | |
println("put param: $param") | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package ru.maksim.sample; | |
import org.junit.jupiter.api.Test; | |
public class SampleJavaTest { | |
@Test | |
void foo() { | |
Source<Integer> intSource = new SourceImpl<>(11); | |
Source<? extends Number> source = intSource; | |
final Number o = source.nextT(); | |
System.out.println(o); | |
// consumer | |
final Consumer<? super Number> consumer = new ConsumerImpl<>(); | |
consumer.put(12); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface Source<out T> { | |
fun nextT(): T | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment