Я изучаю Kotlin и какое-то время застрял с этой ошибкой: я пытаюсь определить свойство класса ie heat
на основе другого свойства ie spiciness
, используя Оператор when
со следующим кодом
14 class SimpleSpice{
15 var name: String = "curry"
16 var spiciness: String = "mild"
17 var heat: Int = {
18 get(){
19 when (spiciness) {
20 "weak" -> 0
21 "mild" -> 5
22 "strong" -> 10
23 else-> -1
24 }
25 }
26 }
27 }
Я получаю следующую ошибку:
Error:(10, 29) Kotlin: Type mismatch: inferred type is () -> Int but Int was expected
Error:(17, 21) Kotlin: Type mismatch: inferred type is () -> ??? but Int was expected
Error:(18, 9) Kotlin: Unresolved reference. None of the following candidates is applicable because of receiver type mismatch:
public inline operator fun <K, V> Map<out ???, ???>.get(key: ???): ??? defined in kotlin.collections
public operator fun MatchGroupCollection.get(name: String): MatchGroup? defined in kotlin.text
Кроме того, код get () окрашен в красный цвет со следующим исключением:
Type mismatch: inferred type is () -> ??? but Int was expected. Unresolved reference. None of the following candidates is applicable because of receiver type mismatch:
public operator fun MatchGroupCollection.get(name: String): MatchGroup? defined in kotlin.text.