Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

For what it's worth, you can pull the same tricks in any language.

I wouldn't like to work with it, but I wouldn't be too surprised if I encountered a List<Optional<WeakReference<Holder<Node>>>>. A list, containing optionally-present weak references to a holder object (which you might need if you intend to update such an object from a lambda, as those can only take effectively final variables from their parent scope).

I think there's value to the Rust implementation. In many other languages, these wrappers are either non-optional or hidden away with syntax, but they're still present. The Rust approach puts the developer in charge of how data should be exchanged (copied/borrowed/on stack/in heap/etc.). You can write extremely efficient programs that stack very few of those types, with many restrictions on how they ca be passed around to methods, or you can make your life easier at the cost of some performance and clutter.

For clarity, you could do something like this:

    type RVec<T> = Vec<Rc<T>>;
    type RNode = RefCell<Node>;
    
    struct Node {
        value: i32,
        children: RVec<RNode>,
        parent: Option<Weak<RNode>>
    }
That would allow for some much-needed brevity, but it would also fill your code with custom types that others will need to learn about.
 help



Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: