Skip to content

Commit

Permalink
Change enum tags in the documentation to the new syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
vkleen committed Apr 25, 2023
1 parent 6901750 commit b2bc7f8
Show file tree
Hide file tree
Showing 11 changed files with 48 additions and 49 deletions.
4 changes: 2 additions & 2 deletions RELEASES.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ Breaking changes
- The `Num` builtin type has been renamed to `Number`
- The `Str` builtin type has been renamed to `String`
- The `num` stdlib module has been remaned to `number`
- The `builtin.typeof` function now returns `` `Number ``, `` `String ``,
`` `Function `` instead of respectively `` `Num ``, `` `Str ``, and `` `Fun ``
- The `builtin.typeof` function now returns `'Number`, `'String`, `'Function`
instead of respectively `'Num`, `'Str`, and `'Fun`
- The `builtin.is_num`, `builtin.is_str` and `builtin.to_str` functions hav been
renamed to `is_number`, `is_string` and `to_string`
- The `string.to_num` and `string.from_num` functions have been renamed to
Expand Down
4 changes: 2 additions & 2 deletions benches/mantis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ ncl_bench_group! {
{
name = "mantis",
path = "mantis/run",
args = (r#"{namespace = "mantis-staging", job="miner", role=`miner}"#),
args = (r#"{namespace = "mantis-staging", job="miner", role='miner}"#),
eval_mode = EvalMode::DeepSeq,
}, {
name = "mantis serialize",
path = "mantis/run",
subtest = "serialize",
args = (r#"{namespace = "mantis-staging", job="miner", role=`miner}"#),
args = (r#"{namespace = "mantis-staging", job="miner", role='miner}"#),
}
}
criterion_main!(benches);
4 changes: 2 additions & 2 deletions doc/manual/contracts.md
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ let Schema = {
bar | Number,
}
nickel> let config | Schema = {bar = 2}
nickel> std.serialize `Json config
nickel> std.serialize 'Json config
"{
"bar": 2,
"foo": "foo"
Expand Down Expand Up @@ -410,7 +410,7 @@ let Secure = {
must_be_very_secure | Bool = true,
data | String,
}
nickel> std.serialize `Json ({data = ""} | Secure)
nickel> std.serialize 'Json ({data = ""} | Secure)
"{
"data": "",
"must_be_very_secure": true
Expand Down
4 changes: 2 additions & 2 deletions doc/manual/merging.md
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ A field can be marked as optional using the `optional` annotation:
command
| String,
arg_type
| [| `String, `Number |],
| [| 'String, 'Number |],
alias
| String
| optional,
Expand All @@ -301,7 +301,7 @@ field:
```nickel
{
command = "exit",
arg_type = `String,
arg_type = 'String,
alias = "e",
} | Command
```
Expand Down
43 changes: 21 additions & 22 deletions doc/manual/syntax.md
Original file line number Diff line number Diff line change
Expand Up @@ -283,47 +283,46 @@ The following examples show how symbolic strings are desugared:
```text
> mytag-s%"I'm %{"symbolic"} with %{"fragments"}"%
{
tag = `SymbolicString,
prefix = `mytag
tag = 'SymbolicString,
prefix = 'mytag
fragments = [ "I'm ", "symbolic", " with ", "fragments" ],
}
> let terraform_computed_field = {
tag = `TfComputed,
tag = 'TfComputed,
resource = "foo",
field = "id",
}
> tf-s%"id: %{terraform_computed_field}, port: %{5}"%
{
tag = `SymbolicString
prefix = `tf,
fragments = [ "id: ", { resource = "foo", field = "id", tag = `TfComputed }, ", port: ", 5 ],
tag = 'SymbolicString
prefix = 'tf,
fragments = [ "id: ", { resource = "foo", field = "id", tag = 'TfComputed }, ", port: ", 5 ],
}
```

#### Enum tags

Enumeration tags are used to express a choice among finitely many alternatives.
They are formed by writing a backtick `` ` `` followed by any valid identifier
They are formed by writing a single quote `'` followed by any valid identifier
or by a quoted string. For example, `std.serialize` takes an export format as a
first argument, which is an enum tag among `` `Json ``, `` `Toml `` or `` `Yaml
``:
first argument, which is an enum tag among `'Json`, `'Toml` or `'Yaml`:

```nickel
> std.serialize `Json {foo = 1}
> std.serialize 'Json {foo = 1}
"{
\"foo\": 1
}"
> std.serialize `Toml {foo = 1}
> std.serialize 'Toml {foo = 1}
"foo = 1
"
```

An enum tag `` `foo `` is serialized as the string `"foo"`:
An enum tag `'foo` is serialized as the string `"foo"`:

```nickel
> std.serialize `Json {foo = `bar}
> std.serialize 'Json {foo = 'bar}
"{
\"foo\": \"bar\"
}"
Expand Down Expand Up @@ -674,11 +673,11 @@ Nickel features the following builtin types and type constructors:
represents any value)
- Arrays: `Array <type>` is an array whose elements are of type `<type>`.
- Dictionaries: `{_ : <type>}` is a record whose fields are of type `<type>`.
- Enums: ``[| `tag1, .., `tagn |]`` is an enumeration comprised of the alternatives
`` `tag1 ``, .., `` `tagn``. Tags have the same syntax as identifiers and must
be prefixed with a backtick `` ` ``. Like record fields, they can however be
- Enums: `[| 'tag1, .., 'tagn |]` is an enumeration comprised of the alternatives
`'tag1`, .., `'tagn`. Tags have the same syntax as identifiers and must
be prefixed with a single quote `'`. Like record fields, they can however be
enclosed in double quotes if they contain special characters:
`` `"tag with space" ``.
`'"tag with space"`.
- Arrows: `<source> -> <target>` is a function taking an argument of type
`<source>` and returns values of type `<target>`.
- Foralls: `forall var1 .. varn. <type>` is a polymorphic type quantifying over type
Expand Down Expand Up @@ -711,14 +710,14 @@ Here are some examples of more complicated types in Nickel:
{ foo = [ [ 1 ] ] }
> let select
: forall a. {left: a, right: a} -> [| `left, `right |] -> a
: forall a. {left: a, right: a} -> [| 'left, 'right |] -> a
= fun {left, right} =>
match {
`left => left,
`right => right,
'left => left,
'right => right,
}
in
(select {left = true, right = false} `left) : Bool
(select {left = true, right = false} 'left) : Bool
true
> let add_foo : forall a. {_: a} -> a -> {_: a} = fun dict value =>
Expand Down Expand Up @@ -905,7 +904,7 @@ record is serialized. This includes the output of the `nickel export` command:
> value
{ foo = 1, bar = 2 }
> std.serialize `Json value
> std.serialize 'Json value
"{
"foo": 1
}"
Expand Down
16 changes: 8 additions & 8 deletions doc/manual/typing.md
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ The following type constructors are available:
std.record.map (fun char count => count + 1) occurrences : {_ : Number}
```

- **Enum**: ``[| `tag1, .., `tagn |]``: an enumeration comprised of alternatives
- **Enum**: ``[| 'tag1, .., 'tagn |]``: an enumeration comprised of alternatives
`tag1`, .., `tagn`. An enumeration literal is prefixed with a backtick and
serialized as a string. It is useful to encode finite alternatives. The
advantage over strings is that the typechecker handles them more finely: it is
Expand All @@ -228,11 +228,11 @@ The following type constructors are available:
Example:

```nickel
let protocol : [| `http, `ftp, `sftp |] = `http in
let protocol : [| 'http, 'ftp, 'sftp |] = 'http in
(protocol |> match {
`http => 1,
`ftp => 2,
`sftp => 3
'http => 1,
'ftp => 2,
'sftp => 3
}) : Number
```

Expand Down Expand Up @@ -430,9 +430,9 @@ tail that can be substituted for something else. For example:

```nickel
{
port_of : forall a. [| `http, `ftp; a |] -> Number = match {
`http => 80,
`ftp => 21,
port_of : forall a. [| 'http, 'ftp; a |] -> Number = match {
'http => 80,
'ftp => 21,
_ => 8000,
}
}
Expand Down
4 changes: 2 additions & 2 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ data!

```console
$ nickel -f record-contract.ncl query kind
• contract: [|`ReplicationController, `ReplicaSet, `Pod|]
• contract: [|'ReplicationController, 'ReplicaSet, 'Pod|]
• documentation: The kind of the element being configured.
```

Expand Down Expand Up @@ -76,7 +76,7 @@ nickel>
```text
nickel>let config = import "record-contract.ncl"
nickel>:query config.kind
• contract: [| `ReplicationController, `ReplicaSet, `Pod |]
• contract: [| 'ReplicationController, 'ReplicaSet, 'Pod |]
• documentation: The kind of the element being configured.
nickel>
Expand Down
2 changes: 1 addition & 1 deletion src/parser/uniterm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -649,7 +649,7 @@ pub(super) trait FixTypeVars {
/// forall a. Num -> {foo: Str ; a} -> {bar: Str ; a}
/// # occurs both in record rows and enum rows position
/// # this is inconsistent and will raise a parse error
/// forall a. [| `foo, `bar; a |] -> {foo : Str, bar: Str; a}
/// forall a. [| 'foo, 'bar; a |] -> {foo : Str, bar: Str; a}
/// ```
fn fix_type_vars(&mut self, span: RawSpan) -> Result<(), ParseError> {
self.fix_type_vars_env(BoundVarEnv::new(), span)
Expand Down
2 changes: 1 addition & 1 deletion src/term/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1830,7 +1830,7 @@ pub mod make {
/// Switch for types implementing `Into<Ident>` (for patterns) and `Into<RichTerm>` for the
/// body of each case. Cases are specified as tuple, and the default case (optional) is separated by a `;`:
/// `mk_switch!(format, ("Json", json_case), ("Yaml", yaml_case) ; def)` corresponds to
/// ``switch { `Json => json_case, `Yaml => yaml_case, _ => def} format``.
/// ``match { 'Json => json_case, 'Yaml => yaml_case, _ => def} format``.
#[macro_export]
macro_rules! mk_match {
( $( ($id:expr, $body:expr) ),* ; $default:expr ) => {
Expand Down
2 changes: 1 addition & 1 deletion src/typecheck/operation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub fn get_uop_type(
mk_uty_arrow!(branches.clone(), branches.clone(), branches),
)
}
// Dyn -> [| `Number, `Bool, `String, `Enum, `Function, `Array, `Record, `Label, `Other |]
// Dyn -> [| 'Number, 'Bool, 'String, 'Enum, 'Function, 'Array, 'Record, 'Label, 'Other |]
UnaryOp::Typeof() => (
mk_uniftype::dynamic(),
mk_uty_enum!(
Expand Down
12 changes: 6 additions & 6 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -423,8 +423,8 @@ impl<ERows> EnumRowsF<ERows> {
/// If we put aside the state and the error (see [EnumRowsF::map), this function makes
/// `EnumRowsF` a functor. As hinted by the type signature, this function just maps on
/// "one-level" of recursion, so to speak. Take the instantiated version `EnumRows`, and
/// enum rows of the form ``[| `foo, `bar, `baz |]``. Then, calling `try_map_state(f_erows,
/// state)` on these rows will map `f_erows` onto ``[| `bar, `baz |]``.
/// enum rows of the form ``[| 'foo, 'bar, 'baz |]``. Then, calling `try_map_state(f_erows,
/// state)` on these rows will map `f_erows` onto ``[| 'bar, 'baz |]``.
///
/// Note that `f_erows` is just mapped once. Map isn't a recursive operation. It's however a
/// building block to express recursive operations: as an example, see [RecordRows::traverse].
Expand Down Expand Up @@ -768,15 +768,15 @@ impl EnumRows {
// Otherwise, we build a match with all the tags as cases, which just returns the
// original argument, and a default case that blames.
//
// For example, for an enum type [| `foo, `bar, `baz |], the `case` function looks
// For example, for an enum type [| 'foo, 'bar, 'baz |], the `case` function looks
// like:
//
// ```
// fun l x =>
// match {
// `foo => x,
// `bar => x,
// `baz => x,
// 'foo => x,
// 'bar => x,
// 'baz => x,
// _ => $enum_fail l
// } x
// ```
Expand Down

0 comments on commit b2bc7f8

Please sign in to comment.