abstraction barrier for EMPTY_LIST; storage type conflict
The static constants
Submitted by Sebastian Erdweg on 18 March 2011 at 11:47AbstractTermFactory.EMPTY_LISTandTermFactory.EMPTY_LISTshould be declaredprivatebecause their use potentially contradicts the value ofdefaultStorageTypethat would be used when callingTermFactory.makeList(). In particular, building an application with anEMPTY_LISTargument fails inParentTermFactorybecause no attachment can be registered toEMPTY_LIST.
Issue Log
Good point. I think it should be marked as deprecated. Internally, a different private field could be used. For other classes, a call to
makeList()should be used instead.
I am actually experiencing bugs because of this issue, thus I would prefer a more invasive fix then depreciation only.
I’m always hesitant to just break binary compatibility like that. The real problem is with how
EMPTY_LISTis used now. I think all those uses should be reconsidered. Many of them are just harmless comparisons, intended to help performance a bit, they could use some kind of package-internalSHARED_EMPTY_LISTfield instead. The cases whereEMPTY_LISTis actually returned are harmful. One particularly problematic case is probablyStrategoTerm.getAnnotations(). There doesn’t seem to be a nice way to fix that one; it really should be shared. If a term t has an empty list of annotations t’, then what are the annotations of list t’?Perhaps
ParentTermFactoryandOriginTermFactoryshould just handleEMPTY_LISTas a special case, and create a new, unshared object for it? Which also wouldn’t be very nice…
Wouldn’t it also make sense to just generate new non-shared term whenever an attachment is registered to a shared term. Then you have lazy duplication of terms and the existing code is untouched.
Apart from that I agree that the uses of
EMPTY_LISTneed to be reconsidered.
For parent terms it would be prohibitively expensive to clone terms in the general case. Trees are constructed in a bottom-up fashion, that would be a lot of recursive cloning operations of increasingly larger trees. The parent term factory is only used inside the parser, or if you happen to use the
SSL_EXT_clone_and_set_parentsprimitive, so mutably setting parents should be safe as the subterms can never escape before the tree is fully constructed.Origins are a different story: in Spoofax, the
OriginTermFactoryis always active. For theOriginTermFactoryI don’t think it would be a problem to clone small subtrees like[]though, so perhaps that factory should just have agetStorageType()test to see if it should clone a tree before adding the attachment.
Log in to post comments