Skip to content

Commit

Permalink
Add NamedTuple.types to return named tuple of types
Browse files Browse the repository at this point in the history
  • Loading branch information
Brian J. Cardiff committed Sep 15, 2017
1 parent 8a00ab4 commit 8e135da
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
5 changes: 5 additions & 0 deletions spec/std/named_tuple_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -296,4 +296,9 @@ describe "NamedTuple" do
tup = {a: 1, b: 'a'}
tup.values.should eq({1, 'a'})
end

it "does types" do
tuple = {a: 1, b: 'a', c: "hello"}
tuple.class.types.to_s.should eq("{a: Int32, b: Char, c: String}")
end
end
10 changes: 10 additions & 0 deletions src/named_tuple.cr
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,16 @@ struct NamedTuple
hasher
end

# Returns the types of this named tuple type.
#
# ```
# tuple = {a: 1, b: "hello", c: 'x'}
# tuple.class.types # => {a: Int32, b: String, c: Char}
# ```
def self.types
NamedTuple.new(**{{T}})
end

# Same as `to_s`.
def inspect
to_s
Expand Down

0 comments on commit 8e135da

Please sign in to comment.