Rework Options
Hey there! I stumbled across your project because I also thought of implementing iterators in Go using generics, and I also would have taken influence from Rust. I think you've done a great job so far, however I think I have a more interesting approach to the Option type. It more closely resembles Rust, and it completely avoids the use of nil. I put some long descriptions in the comments, but will try to briefly reexplain here.
Your implementation takes a pointer of type T for the inner value. You simply compare to nil to tell if it's Some or None. There are 3 very very minor caveats to this:
-
nilis still a value that takes memory. For example a slice of the value nil will still set zero values. A None type that is a empty struct will take no space, and basically acts as a type container for the compiler. As proof if you set a slice to anilvalue and reflect it's structure you can see it's values get set to zero values. - Taking a reference to a type makes it a candidate for allocation. This means that storing the value as a pointer of the generic type (
*T) could in some cases make a value allocate on heap when it may not need to, and potentially requires more from the garbage collector. Ultimately what it really boils down to is letting the implementer decide what they want. The typeTcan still contain a pointer to avoid copying, and only the struct around it will be cloned. - As a matter of arbitrary and useless principals I decided something that is used to avoid the pitfalls of
nilvalues shouldn't use or rely on them.
I could be wrong on some of this, but this is my best attempt to make the Option type behave most similarly to Rust's, and there are some potential very small and insignificant improvements with this also. Then again, I may be missing something. Regardless I think it still matches the semantics of Rust better.
Let me know what you think!