https://kotlinlang.org logo
Title
q

Quinn

05/15/2019, 1:38 PM
Having problems with the following code. When I call .sort { a,b -> distanceFormula } I can no longer access the array named sources. If I don't call sort I have no problems.
var sources = spawn.room.find(FIND_SOURCES).sort { a,b ->
        (a.pos.x - b.pos.x) * (a.pos.x - b.pos.x)
         + (a.pos.y - b.pos.y) * (a.pos.y - b.pos.y)}
    val creepsWithSource1 = creeps.count { it.memory.assignedSource == sources[0].id }
    val creepsWithSource2 = creeps.count { it.memory.assignedSource == sources[1].id }
    val assignedSource = if(creepsWithSource1 < creepsWithSource2) sources[0].id else sources[1].id
This results in the following error message. I'm trying to decipher what this error means so I can understand it in the future.
Unresolved reference. None of the following candidates is applicable because of receiver type mismatch:
public inline operator fun <K, V> Record<Int, [ERROR : No type element]?>.get(key: Int): [ERROR : No type element]? defined in starter
d

dalexander

05/15/2019, 1:43 PM
I believe
sort
doesn't return the sorted array, but rather sorts it in place. So by calling sort like that you are changing the type of
sources
. (I ran into a similar problem recently myself)
q

Quinn

05/15/2019, 1:50 PM
Interesting. In that case, I'll play with trying to figure out how to sort this array of
sources
. Do you happen to know what
fun <K, V>
is? Is that two types, one of which is
Record<Int, [Error]>
and the other is
.get(key: Int)
? I see that the error message mentions that the Record function is returning no type elements. Still very confused by this error messages.
d

dalexander

05/15/2019, 1:52 PM
Ah... the error message isn't very helpful, I agree. The error message is from the code
sources[0]
ie. you're trying to get the first element in the sources object. And because sort doesn't return a list it's complaining.
q

Quinn

05/15/2019, 1:53 PM
I would not have gleamed that by myself. Thanks @dalexander
d

dalexander

05/15/2019, 1:58 PM
By the way, it is still correct to use sort, but I would do something like:
val sources = spawn.room.find(FIND_SOURCES)
sources.sort { a,b -> (a.pos.x - b.pos.x) * (a.pos.x - b.pos.x) + (a.pos.y - b.pos.y) * (a.pos.y - b.pos.y) }
And no problem, glad I could help 🙂
✔️ 1
👍 1
q

Quinn

05/15/2019, 2:24 PM
Oh I see, that fixed my issue. I was super confused and went to scratch to try to figure it out and was stuck trying to figure out my types lol. Thanks again!
d

dalexander

05/15/2019, 2:29 PM
IDEA has some useful tools for figuring out what type something is. You can check the type of something your cursor is over with
ctrl + shift + p
on OS X. I think it's similar on Windows. You can also open the "do an action" window with
ctrl + shift + a
and type in expression type (or any other action, really).
q

Quinn

05/15/2019, 2:36 PM
Will those work outside of debugging? I can't run debugging because this code is for a game called screeps where I am just pushing code to their API.
I'm converting all of my code to javascript on compile and using types from their API that are converted from javascript to Kotlin. This has made it incredibly difficult to debug.
d

dalexander

05/15/2019, 2:37 PM
The type inference will definitely work outside of debugging.
types from their API that are converted from javascript to Kotlin
I could see this being a problem based on working with loosely typed JS API's in the past.
This is what invoking type information in the editor looks like
q

Quinn

05/15/2019, 2:42 PM
Ah nice, that is working for me. I just switched from Vim to IntelliJ for kotlin because it's almost completely necessary. Guess IntelliJ has something going for it. Thanks again!
👍 1