Last active
December 30, 2015 15:19
-
-
Save v6ak/7847548 to your computer and use it in GitHub Desktop.
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
// Some plain classes with subclasses | |
class Foo | |
class SubFoo extends Foo | |
class SubSubFoo extends SubFoo | |
class SubSubSubFoo extends SubSubFoo | |
// A generic class | |
class Bar[T <: Foo](val foo: T) { | |
} | |
object Main{ | |
//def extractFoo(bar: Bar[_]): Foo = bar.foo // This does not compile | |
def extractFoo2(bar: Bar[_ <: Foo]): Foo = bar.foo // But this does. | |
// def extractAny(bar: Bar[_ <: Any]): Foo = bar.foo // This also does not compile. | |
} |
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 com.v6ak.experiments; | |
// Some plain classes with subclasses | |
class Foo{} | |
class SubFoo extends Foo{} | |
class SubSubFoo extends SubFoo{} | |
class SubSubSubFoo extends SubSubFoo{} | |
// A generic class | |
class Bar<T extends Foo>{ | |
private final T foo; | |
public Bar(T foo) { | |
this.foo = foo; | |
} | |
public T getFoo() { | |
return foo; | |
} | |
} | |
public class Main { | |
// All of these methods work in Java: | |
public static Foo extractFoo(Bar<?> bar) { | |
return bar.getFoo(); | |
} | |
public static Foo extractFoo2(Bar<? extends Foo> bar){ | |
return bar.getFoo(); | |
} | |
public static Foo extractAny(Bar<? extends Object> bar){ | |
return bar.getFoo(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment