Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: crystal-lang/crystal
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 5b32e46bcc82
Choose a base ref
...
head repository: crystal-lang/crystal
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 3e1abe3bb325
Choose a head ref
  • 3 commits
  • 2 files changed
  • 3 contributors

Commits on Mar 10, 2017

  1. Calling an NamedTuple#to_h now generates a meaningful error message

    karlseguin authored and Brian J. Cardiff committed Mar 10, 2017
    Copy the full SHA
    abe4f80 View commit details
  2. Add spec to to_h of empty tuples

    Brian J. Cardiff committed Mar 10, 2017
    Copy the full SHA
    fe13cbc View commit details
  3. Merge pull request #4076 from karlseguin/feature/empty_named_tuple_to_h

    Calling an NamedTuple#to_h on an empty now has meaningful error message
    bcardiff authored Mar 10, 2017
    Copy the full SHA
    3e1abe3 View commit details
Showing with 12 additions and 1 deletion.
  1. +9 −0 spec/compiler/semantic/named_tuple_spec.cr
  2. +3 −1 src/named_tuple.cr
9 changes: 9 additions & 0 deletions spec/compiler/semantic/named_tuple_spec.cr
Original file line number Diff line number Diff line change
@@ -285,4 +285,13 @@ describe "Semantic: named tuples" do
a[0]
)) { named_tuple_of({"x": types["Foo"].virtual_type!, "y": types["Foo"].virtual_type!}) }
end

it "does not compile to_h of empty tuples" do
# TODO change the location of this spec upon #2391
assert_error %(
require "prelude"
NamedTuple.new.to_h
),
"Can't convert an empty NamedTuple to a Hash"
end
end
4 changes: 3 additions & 1 deletion src/named_tuple.cr
Original file line number Diff line number Diff line change
@@ -399,7 +399,9 @@ struct NamedTuple
# tuple.to_h # => {:name => "Crystal", :year => 2011}
# ```
def to_h
{% begin %}
{% if T.size == 0 %}
{% raise "Can't convert an empty NamedTuple to a Hash" %}
{% else %}
{
{% for key in T %}
{{key.symbolize}} => self[{{key.symbolize}}].clone,