From 2bb6a78f79446a93c52c85936ee2941ea4b1816c Mon Sep 17 00:00:00 2001 From: Jan Vrany Date: Thu, 8 Feb 2024 21:53:08 +0000 Subject: [PATCH] =?UTF-8?q?Allow=20to=20notate=20`TypedSlot`=20using=20`te?= =?UTF-8?q?rm=20=E2=88=B7=20type`=20notation?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This together with `TypingJudgement >> asSlot` allows to use `term ∷ type` notation when defining typed instance variables and elements of future class `Record`. --- .../TypingJudgement.extension.st | 10 ++++++++ src/MathNotation/TypedSlot.extension.st | 25 +++++++++++++++++++ 2 files changed, 35 insertions(+) create mode 100644 src/Collections-Homogeneous/TypingJudgement.extension.st create mode 100644 src/MathNotation/TypedSlot.extension.st diff --git a/src/Collections-Homogeneous/TypingJudgement.extension.st b/src/Collections-Homogeneous/TypingJudgement.extension.st new file mode 100644 index 000000000..c07d532bc --- /dev/null +++ b/src/Collections-Homogeneous/TypingJudgement.extension.st @@ -0,0 +1,10 @@ +Extension { #name : #TypingJudgement } + +{ #category : #'*Collections-Homogeneous' } +TypingJudgement >> asSlot [ + term isString ifFalse:[ + self error:'term must be a string or symbol'. + ^nil. + ]. + ^TypedSlot named: term asSymbol type: type +] diff --git a/src/MathNotation/TypedSlot.extension.st b/src/MathNotation/TypedSlot.extension.st new file mode 100644 index 000000000..6c224ae1b --- /dev/null +++ b/src/MathNotation/TypedSlot.extension.st @@ -0,0 +1,25 @@ +Extension { #name : #TypedSlot } + +{ #category : #'*MathNotation' } +TypedSlot >> definitionString [ + | scanner typeString | + + "Try to put parenthesis around term and type + when needed. + " + scanner := RBScanner new initializeClassificationTable. + + ^String streamContents: [ :aStream| + name storeOn: aStream. + aStream nextPutAll: ' ∷ '. + + typeString := type printString. + (typeString anySatisfy:[:char | | charCls | (charCls := scanner classify: char) ~~ #alphabetic and:[ charCls ~~ #digit ]]) ifTrue:[ + aStream nextPut:$(. + type printOn: aStream. + aStream nextPut:$). + ] ifFalse:[ + type printOn: aStream + ]. + ] +]