Kotlin unexpected `unresolved link`

I am new to Kotlin and here is my code:

class C(val boy: Int = 0) { fun <T, E> boy(i: Int) = i } fun girl(b1: Boolean, b2: Boolean) = println("boy($b1, $b2)") fun main(args: Array<String>): Unit { val A = 234 // see? A defined! val B = 345 // see? B defined! val c = C(123) // c is also defined! girl(c.boy < A, B > A) // hey look at here } 

IntelliJ IDEA gives me:

  • unresolved reference: A
  • unresolved reference: B
  • unresolved reference: c

In the line hey look at here .

I think my code is syntactically correct, what's wrong?

+5
source share
2 answers

You have come across a very rare case of syntactic ambiguity. I think this is the first for SO, congratulations!

Your original syntax is technically correct, but in this context it can also be interpreted as an attempt to call c.boy<A,B> . Since the compiler did not know what you had in mind, it suggested that you need a function call.

The simplest fix is ​​to add parentheses as you did, or reorder the expressions:

 girl(c.boy < A, A < B) 

PS The same thing can happen in C #, so it is not unique to Kotlin

+9
source

Well .. I solved these errors by adding a pair of curly braces:

 fun main(args: Array<String>): Unit { val A = 234 val B = 345 val c = C(123) girl((c.boy < A), B > A) // hey look at here } 

But I'm still wondering why my code above doesn't work

Edit: see another answer

+2
source

Source: https://habr.com/ru/post/1268525/


All Articles