Scala can't infer type arguments of Java method -


i have following sophisticated type hierarchy in java:

// first type interface element<type extends element<type>> {     type foo(type a, type b); }  // second type interface payload<type extends payload<type>> {     type bar(type[] array); }  // toy implementation final class somepayload implements payload<somepayload> {     @override     public somepayload bar(somepayload[] array) { return array[0]; } }  // mix of first , second interfaces interface complicatedelement<               payloadt extends payload<payloadt>,               objectt extends complicatedelement<payloadt, objectt>>           extends element<objectt> {      payloadt getpayload();      objectt add(objectt a, objectt b); }  // toy implementation final class somecomplicatedelement        implements complicatedelement<somepayload, somecomplicatedelement> {     final somepayload data;      public somecomplicatedelement(somepayload data) {         this.data = data;     }      @override     public somepayload getpayload(){ return data; }      @override     public somecomplicatedelement foo(somecomplicatedelement a, somecomplicatedelement b) {         return b;     }      @override     public somecomplicatedelement add(somecomplicatedelement a, somecomplicatedelement b) {         return a;     } } 

i have static method deals complicatedelements:

pubic static <payloadt extends payload<payloadt>,               objectt extends complicatedelement<payloadt, objectt>> list<objectt> method(objectt input) {     return collections.singletonlist(input); } 

now, java can call method without problems this:

public static void main(string[] args) {     system.out.println(method(new somecomplicatedelement(new somepayload()))); } 

however, when try same in scala:

import foobarjava.{somecomplicatedelement, somepayload, method}  def main(args: array[string]): unit = {   println(method(new somecomplicatedelement(new somepayload()))) } 

i have compilation error:

error:(10, 21) inferred type arguments [nothing,foojava.somecomplicatedelement] not conform method method's type parameter bounds [payloadt <: foojava.payload[payloadt],objectt <: foojava.complicatedelement[payloadt,objectt]] println(foojava.method(new somecomplicatedelement(new somepayload())))

i can fix specifying type parameters explicitly:

println(method[somepayload, somecomplicatedelement](new somecomplicatedelement(new somepayload()))) 

but annoying , avoid (i guess possible since java compiler work fine this). there way so?

(i guess possible since java compiler work fine this)

scala has richer type system java. example in java there no type similar nothing in scala (i.e. universal subtype). it's possible compiler in language richer type system unable infer types while in similar situation compiler in language poorer type system can this.

if specifying type parameters annoying why not create helper method?

private def methodwithpayload(data: somepayload): java.util.list[somecomplicatedelement] =     method[somepayload, somecomplicatedelement](new somecomplicatedelement(data))  methodwithpayload(new somepayload) 

in java there option <somepayload, somecomplicatedelement>method(..), in scala there 2 options method[somepayload, somecomplicatedelement](..) , method[nothing, somecomplicatedelement](..) , both options valid.


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -