https://kotlinlang.org logo
#announcements
Title
# announcements
r

robfletcher

12/10/2017, 7:29 PM
I have 2 methods
Copy code
fun <ACTUAL> expect(
  actual: ACTUAL,
  block: ObjectAssert<ACTUAL>.(ACTUAL) -> Unit
): ObjectAssert<ACTUAL>
and
Copy code
fun <ELEMENT> expect(
  actual: List<ELEMENT>,
  block: SoftAssertionListAssert<ELEMENT>.(List<ELEMENT>) -> Unit
): SoftAssertionListAssert<ELEMENT>
that the compiler is unable to distinguish between at the call site. I get why that’s an issue but confusingly I have overloads of both that only accept the first parameter:
Copy code
fun <ACTUAL> expect(actual: ACTUAL): AbstractObjectAssert<*, ACTUAL>
and
Copy code
fun <ELEMENT> expect(actual: List<ELEMENT>): ListAssert<ELEMENT>
that the compiler can distinguish. Can anyone enlighten me as to why one is a problem and no the other? Is there any way I can disambiguate the 2 param form?
k

karelpeeters

12/10/2017, 7:58 PM
Is there an inheritance relation between
SoftAssertionListAssert
and
ObjectAssert
?
a

Andreas Sinz

12/10/2017, 10:51 PM
@robfletcher can you give us an example how you try to invoke
expect
?
r

robfletcher

12/11/2017, 1:21 AM
I was wondering about the inheritance relationship but no there is not.
Those are classes from AssertJ. I’m working on some Kotlin extensions for it.
The only solution I’ve found so far is to add a 2nd generic parameter to the list version but even then I have to specify the generic type at the call site
a

Andreas Sinz

12/11/2017, 9:57 AM
@robfletcher do you pass a lambda as the second parameter?
r

robfletcher

12/11/2017, 6:38 PM
yes
I can get it to work by declaring the lambda as a variable with a specified type. That’s not ideal as I’m trying to create a reasonable test DSL. I’ll have to see if there’s another way I can disambiguate things
a

Andreas Sinz

12/11/2017, 8:55 PM
@robfletcher the problem is, that the kotlin-compiler needs to know the inputs/outputs of a lambda before it checks the body of the lambda. Thats why declaring the lambda before works. I don't really see a way around that apart from renaming one of the methods
r

robfletcher

12/11/2017, 8:56 PM
yeah, it makes sense
thanks for the help
2 Views