DEFAULT with PATH!...What Does It *Mean*?

The ability to have random side-effects in paths creates all kinds of nasty potentials.

That is why I made a way to say you only allow paths to contain evaluations if you also ask for an "invariant" representation of the path to be returned...that you could use in place of a path for further assignments.

>> var: 'x

>> path: 'obj/(print "side effect" var)

>> set path 30
** Error: PATH contains GROUP!s, must request /STEPS output (can use #)

>> [value steps]: set path 30
side effect
== 30

>> steps
== @[obj x]

You can then use STEPS instead of the path. e.g. set @[obj x] will act like set 'obj/x. (The reason a BLOCK! is used is because not all datatypes would be legal in paths...a GROUP! could evaluate to anything, like an OBJECT!...which is a perfectly valid thing to set in a MAP! for instance.)

This "Invariant" Form Was Intended For Things Like DEFAULT

The concept was that even if you have evaluations on the left hand side, you will be able to capture where it got the value or absence-of-value from...and then in the event you need to write it back because a default is necessary, you don't have to run the evaluation again:

>> n: 5

>> data.(n: n + 1): default [10]

Internally, DEFAULT can make a GET request of data.(n: n + 1) and get back STEPS with a fixed value for N, like @[data 6]. If it discovers that the value is set to blank and needs defaulting, it can then use the steps instead of the path as the basis for the N is not changed twice.

But Other Things Can Change...

All kinds of changes can happen between the GET and the SET in DEFAULT. What about this?

>> data.(n: n + 1): default [data: ~gone~, 10]

We've prevented running imperative code and getting side effects from the path...but the side effects can come from other places. Anything you do between the GET and the SET can make the "invariant" steps not so invariant.

Let's say DATA was a BLOCK! at the outset. Should the @[...] steps have actually stored a reference to the block instead of the WORD! data, so that changes to the variable couldn't redirect the invariant reference?

That doesn't work for immediate types. Let's say you have:

>> time: 4:00

>> path: 'time.(if true ['hour])

>> [# steps]: set path 12
== 12

>> steps
== @[4:00 hour]

Now you can't use that steps to go back and change the original time, because 4:00 is an immediate value. You need to get at the object the time lives in. You'd wind up with a 3-element steps in that case:

>> steps
== @[make object! [...] time hour]

If Nothing Is Going To Be Perfect... Should We Shortcut It?

Let's say you have a path like:

obj1.obj2.(if true [obj3])

We could say that the "steps" for this path are:

@[obj1 obj2 obj3 obj4 date time]

But is that empirically more desirable than just noticing when we cross the line from non-immediate values into immediate values, and preserving only as much as we need?

@[(make object! [...this is obj4...]) date time]

That's enough to write back to obj4...but it is fully disconnected from the path that was initially being represented. But which is more suitable for the intent of something like DEFAULT?

My Head Hurts :face_with_head_bandage:

...and it's not because of a booster shot. It's because this is crazy, and way past the point of being fun.

What started out as a nice idea for taming GROUP!s in PATH!s becomes a real headache.

So I'm going to leave well enough alone, and say that this does only that. If you want to prune a path to get better invariants, that's on you:

For example:

(obj1.obj2.obj3.obj4).date.time: default [...]

When you do this, then when DEFAULT asks for steps it will get 4 items in the block:

>> steps
== @[(make object! [...]) date time]

It has latched onto the object, and so you can say inside the body of the defaulting code things like obj1: null or obj1.obj2.obj3: [] and the assignment will apply to what it was looking at, because those names aren't in the steps.

1 Like