Login  Register

Proposal for LiteralArray class

Posted by Ben Coman on Feb 01, 2015; 9:48am
URL: https://forum.world.st/Proposal-for-LiteralArray-class-tp4803015.html


Looking into Image locking problems [1] caused by a recursive array such as this...

    literalArray := #( 1 2 3 ).
    literalArray at: 3 put: literalArray.

I find that "literalArray printString" locks the image due to Array>>printOn: use of the recursive #shouldBePrintedAsLiteral method. Now its implementation is identical to #isLiteral and indeed "literalArray isLiteral" also locks the Image. So comparing implementors of #isLiteral...  

  Object>>isLiteral   ^false
  Boolean>>isLiteral ^true
  Character>>isLiteral ^true
  Integer>>isLiteral ^true
  String>>isLiteral ^true
  UndefinedObject>>isLiteral ^true

  ByteArray>>isLiteral ^self class == ByteArray
  Float>>isLiteral ^self isFinite "^(self - self) = 0.0"
  ScaledDecimal>>isLiteral ^denominator = 1 or: [(10 raisedTo: scale)\\denominator = 0]

  Array>>isLiteral ^self class == Array and: [self allSatisfy: [:each | each isLiteral]]

...I find most are very basic (might even say deterministic), with the recursion of Array>>isLiteral seeming an annomaly.  Also, the big IF condition in Array>>printOn: smells like a design decision being made at runtime (Valloud AMCOS p12).  

    Array>>printOn: aStream
self shouldBePrintedAsLiteral ifTrue: [self printAsLiteralFormOn: aStream. ^ self].
self isSelfEvaluating ifTrue: [self printAsSelfEvaluatingFormOn: aStream. ^ self].
super printOn: aStream

Flipping between two printString formats seems like selecting between two class types. Indeed, if we had a LiteralArray class, there would be no need for its printOn: to recursively search to determine its form, thus allowing #printStringLimitedTo: to do its thing to protect against infinite recursion.

Also, instead of a recursive Array>>isLiteral we'd have something like
  LiteralArray>>isLiteral ^true
  Array>>isLiteral ^false
which seems to align much better with the pattern of the other #isLiteral implementors.

I notice there is both RBArrayNode and RBLiteralArrayNode.

So what are the wider concerns that might apply?
(In particular, I'm not sure how the #isSelfEvaluating (which is also recursive) fits into the big picture)

cheers -ben

[1] https://www.mail-archive.com/pharo-dev@.../msg25156.html