swarm-game / swarm

Resource gathering + programming game
Other
834 stars 52 forks source link

Be more lenient in accepting known arguments to `require` #882

Open kostmo opened 1 year ago

kostmo commented 1 year ago

Describe the bug

I am not able to compile the following:

build {
    let quantity = 500 in
    require quantity "rock";
}

The error message is:

expecting device name in double quotes or integer literal

The workaround to ensure that "child" robots receive a programmatic quantity from inventory seems to be for the "parent" to give items repeatedly while the child polls using count.

byorgey commented 1 year ago

This is not a bug, and has been discussed in some detail in #738 . The basic explanation is that the arguments to require need to be statically known since requirements checking happens prior to running a program. That is, we have to be able to figure out what a newly built robot will need before we run its program.

Potentially, I could imagine doing something like this:

let quantity = 500 in
build { require quantity "rock"; ... }

That is, as long as quantity is already known at the time we execute the build we might be able to make it work. But I expect it would be complicated.

The workaround to ensure that "child" robots receive a programmatic quantity from inventory seems to be for the "parent" to give items repeatedly while the child polls using count.

Yes, that is annoying. Once we have #115 this will probably become easier since you can e.g. stuff a bunch of rocks in a box and then just give the box.

kostmo commented 1 year ago

At least for the application I had in mind, the quantity can be known outside of the build context, i.e.

let quantity = 500 in
build { require quantity "rock"; ... }

However, it's merely an issue of convenience now, as I've successfully employed the polling workaround.

xsebek commented 1 year ago

@byorgey do we have a way to get something like Ctx Term to see what the current variable name points to?

AFAIK we only have Env = Ctx Value that will be used at runtime.

I guess requirements would have to create the map itself. 🤔

byorgey commented 1 year ago

do we have a way to get something like Ctx Term to see what the current variable name points to?

No, that would be something extra we'd have to keep track of while doing typechecking & requirements analysis.

byorgey commented 2 months ago

I'm going to close this soon, since there's no general solution and anything we did to make it slightly better would be an ugly hack. However, I wanted to summarize the current state of things and maybe spark some discussion of other ways to improve the situation.