REBOL3 tracker
  0.9.12 beta
Ticket #0001529 User: anonymous

Project:



rss
TypeBug Statuscomplete Date16-Mar-2010 23:39
Versionalpha 97 CategoryNative Submitted byLadislav
PlatformAll Severityminor Priorityhigh

Summary 'self is being bound by For, Repeat, Foreach, Map-each and Remove-each
Description This makes it problematic to use the above control functions inside an object spec block, or inside a module spec block, or script (where 'self would otherwise refer to the user context). Basically it would cause a problem for all code that refers to 'self, no matter where it is defined.

This is similar to bug #447 (closures used to do the same thing and now don't).
Example code
>> same? 'self for i 1 1 1 ['self]
== false  ; should be true

Assigned ton/a Fixed inalpha 98 Last Update5-Nov-2010 22:46


Comments
(0002119)
BrianH
25-Mar-2010 07:06

From Steeve in AltME and the blog:

Perhaps the usefulness of having self in FOR loops has been underestimated.
See that counter-example. - How to construct a block of objects from serialized values, in a nutshell.

>> map-each [a b][1 2 3 4 5 6][copy self]
== [make object! [
a: 1
b: 2
] make object! [
a: 3
b: 4
] make object! [
a: 5
b: 6
]]
(0002120)
BrianH
25-Mar-2010 07:07

From Andreas in AltME (abolka here):

Steeve, while this may be considered a nice trick, I don't think it's worth keeping the nasty self around.
map-each [a b] [1 2 3 4 5 6] [copy bind? 'a] does the same and is more explicit about what you want.
(0002121)
BrianH
25-Mar-2010 07:18

Steeve that is a nice trick. That could come in handy for converting from records that are stored in fixed-length format in a block to save space, to object format for passing along to generalized functions that accept objects.

However, Andreas is right: You don't need the 'self trick to do that, you can always use BIND?, especially since all loop contexts have at least one field other than the hidden 'self. And as cool as that trick is, it is unlikely to be as common as using 'self to refer to the outer object, module or script context.
(0002125)
Ladislav
25-Mar-2010 08:40

one more example in hope, that it will be understandable enough. First, the description in words: we define a function obtaining a block of code, which uses e.g. a Repeat loop. During the loop, the Block argument shall be executed too, but bound to the loop context, to be able to use the loop variable (s). The code looks as follows:

a-function: func [block [block!]] [
repeat i 1 [do-in-context block 'i]
]
do-in-context: func [block [block!] context [word! object!]] [do bind block context]

I hope, that everyone understands, that this may well be a part of a real-world application, but my example is minimized to contain only the constructs that illustrate the problem. Now, in R2 the block will be executed "in the right context", being able to use the cycle variable 'i. In R3, the block is executed "in the wrong context", since the 'self variable will be bound, which was unsolicited.

Generally, I consider 'self binding as unsolicited for purposes like: loop constructs, functions, closures, use, etc. For a similar opinion, see also #447
(0002131)
BrianH
25-Mar-2010 21:20

Ladislav, R2's BIND also binds the hidden 'self field in code blocks (less hidden in R2 than R3, but still). Loop functions are a separate issue.
(0002789)
Ladislav
4-Nov-2010 16:52

in the core-tests suite

Date User Field Action Change
5-Nov-2010 22:46 BrianH Status Modified tested => complete
4-Nov-2010 16:52 Ladislav Comment : 0002789 Added -
6-May-2010 08:23 BrianH Status Modified built => tested
1-May-2010 04:04 carl Status Modified reviewed => built
1-May-2010 04:04 carl Fixedin Modified => alpha 98
30-Mar-2010 11:02 Ladislav Severity Modified major => minor
30-Mar-2010 11:00 Ladislav Severity Modified minor => major
29-Mar-2010 00:20 Ladislav Description Modified -
29-Mar-2010 00:09 Ladislav Comment : 0002138 Removed -
28-Mar-2010 23:33 Ladislav Comment : 0002138 Added -
25-Mar-2010 21:22 BrianH Comment : 0002131 Modified -
25-Mar-2010 21:20 BrianH Comment : 0002131 Added -
25-Mar-2010 08:46 Ladislav Comment : 0002125 Modified -
25-Mar-2010 08:42 Ladislav Comment : 0002125 Modified -
25-Mar-2010 08:40 Ladislav Comment : 0002125 Added -
25-Mar-2010 07:18 BrianH Comment : 0002121 Added -
25-Mar-2010 07:17 BrianH Code Modified -
25-Mar-2010 07:17 BrianH Description Modified -
25-Mar-2010 07:07 BrianH Comment : 0002120 Added -
25-Mar-2010 07:06 BrianH Comment : 0002119 Added -
22-Mar-2010 23:22 BrianH Priority Modified normal => high
22-Mar-2010 23:22 BrianH Status Modified submitted => reviewed
22-Mar-2010 23:22 BrianH Category Modified Unspecified => Native
22-Mar-2010 23:22 BrianH Code Modified -
22-Mar-2010 23:22 BrianH Description Modified -
16-Mar-2010 23:39 Ladislav Ticket Added -