Ahoj, Petre,

in your previous post you suggested the KISS strategy.
All, that I can offer, is a ASAICFO (As Simple As I Can Figure Out)
strategy.

Let's start with it:

1) meet the notion of Side-Effect Creating Function (or, Function Modifying
It's Argument, if you prefer)

modify: func [blk [string!]] [
    ; the following row is causing the side-effect
    append blk "x"
]

;Example use:
a: make string! 0
modify a
a ;this is the side-effect:

The results are here:
>> ;Example use:
>> a: make string! 0
== ""
>> modify a
== "x"
>> a ;this is the side-effect:
== "x"

The former style of programming is considered as non-advisable by some
(purists?). Russell suggested something different:

don't-modify: func [blk [string!]] [
    ; the following row is not causing the side-effect, although Append
normally is
    append copy blk "x"
]

;Example use:
a: make string! 0
don't-modify a
a ;no side-effect observed:

>> a: make string! 0
== ""
>> don't-modify a
== "x"
>> a ;no side-effect observed:
== ""

2) Meet the notion of Self-Modifying Code

smf: func [] [
    modify ""
    source smf
]

>> smf
smf: func [][
    modify "x"
    source smf
]

Now some morals:

The use of self-modifying code is not considered as  advisable (not by me,
it's an old rule). Every programmer should take care, if his code is clean
from such effects.

How do you recognize a self-modifying code? It contains e.g. something like:

modify "anything"

or, more indirectly,

a: "anything"
modify a

This is hard (no KISS). If you use or create Side-Effect Creating Functions,
there is no simple instruction how to recognize the Self-Modifying Code.

3) Two examples as a goodbye:

; warning, Self-Modifying Code, not advisable!

counting-func: func [/local a counter] [
    a: "-1"
    counter: 1 + to-integer a
    ;warning, the code modification in the next two rows!
    clear a
    append a to-string counter
]

;Example use:
counting-func
counting-func
counting-func
counting-func
counting-func
counting-func
counting-func
counting-func

Results:
>> counting-func
== "0"
>> counting-func
== "1"
>> counting-func
== "2"
>> counting-func
== "3"
>> counting-func
== "4"
>> counting-func
== "5"
>> counting-func
== "6"
>> counting-func
== "7"

The non Self-Modifying code:

block: [
    a: "-1"
    counting-func: func [/local counter] [
        counter: 1 + to-integer a
        a: to-string counter
    ]
]
do block

;Example use:
counting-func
counting-func
counting-func
counting-func
counting-func
counting-func
counting-func
counting-func

Results:
>> counting-func
== "0"
>> counting-func
== "1"
>> counting-func
== "2"
>> counting-func
== "3"
>> counting-func
== "4"
>> counting-func
== "5"
>> counting-func
== "6"
>> counting-func
== "7"

How can I be so sure that this code isn't Self-Modifying? I am not. By can
make sure:

>> source block
block: [
    a: "-1"
    counting-func: func [/local counter] [
        counter: 1 + to-integer a
        a: to-string counter
    ]
]

So, it looks good. Until we don't use Print:

>> print block
-1 ?function?

So, we can see, that even in this case block got modified. Who is guilty?
(Do).

Howgh

Ladislav

> Hi,
>
> reading all the emails in the "logical" value referencing thread, I
> always learn something new. After reading each email, I always think -
> heh, finally conclusion came out and everythink seems to be explained
> now, but then another email arrives to the list and there is something
> new to learn in it.
>
> What about taking all the experience of what was said, tested, and write
> any guide from the info available? It would even be worth buying for
> some $X ...
>
> Just a thought ...
>
> See you ...
>
> -pekr-
>
>

Reply via email to