abstraction barrier for EMPTY_LIST; storage type conflict
The static constants
Submitted by Sebastian Erdweg on 18 March 2011 at 11:47AbstractTermFactory.EMPTY_LIST
andTermFactory.EMPTY_LIST
should be declaredprivate
because their use potentially contradicts the value ofdefaultStorageType
that would be used when callingTermFactory.makeList()
. In particular, building an application with anEMPTY_LIST
argument fails inParentTermFactory
because 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_LIST
is 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_LIST
field instead. The cases whereEMPTY_LIST
is 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
ParentTermFactory
andOriginTermFactory
should just handleEMPTY_LIST
as 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_LIST
need 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_parents
primitive, 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
OriginTermFactory
is always active. For theOriginTermFactory
I 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