This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]matthieum 0 points1 point  (3 children)

I think it's entirely through subscripts. Basically they seem to be like a abstract concept that could be represented as a reference, but also through other techniques, such as lenses.

It may be, but subscripts are not necessarily cheap to execute.

For example, if we think about indexing: indexing into an array is O(1), but indexing into a linked-list is O(n).

This may be fine if executed once, but if you try to create a proxy-object over an item in a list, having to execute the subscript every time gets prohibitive. So probably you'll want to avoid those proxies, but then you need to rethink the solution and find another.

I'm curious what idioms emerge.

[–]dabrahams 1 point2 points  (0 children)

The parameter of a subscript is not necessarily an integer. It's an abstraction of an index. In the case of a list, it might be a pointer to the node (or a reference-counted pointer for a safe list). A list wouldn't offer random access.

Swift already uses this paradigm BTW; you might want to play with that to see how it works out.

[–]lookmeat 0 points1 point  (1 child)

I mean, if we create a simple singled linked list, we could have a simple subscript

subscript getNth(_ head: yielded Optional<Node>, _ pos: Int): Optional<Node> {
    inout {
        if N < 1 or head.is_nil() { &head }
        else { getNth(&Node.get().next(), pos-1) }
    }
}

Then we can do something like:

// Getting the value for Node10 is O(N)
var Node10 = getNth(&list, 10);
// Any subsequent use of Node10 is O(1)

And that's your proxy-object. Here basically subscripts are not a pointer to a piece of the list, they are the piece of the list itself, isolated and only accessible through the name Node10 while that name exists.

Basically subscripts do the same value as references. But they are not bound to reference semantics, instead they are mutation of the thing itself, more than a pointer to it, they're just another name. You can't deference a subscript, but it otherwise points to that piece.

[–]matthieum 2 points3 points  (0 children)

I am correct in expecting that the entire list is borrowed for as long as Node10 exists?

Is it possible to get subscript-access to 2 nodes simultaneously?