Skip to content

Universal collection syntax #845

@dvdvgt

Description

@dvdvgt

While writing tests for the newly added array function in the stdlib (#839), I have found myself using the array::fromList function quite often. We also have briefly discussed this in one of the previous Effekt meetings and here; what if we had a shared syntax for creating collections?

Idea

We re-use the emit effect:

array#[1, 2, 3]

is converted to

collectArray {
  do emit(1)
  do emit(2)
  do emit(3)
}

Note that collectArray and other functions for collecting (ha!) collections are already implemented in stream.effekt
such this would merely be a change in the parser similar to #743.

Of course, this would also work for bytearray:

bytes#[1.toByte, 2.toByte, 3.toByte]

is converted to

collectBytes {
  do emit(1.toByte)
  do emit(2.toByte)
  do emit(3.toByte)
}

(We should probably rename collectBytes to just bytes and collectArray to array for consistency.)

Caveats

  • Possible performance & overhead penalty.
  • Possibly confusing error messages similar to Add basic string interpolation / string templates #743.
  • The syntax does not work for maps, sets and other collections that need a comparison function. I can see this changing if and once Effekt has type classes.

Benefits

  • Nice and unique usage of effects!
  • Better ergonomics

To Discuss

  • Weigh pro and cons
  • Decide on syntax

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions