In purely functional languages, data is immutable. With reference counting, creating a reference cycle requires changing already created data. It seems like purely functional languages could use reference counting without worrying about the possibility of cycles. Am is right? If so, why don’t they?
I understand that reference counting is slower than GC in many cases, but at least it reduces pause times. It would be nice to have the option to use reference counting in cases where pause times are bad.
Your question is based on a faulty assumption. It’s perfectly possible to have circular references and immutable data. Consider the following C# example which uses immutable data to create a circular reference.
This type of trick can be done in many functional languages and hence any collection mechanism must deal with the possibility of circular references. I’m not saying a ref counting mechanism is impossible with a circular reference, just that it must be dealt with.
Edit by ephemient
In response to the comment… this is trivial in Haskell
and barely any more effort in SML.
No mutation required.