Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
szabgab committed Jul 10, 2021
1 parent 5d82e89 commit 4b93d3a
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
12 changes: 12 additions & 0 deletions crystal/examples/tuples/named_tuple_with_non_alnum_fields.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
macro t(name)
print "%s %s\n" % {typeof({{name}}), {{name}}}
end

h = {a: 1, "a b-23": 2}
t h
h.each {|x, y|
t x
t y
}


25 changes: 25 additions & 0 deletions crystal/examples/tuples/optional_field.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
require "json"

alias Person = NamedTuple(
name: String,
email: String?,
)

p1_str = %[{"name": "Foo", "email": "[email protected]"}]
p1 = Person.from_json(p1_str)
p p1


p2_str = %[{"name": "Bar"}]
p2 = Person.from_json(p2_str)
# Unhandled exception: Missing json attribute: email
p p2

p3_str = %[{"name": "Bar", "email": null}]
p3 = Person.from_json(p3_str)
p p3

# {name: "Foo", email: "[email protected]"}
# {name: "Bar", email: nil}
# {name: "Bar", email: nil}

10 changes: 10 additions & 0 deletions crystal/tuples.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,13 @@

![](examples/tuples/named_tuple_fields.cr)

## Named Tuple with non-alphanumeric fields
{id: named-tuple-with-non-alphanumeric-fields}

![](examples/tuples/named_tuple_with_non_alnum_fields.cr)

## Named Tuple with optional field
{id: named-tuple-with-optional-field}

![](examples/tuples/optional_field.cr)

0 comments on commit 4b93d3a

Please sign in to comment.