In a Phep005 soon available
testLiteralSet
| compiler |
compiler := OpalCompiler new.
compiler compilationContext parserClass: RBParserLiteralCollection.
self assert: (compiler evaluate:
'{ :Set 1 . 2 . 1}' )
equals: #(1 2) asSet.
testOrderedCollection
| compiler |
compiler := OpalCompiler new.
compiler compilationContext parserClass: RBParserLiteralCollection.
self assert: (compiler evaluate:
'{ :OrderedCollection 1 . 2 . 1 . 3}' )
equals: #(1 2 1 3) asOrderedCollection.
testDictionary
| compiler dict |
compiler := OpalCompiler new.
compiler compilationContext parserClass: RBParserLiteralCollection.
dict := (compiler evaluate:
'{ :Dictionary #a -> 33 . #b -> 44}' ).
self assert: (dict at: #a) equals: 33
Cool!
Noury
On Sep 18 2023, at 5:20 pm, stephane ducasse stephane.ducasse@inria.fr wrote:
In a Phep005 soon available
testLiteralSet
| compiler |
compiler := OpalCompiler new.
compiler compilationContext parserClass: RBParserLiteralCollection.
self assert: (compiler evaluate:
'{ :Set 1 . 2 . 1}' )
equals: #(1 2) asSet.
testOrderedCollection
| compiler |
compiler := OpalCompiler new.
compiler compilationContext parserClass: RBParserLiteralCollection.
self assert: (compiler evaluate:
'{ :OrderedCollection 1 . 2 . 1 . 3}' )
equals: #(1 2 1 3) asOrderedCollection.
testDictionary
| compiler dict |
compiler := OpalCompiler new.
compiler compilationContext parserClass: RBParserLiteralCollection.
dict := (compiler evaluate:
'{ :Dictionary #a -> 33 . #b -> 44}' ).
self assert: (dict at: #a) equals: 33
Interesting! I submitted a Phep a while back to implement a compile-time
literal syntax ##() as seen in Dolphin Smalltalk, with literal collections
like this as a major (though not exclusive) use-case. At the time I
couldn't think of a way to do better for arbitrary collections, but this is
a very natural extension of the brace-array syntax—clever use of the
block-argument-esque leading colon. I still like some things about the
flexibility of the compile-time literal approach, but this certainly has it
beat for terseness in the common case!
One question about the implementation—does it reduce straightforwardly to
instantiation and repeated sends of add:
, or a single withAll:
or
similar, or does it depend on knowledge of the underlying collection
implementation? Put another way, would it work with a custom collection
class? What about a class that isn't a descendant of Collection at all, but
understands the right protocol?
On Mon, Sep 18, 2023 at 3:16 PM Noury Bouraqadi bouraqadi@gmail.com wrote:
Cool!
Noury
On Sep 18 2023, at 5:20 pm, stephane ducasse stephane.ducasse@inria.fr
wrote:
In a Phep005 soon available
testLiteralSet
| compiler |
compiler := OpalCompiler new.
compiler compilationContext parserClass: RBParserLiteralCollection.
self assert: (compiler evaluate:
'{ :Set 1 . 2 . 1}' )
equals: #(1 2) asSet.
testOrderedCollection
| compiler |
compiler := OpalCompiler new.
compiler compilationContext parserClass: RBParserLiteralCollection.
self assert: (compiler evaluate:
'{ :OrderedCollection 1 . 2 . 1 . 3}' )
equals: #(1 2 1 3) asOrderedCollection.
testDictionary
| compiler dict |
compiler := OpalCompiler new.
compiler compilationContext parserClass: RBParserLiteralCollection.
dict := (compiler evaluate:
'{ :Dictionary #a -> 33 . #b -> 44}' ).
self assert: (dict at: #a) equals: 33