Re:Pointer Free Programming and the Future of Nim
by cwzwarich on Friday October 20, 2017 @06:38AM
(#15514760)
An Option type in Rust forces a particular memory layout by requiring the components to be contiguous, which might not be what you want if you are doing low-level optimizations of data layout.
Re:Pointer Free Programming and the Future of Nim
by dbaupp on Friday October 20, 2017 @11:44AM
(#15516638)
That's true, and does mean one has to drop down to C/C++-style pointers and manual layout when the default enums don't work. However, that's somewhat orthogonal to the semantics about what they can express, especially for function arguments where the value isn't being stored in memory. One common approach is for an enum to be teased apart to be stored, and then rematerialised at the API surface (e.g. HashMap and it semantically storing an Option<(K, V)>).
Re:Pointer Free Programming and the Future of Nim
by jjnoakes on Friday October 20, 2017 @11:13AM
(#15516408)
So put a ref or a box in your optional.
Re:Pointer Free Programming and the Future of Nim
by dbaupp on Friday October 20, 2017 @12:01PM
(#15516808)
I don't think adding indirection is what the parent was thinking of. It's more like storing the Option<T> Some vs. None bit in its own separate bitvector along with a packed vector of Ts. This layout, due to padding, may be significantly less memory than alternating bool, T like a vector of Option<T>s gives, but it still retains properties like contiguous layout and minimal allocation/indirection.
Alternatively, since isDriving only makes sense when there's a train, t could be Option<(bool, Train)>, i.e. an optional train along with a boolean for if it's being driven or not.