Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

0005 - Literal syntax for collections #22

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions pheps-0005.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
```
PhEP {
#id: 0005,
#type: #image,
#title: 'Literal syntax for collections',
#status: 'proposal',
#authors: [ 'Stéphane Ducasse' ],
#created: '2023-09-17'
}
```

# Abstract

A proposal to be able to write literal collections in the same way we have DynamicArray.
The proposal is based on the idea of Dave Mason to support `{:Set 1 . 2 . 1}` to get a set with 2 elements. This approach works for any collection (assuming that they implement `withAll:`). Also some other objects could benefit from this new notation, e.g. `Point` or `Rectangle`. For this reason, it will be best to define a new selector that will be used by the Brace Litteral syntax: `withAllForBrace:`.

# Changelog

- 2023-09-17: initial document

# Motivation

The following illustrates the point:

```
(Set new add: 1 ; add: (Set new add: 2; add: 2; yourself); add: 1 ;yourself)
```

When scripting we would like to be able to write:

```
{:Set 1 . {:Set 2 . 2} . 1}
```

Similarly people could write

```
{:Dictionary #a -> 33 . #b -> 44 }
```

# Discussion

This design is nice since there is no look-ahead. It is familiar in the sense that it uses : to denote a kind of argument (here it is the class of the collection).

In addition, we do not use another special character or character sequence such as `{{` or `#{` or whatever.

## Possible questions for another iteration

The compiler could check that the class defines the method `withAllForBrace:`.

# Implementation

It is done and minimal: on extra tree node. We should just move the methods of the `RBParserLiteralCollection` to its superclass. We should check the syntax hilighter. The pretty printing has been fixed.

```
testNestedLiteralSet

| compiler |
compiler := OpalCompiler new.
compiler compilationContext parserClass: RBParserLiteralCollection.
self assert: (compiler evaluate:
'{ :Set 1 . { :Set 2 . 2 } . 1}' )

equals: (Set new add: 1 ; add: (Set new add: 2; add: 2; yourself); add: 1 ;yourself).
```

See Pull Request https://github.com/pharo-project/pharo/pull/14845