简体   繁体   中英

Scala ~ type mismatch; found : _$1 required: _$2

I have a generic in a function like so:

@import java.util.List;

@(field:List[_], min:Int=1)(f: (_, Int) => Html)
@{
    (0 until math.max(if (field.isEmpty) 0 else field.size, min))
        .map(i => f(field.get(i),i)) 
}

When i run this code, i get this really really unhelpful error message:

type mismatch; found : _$1 required: _$2

I don't know what this means, but the error seems to be with field.get(i) - any ideas how to resolve this?

your block take two parameter lists:

  1. first one with two parameters:
    • field: List[_] <--- list of type _$1
    • min: Int
  2. second one with one parameter which is a function (f) who takes (something of type _$2, and an Int) and return Html.

If you assume your field hold list of something with type exact same with first parameter should feed in f, you can define your function like:

def convertSomethingGenericToHtml[T](field: List[T], min: Int = 1)(f: (T, Int) => Html) {
    (0 until math.max(if (field.isEmpty) 0 else field.size, min)).map(i => f(field.get(i),i))
}

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