`carefully` confused by `other-end` (and `myself`)
When using carefully around other-end it gets confused:
to setup
ca
crt 2
ask turtle 0 [ create-link-with turtle 1 ]
end
to foo
ask turtle 0 [ ask link 0 1 [ carefully [show other-end][show error-message] ] ]
end
to foo2
ask link 0 1 [ ask turtle 0 [ carefully [show other-end][show error-message] ] ]
end
will result in:
observer> foo (link 0 1): "Only a turtle can get the OTHER-END of a link."
respectively
observer> foo2 (turtle 0): "Only a link can get the OTHER-END from a turtle."
While those commands work fine without the carefully around them.
Maybe related to Issue #1071 ?
Thanks for reporting. This isn't related to #1071 except in the sense that it involves carefully. The problem (in NetLogo terms) is that myself is incorrectly set to self within carefully (and the implementation of other-end uses myself). The following example illustrates this problem:
observer> ask turtle 0 [ ask turtle 1 [ carefully [show myself][show error-message] ] ]
(turtle 1): (turtle 1)
observer> ask turtle 0 [ ask turtle 1 [ show myself ] ]
(turtle 1): (turtle 0)
There are no prims that I could find other than myself and other-end which make use of myself, so the problems should be limited to those two.
In terms of NetLogo internals (for working on a fix), the problem is that primitives which use Context.runExclusiveJob and are not agent-based (so carefully, with-local-randomness, and without-interruption) effectively destroy information about myself. Context.myself() tries to retrieve myself based on the agent of the parent context, but the three primitives in question here cause the current parent context to be superseded by their own context which has as its agent the agent calling that primitive. As you may be able to tell from the description. This is a hairy mess to fix, but it should be fixed (and probably pretty soon) because it's obviously a pretty big bug that carefully actually causes code to break.
Thanks as always for the quick and detailed reply :). Unfortunately I don't have any programming background outside of NetLogo myself (and even that is rather recent). So as much as I would like to help with working on a fix I'm unfortunately not qualified in this regard (as you can see from my conjecture above ;) ).
Here is a language test case that could be used to work on this issue, reported by a user on the NetLogo users group. The ms global should be "undefined" because the carefully block should blow up before the set is complete (as the line above does). Instead it gets the value of a patch set somehow and the err value is not reset to "oh-no!". The above comment by Robert lays out the root problem pretty clearly.
MyselfCarefully
globals [ms err]
O> set ms "undefined"
O> set err "undefined"
O> ask one-of patches [ set ms myself ] => ERROR There is no agent for MYSELF to refer to.
O> ask one-of patches [ carefully [set ms myself] [set err "oh-no!"] ]
ms => "undefined"
err => "oh-no!"