简体   繁体   中英

argument expression's type is not compatible with formal parameter type,You may wish to investigate a wildcard type such as `_ <: Any`. (SLS 3.2.10)

I'm using below code to check for something, and I get two errors at ".map(f => f.getBlacklistedAccounts.contains(accountID))" line, have no idea what does that means, can anyone help me with that? Thanks.

The first one is

argument expression's type is not compatible with formal parameter type;
no type parameters for method map: (x$1: java.util.function.Function[_ >: my.util.BlacklistRule, _ <: U])java.util.Optional[U] exist so that it can be applied to arguments (java.util.function.Function[my.util.BlacklistRule,Boolean])
found   : java.util.function.Function[my.util.BlacklistRule,Boolean]
[scalac-2.12]  required: java.util.function.Function[_ >: my.util.BlacklistRule, _ <: ?U]
[scalac-2.12] Note: my.util.BlacklistRule <: Any, but Java-defined trait Function is invariant in type T.
[scalac-2.12] You may wish to investigate a wildcard type such as `_ <: Any`. (SLS 3.2.10)
[scalac-2.12]       .map(f => f.getBlacklistedAccounts.contains(accountID))
[scalac-2.12]        ^

Second one

 error: type mismatch;
[scalac-2.12]  found   : java.util.function.Function[my.util.BlacklistRule,Boolean]
[scalac-2.12]  required: java.util.function.Function[_ >: my.util.BlacklistRule, _ <: U]
[scalac-2.12]       .map(f => f.getBlacklistedAccounts.contains(accountID))

my code

def isAccountBlacklistForAction(ruleBlacklist: RuleBlacklist, accountID: String, rule: String): Boolean ={
    ruleBlacklist.getRules
      .stream
      .filter(a => a.getRuleName.equals(rule))
      .findFirst()
      .map(f => f.getBlacklistedAccounts.contains(accountID))
      .orElse(true)
  }

It seems to have trouble inferring the type arg to map when the result is Boolean .

object Test extends App {
  val vs = java.util.List.of(1, 2, 3)

  println {
    vs.stream()
      .filter(_ > 2)
      .findFirst()
      //.map(i => i + 1)         // OK
      //.orElse(42)
      //.map(i => i > 0)         // OK scala 2.13
      .map[Boolean](i => i > 0)  // required for scala 2.12
      .orElse(true)
  }
}

It was fixed in 2.13.1 . The issue hints that the bug is hit if the A => B output is different type from input.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM