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

Convenient syntax for "list of type" #120

Open
jobarr-amzn opened this issue May 10, 2023 · 3 comments
Open

Convenient syntax for "list of type" #120

jobarr-amzn opened this issue May 10, 2023 · 3 comments
Labels
enhancement New feature or enhancement for the Ion Schema _specification_ requires new version Something that should be considered for next version of the Ion Schema Specification

Comments

@jobarr-amzn
Copy link
Contributor

jobarr-amzn commented May 10, 2023

When working with ISL recently I had several occasions where I had to define a list of a given type of things.
E.g.:

type::{
    name: TodoList,
    type: { 
        type: list,
        element: text,
    },
}

I can define my own list types, but this grows cumbersome as types multiply:

type::{
    name: texts,
    type: list,
    element: text,
}

type::{
    name: TodoList,
    type: texts,
}

It would be more convenient if there was an easy way to express "list of something", e.g. something like:

type::{
    name: TodoList,
    type: list_of::text,
}

The same syntactic sugar could exist for other container types as appropriate, and should be usable in any context where a type might be expected.

@jobarr-amzn jobarr-amzn added the enhancement New feature or enhancement for the Ion Schema _specification_ label May 10, 2023
@popematt popematt added the requires new version Something that should be considered for next version of the Ion Schema Specification label May 10, 2023
@popematt
Copy link
Contributor

Another possible solution that is more succinct, but also more specialized (and therefore limited in applicability), would be a syntactical sugar that expands to a union of the type or a list of that type. E.g. Foo and Bar are equivalent:

type::{
  name: Foo,
  type: list_of_or::number,
}

type::{
  name: Bar,
  type: { one_of: [ number, { type: list, element: number } ] },
}

If we want to go ahead with any annotation-based solution, we need to be careful to define how this would interact with $null_or. The easiest choice would be to say that these annotations are mutually exclusive, but if they are not mutually exclusive, does the order of the annotations matter? Consider, for example, $null_or::list_of::number vs list_of::$null_or::number. (What about $null_or::list_of::$null_or::number?)

On the other hand, if we use a S-Exp based syntax, then we can avoid any confusion around annotation ordering. E.g.:

type::{
  name: Foo,
  type: $null_or::(list_of number),
}

type::{
  name: Bar,
  type: (list_of  $null_or::number),
}

@popematt
Copy link
Contributor

We could even take this premise a step further and define a macro system that would allow you to create your own syntactical sugar. For example:

schema_header::{
  macros:[
    (list_of 
      ($type) 
      { type: list, element: $type }),
    (sexp_of 
      ($type) 
      { type: sexp, element: $type }),
  ]
}

But that would complicate things because people might want to import macros (or do other things with them that I can't think of right now), and we'd need to figure out how to handle importing macros and whatever else.

@popematt
Copy link
Contributor

I think that this can be solved by Ion 1.1 macros, but if we release a new version of Ion Schema before that, we can add list_of::<type> that is mutually exclusive with $null_or::.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or enhancement for the Ion Schema _specification_ requires new version Something that should be considered for next version of the Ion Schema Specification
Projects
None yet
Development

No branches or pull requests

2 participants