Strict Symbol Comparison for agents developing in GT

GT can run with strict String/Symbol comparison (see Strict Symbol Comparison - Enabling). Then 'abc' = #abc and #abc = 'abc' are both false, as in GemStone. Everything that compares with = is affected: includes:, indexOf:, remove:, detect: [ :each | each = x ], and the keys of a Dictionary or Set, where 'abc' and #abc become two different keys. Code you write must give the same answers in both modes: Symbol strictComparison tells which mode is on.

Know the kind of every name you compare.

  • Symbols: class names, selectors, instance and class variable names, slot names, temporary and argument names in the AST, protocol names, package and tag names, pragma selectors, Symbol literals.
  • Strings: text from files, JSON, STON written as JSON, HTTP, git and the command line; text typed by users; printString, asString, displayString; copyFrom:to:, allButFirst, allButLast, asLowercase, the keywords of a Symbol; the name of a metaclass ('Foo class'); path segments.
  • aSymbol , 'x' is a Symbol: #Foo , ' is not a class' is a Symbol message.
  • Decoded JSON has String keys: look it up with String keys.

When the kinds can differ, fix it in this order:

  1. Give the names one kind where they enter: asSymbol for names of code entities, asString for text keys.
  2. Use a collection that treats a String and a Symbol with the same characters as the same key: GtStringContentDictionaryDictionary << #GtStringContentDictionary slots: {}; tag: 'Collections'; package: 'GToolkit-PharoBasePatch-StrictSymbol', GtStringContentSetSet << #GtStringContentSet slots: {}; tag: 'Collections'; package: 'GToolkit-PharoBasePatch-StrictSymbol', GtStringContentOrderedDictionaryOrderedDictionary << #GtStringContentOrderedDictionary slots: {}; tag: 'Collections'; package: 'GToolkit-PharoBasePatch-StrictSymbol'.
  3. Compare by content: sameContentAs:, includesSameContentAs:.

Normal mode must stay identical: the same answers, the same objects stored, the same duplicates. If converting where a name enters would change what normal mode stores, compare by content instead. Never use asString or asSymbol on both sides of every comparison as a reflex.

Pharo code.

Pharo methods that GT relies on are fixed by the package GToolkit-PharoBasePatch-StrictSymbol-Overrides (each method is marked <gtPharoPatch: #StrictComparison>). Prefer a fix in the GT caller. A new override must keep normal mode identical, have an example in GtStrictSymbolPharoOverridesExamples Object << #GtStrictSymbolPharoOverridesExamples traits: {TAssertable}; slots: {}; tag: 'Examples'; package: 'GToolkit-Pharo-StrictSymbolComparison' , and may only override methods of repositories loaded before the GT prerequisites (an override of a later repository shows that repository as modified).

Examples and checks.

  • Build expected values with the kind the code answers: assert: aName equals: #foo fails in strict mode when the code answers 'foo'.
  • Setting the flag in an image where strict comparison is not installed does not change =. Check strict behaviour in a copy of the image where GtStrictSymbolInstallation new enableStrictSymbolComparison was run, not in the image you work in.
  • GtStrictSymbolComparisonFlag>>#useStrictComparison:during:useStrictComparison: aBoolean during: aBlock "Evaluate aBlock with the strict comparison flag set to aBoolean, then restore the previous value" | aPreviousValue | aPreviousValue := UseStrictComparison. UseStrictComparison := aBoolean. ^ aBlock ensure: [ UseStrictComparison := aPreviousValue ] runs a block in one mode and restores the flag.
  • Remote runner workers compare strictly when their runner does.