Skip to content
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

Respect explicitly provided type vars for (Named)Tuple.new #10047

Merged
merged 2 commits into from
Jan 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion spec/std/named_tuple_spec.cr
Original file line number Diff line number Diff line change
@@ -1,8 +1,23 @@
require "spec"

private record NamedTupleSpecObj, x : Int32 do
def_equals @x
end

describe "NamedTuple" do
it "does new" do
it "does NamedTuple.new, without type vars" do
NamedTuple.new(x: 1, y: 2).should eq({x: 1, y: 2})
NamedTuple.new(z: NamedTupleSpecObj.new(10)).should eq({z: NamedTupleSpecObj.new(10)})
end

it "does NamedTuple.new, with type vars" do
NamedTuple(foo: Int32, bar: String).new(foo: 1, bar: "a").should eq({foo: 1, bar: "a"})
NamedTuple(z: NamedTupleSpecObj).new(z: NamedTupleSpecObj.new(10)).should eq({z: NamedTupleSpecObj.new(10)})
typeof(NamedTuple.new).new.should eq(NamedTuple.new)

t = NamedTuple(foo: Int32 | String, bar: Int32 | String).new(foo: 1, bar: "a")
t.should eq({foo: 1, bar: "a"})
t.class.should_not eq(NamedTuple(foo: Int32, bar: String))
end

it "does NamedTuple.from" do
Expand Down
15 changes: 14 additions & 1 deletion spec/std/tuple_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ private class TupleSpecObj
def clone
TupleSpecObj.new(@x)
end

def_equals @x
end

describe "Tuple" do
Expand Down Expand Up @@ -154,9 +156,20 @@ describe "Tuple" do
u[1].should_not be(r2)
end

it "does Tuple.new" do
it "does Tuple.new, without type vars" do
Tuple.new(1, 2, 3).should eq({1, 2, 3})
Tuple.new([1, 2, 3]).should eq({[1, 2, 3]})
Tuple.new(TupleSpecObj.new(10)).should eq({TupleSpecObj.new(10)})
end

it "does Tuple.new, with type vars" do
Tuple(Int32, String).new(1, "a").should eq({1, "a"})
Tuple(TupleSpecObj).new(TupleSpecObj.new(10)).should eq({TupleSpecObj.new(10)})
typeof(Tuple.new).new.should eq(Tuple.new)

t = Tuple(Int32 | String, Int32 | String).new(1, "a")
t.should eq({1, "a"})
t.class.should_not eq(Tuple(Int32, String))
end

it "does Tuple.from" do
Expand Down
20 changes: 19 additions & 1 deletion src/named_tuple.cr
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,25 @@ struct NamedTuple
# {} # syntax error
# ```
def self.new(**options : **T)
options
{% if @type.name(generic_args: false) == "NamedTuple" %}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note to self: it would be nice to know whether a type is an uninstantiated generic using a macro call.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By the way, this works slightly differently from the Slice.[] example because it's not possible to attach generic type vars to the macro call directly; they can only be attached via aliases (i.e. Bytes[...] is legal while Slice(UInt8)[...] isn't).

# deduced type vars
options
{% elsif @type.name(generic_args: false) == "NamedTuple()" %}
# special case: empty named tuple
options
{% else %}
# explicitly provided type vars
{% begin %}
{
{% for key in T %}
{{ key.stringify }}: options[{{ key.symbolize }}].as(typeof(begin
x = uninitialized self
x[{{ key.symbolize }}]
end)),
{% end %}
}
{% end %}
{% end %}
end

# Creates a named tuple from the given hash, with elements casted to the given types.
Expand Down
20 changes: 19 additions & 1 deletion src/tuple.cr
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,25 @@ struct Tuple
# {} # syntax error
# ```
def self.new(*args : *T)
args
{% if @type.name(generic_args: false) == "Tuple" %}
# deduced type vars
args
{% elsif @type.name(generic_args: false) == "Tuple()" %}
# special case: empty tuple
args
{% else %}
# explicitly provided type vars
{% begin %}
{
{% for i in 0...@type.size %}
args[{{ i }}].as(typeof(begin
x = uninitialized self
x[{{ i }}]
end)),
{% end %}
}
{% end %}
{% end %}
end

# Creates a tuple from the given array, with elements casted to the given types.
Expand Down