On Wed, Mar 3, 2021 at 1:38 PM tbp1...@gmail.com <tbp100...@gmail.com>
wrote:

Oops, I see that I should ave written about a special value for p.v, not
> for p.
>

The special value is None :-) I want to emphasize the following important
points:

1. Leo *should* crash when any script, including Leo's core, tries to
access p.v.whatever when p.v is None.

Imo, it would be a serious blunder to set p.v to anything other than None
in p.moveToParent and p.moveToNthChild. We want Leo to crash *as soon as
possible.* Setting p.v to anything other than None would make debugging
*harder* by delaying the point at which the problems arise. In effect,
setting p.v to anything other than None would put a time bomb in Leo.

2. There is *nothing* wrong with the present code that should be "fixed".
The *only* question is how to keep mypy happy.

3. leojs works exactly the same way. Here are the relevant parts of the
FĂ©lix's code:

/**
 * Create a new position with the given childIndex and parent stack.
 */
constructor(v: VNode, childIndex: number = 0, stack?: StackEntry[]) {
    this._childIndex = childIndex;
    this.v = v;
    if (stack) {
        this.stack = [...stack]; // Creating a copy here is safest and best.
    } else {
        this.stack = [];
    }
}

/**
 * Return True if a position is valid.
 *
 * The tests 'if p' or 'if not p' are the _only_ correct ways to test
 * whether a position p is valid.
 *
 * Tests like 'if p is None' or 'if p is not None' will not work properly.
 */
public __bool__():boolean {
    return !!this.v;
}

/**
 * Move to Nth child
 */
public moveToNthChild(n:number):Position {
    const p:Position = this;
    if (p.v && p.v.children.length > n){
        p.stack.push([p.v, p._childIndex]);
        p.v = p.v.children[n];
        p._childIndex = n;
    }else{
        // * For now, use undefined p.v to signal null/invalid positions
                    //@ts-ignore
        p.v = undefined;
    }
    return p;
}

*Summary*

Leo's devs must not become confused by type checking issues. Setting p.v to
None is the best way to indicate an invalid position.

Edward

-- 
You received this message because you are subscribed to the Google Groups 
"leo-editor" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to leo-editor+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/leo-editor/CAMF8tS315iO3hqxjcYYFZ9%3D6sUbiJWdXLdWWN-uKj3PuKJHqMg%40mail.gmail.com.

Reply via email to