-
Notifications
You must be signed in to change notification settings - Fork 84
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
Destructuring #81
Comments
It looks like contrary to Nix there's no exhaustiveness check in the pattern-matching of records (in the sense that |
Indeed. This is deliberate, as in the JavaScript syntax. I guess the question is: is destructuring currently used in Nix and intended to be used in Nickel as just a convenient syntax for extracting some fields from a (probably bigger) record, or is it rather a form of pattern matching and actually used for checking the structure of the matched record. In the former case, the exhaustiveness check is probably an annoyance at best. In the latter, an exhaustiveness check is indeed desirable, and partial matching could probably have the same syntax as in Nix: |
I'm in favour of |
I'm not convinced a language that's supposed to be minimalistic should have this (at least not in version 1.0). Even the minimal destructuring supported by Nix isn't really necessary. |
Even the minimal destructuring supported by Nix isn't really necessary.
I beg to differ, I use it all the time, to get a basic amount of error
locality.
…On Fri, Jun 19, 2020 at 3:21 PM Eelco Dolstra ***@***.***> wrote:
I'm not convinced a language that's supposed to be minimalistic should
have this (at least not in version 1.0). Even the minimal destructuring
supported by Nix isn't really necessary.
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#81 (comment)>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAYB5ZUW7DZU77YULKEKCJDRXNQ3XANCNFSM4NPZ6PKQ>
.
|
Records and arrays are the two ubiquitous and fundamental data structures of a configuration language, be it Nix expressions, Jsonnet, Cue, or Nickel. It is useful in practice to be able to easily extract a few specific fields from a record (which may be located deep inside), or a few elements at specific positions from a list. To this end, we propose to add destructuring to Nickel, a syntax that allows to directly extract sub-elements from records or lists by extending let-bindings to bind not only variables, but patterns.
There are good examples out there to take inspiration from, such as pattern matching (restricted to records and lists) in Haskell or OCaml, or the closest to the following proposal, destructuring in Javascript.
Nix expressions also offer destructuring facilities:
let inherit
, which can be confused with its twin{ inherit ...; }
, and cannot specify default values for missing fieldsBoth cannot handle nested records nor bind a field to a variable with a different name. Lists cannot be destructured either. We intend to overcome these limitations in Nickel.
In the following, we provide a concrete proposal, as a starting point for discussion.
Motivating examples
We would like to write this kind of expressions:
Description
We propose JavaScript-like destructuring, with the
@
from Nix for capturing the whole match, featuring:single pattern
...foo
@foo
_
pattern in a list to ignore an elementSyntax
let-binding ::= let letpat = e in e
fun-pat ::= fun letpat1 ... letpatn => e
let-pat ::= pat | pat @ var
pat ::= var | { recpat1; ...; recpatn; rest } | [lstpat1, ..., lstpatn, rest]
rest ::= ε | ...var
recpat ::= fldpat | fldpat ? e
fldpat ::= field | field = pat
lstpat ::= pat | pat ? e | _
Here e is an arbitrary Nickel expression, field is a field identifier, var is a variable identifier, and ε means "empty" or "absent".
Semantics
in let { Q } = e -$ "field" in e'
in let [Q] = tail l in e'
Q denotes the queue of a sequence of declarations, and can be empty. The -$ operator removes a field from a record.
Remarks
We proposed a rich set of features, for completeness and consistency. Even if they do not cost much to add, some are of questionable value, and could be considered for removal or restriction. Notably:
...rest
syntax is handy for popping a few values from a list and get back the queue in one match, but what it adds for records patterns is not clearThe text was updated successfully, but these errors were encountered: