exaV / screeps-kotlin-types

Screep's Kotlin type definitions
MIT License
17 stars 18 forks source link

Combining Constants works in JS but not in Kotlin #44

Closed thigg closed 5 years ago

thigg commented 5 years ago

Bein able to combine e.g. FIND constants is a very useful feature:

find(FIND_CONSTRUCTION_SITES | FIND_STRUCTURES,...)

I guess there is no nice solution to preserve typing, but mebe we should still make it available?

Jomik commented 5 years ago

Can you not do it with

find(FIND_CONSTRUCTION_SITES or FIND_STRUCTURES,...)
thigg commented 5 years ago

Ah I didn't try the or, just |. Maybe that should be documented somewhere?

thigg commented 5 years ago

No, I get a: Unresolved reference: or when I try: findInRange(FIND_MY_CONSTRUCTION_SITES or FIND_MY_STRUCTURES, 4 )

thigg commented 5 years ago

Idea from @AWRyder : FIND_MY_CONSTRUCTION_SITES.value or FIND_MY_STRUCTURES.value seems to work. Will test.

thigg commented 5 years ago

It is compiled to

pos.findClosestByPath((FIND_MY_STRUCTURES.value or FIND_CREEPS.value) as FindConstant<RoomObject>)

$receiver.pos.findClosestByPath(Kotlin.isType(tmp$_0 = FIND_MY_STRUCTURES | FIND_CREEPS, Object) ? tmp$_0 : throwCCE());

This returns always null.

Maybe because it is no Object?

thigg commented 5 years ago

While you can combine them in JS, this is no valid behavior after all. I think i dreamed of that function ;)

thigg commented 5 years ago

Wrote me a helper for this:

/*
 * Find with multiple flags
 */
fun RoomPosition.findClosestByPathMulti(
        types: Array<FindConstant<RoomObject>>,
        opts: FindClosestByPathOptions<RoomObject>
): RoomObject? {
    val objects: Array<RoomObject> = types.map { Game.rooms[this.roomName]?.find(it) }.foldRight(arrayOf<RoomObject>(), { l, r -> l!! + r })
    return findClosestByPath(objects, opts)
}

//Call with:
pos.findClosestByPathMulti(arrayOf(FIND_MY_STRUCTURES,FIND_MY_CREEPS)as Array<FindConstant<RoomObject>>,options {...})