https://kotlinlang.org logo
#codereview
Title
# codereview
p

pguardiola

05/28/2017, 8:12 AM
If I define it using the concrete type
String
doesn’t complaint don’t know why but it works ¯\_(ツ)_/¯ thanks @anstaendig 👍
a

Andreas Sinz

05/28/2017, 11:39 AM
pguardiola: is that a function type from Java8? i mean is the second generic the return type?
p

pguardiola

05/28/2017, 11:44 AM
Not Java 8
Copy code
public static <T> PropertyValue<Function<T, String>> lineColor(Function<T, String> function) {
    return new PaintPropertyValue<>("line-color", function);
  }
a

Andreas Sinz

05/28/2017, 4:04 PM
@pguardiola how does
Function<I, O>
look like?
It's because of Java's raw types https://stackoverflow.com/a/2770692/1266326
a

Andreas Sinz

05/28/2017, 5:11 PM
@pguardiola It has nothing to do with raw types, its just a mismatch of the Generics. Imagine the follow:
Copy code
public class Function<I, O> {
    public O getPropertyValue() { }
}

public static <T> PropertyValue<Function<T, String>> lineColor(Function<T, String> function) {
    return new PaintPropertyValue<>("line-color", function);
}

private fun foo(bar: Function<Any, Any>) {
  lineColor(bar)
}

foo(Function<String, Int>())
The
lineColor
-Method expects your Function<> to return a String on
getPropertyValue
, but our
bar
returns an Int. Passing
Function<String, Int>
into
foo()
is perfectly fine, but passing it into
lineColor
doesn't work because of the different types of
O
p

pguardiola

05/28/2017, 5:17 PM
I know but in Java you're able to pass
Function
(without specifying explicitly the generic types) because of Java's raw types
a

Andreas Sinz

05/28/2017, 5:19 PM
Yeah, kotlin is more restricitve in that matter
p

pguardiola

05/28/2017, 5:21 PM
Yeah because it's 👌implemented 😋
👍 1
3 Views