Also the direct approach of matching and using ? looks cleaner in Rust and formats better than using monadic combinators. Sometimes they do help, but I just tend to use them more sparingly these days.
Conversation
Thanks! The bit w.r.t. using `match` to avoid `**` is nice. W.r.t. some of the others I started writing it that way then thought better of it, or couldn't resist golfing :)
1
1
(W.r.t. variants I think fully-qualified is definitely better for a serious codebase, but for gists like this it's just noise. Either that or I'm too used to Haskell atm.)
1
Ah, I found another way: writing e.g. `Term::clone(&term)` and then it's both clear which thing I'm expecting to clone, and deref coercions can kick in so I don't need to count out the *s.
1
2
So I finished it up: play.rust-lang.org/?gist=a4561e78 (did not attempt to make it super-pretty)
Started with Box<Fn> just to get the shape of the code right before I start messing with lifetimes, then changed to Rc just so I could clone, and then... it... just... worked?
2
1
Or your concern was RC cycles rather than whether it'd compile at all? I'd conjecture there'd only be cycles if the object code itself has cyclic references. If you need to support those, what about pulling in Manish's Gc<T>? 👼
2
1
Yeah, once you started having fixpoints it might get a little more troublesome.... I dunno. 🤷♂️
1
1
(What kind of fixpoints / what for? Maybe you could use the Y combinator which IIRC is self-application without cycles in the heap?)
1
1
As in, recursive types and functions. At the moment I don't allow that.
1
I also think you could detect the cycles and set up the strongly-connected components accordingly on the memory/RC side (I don't know if you can do this with plain Rc or you need to extend it?), but this is a long-standing conjecture of mine and I don't know if anyone's done it.
1
1
Replying to
Yeah, I've considered this too. Rc has a Weak version to that I could use. if I went down this route.
Replying to
I'm not sure that Weak would work - it's not the same thing as a SCC sharing a 'single reference count'. (My reasoning about this is out of cache but that was what I concluded last time iirc.)
1

