We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
See #3858, by @makenowjust. This issue is here to keep track of the problem.
On Crystal 0.20.3, for instance:
private class Foo end Foo.new.dup
this code causes an error:
Error in foo.cr:4: instantiating 'Foo#dup()' Foo.new.dup ^~~ in /.../src/reference.cr:40: expanding macro {% if @type.abstract? %} ^ in macro 'macro_XXX' /.../src/reference.cr:40, line 2: 1. > 2. dup = Foo.allocate 3. dup.as(Void*).copy_from(self.as(Void*), instance_sizeof(Foo)) 4. dup 5.
The text was updated successfully, but these errors were encountered:
This is a simple patch to get Reference#dup work without changing macro: (But we still need to improve the macro system)
Reference#dup
From 2b74a5a99eea3e7da6c62646ef609db5e7a6875e Mon Sep 17 00:00:00 2001 From: David Kuo <s50407s@gmail.com> Date: Fri, 13 Jan 2017 21:42:00 +0800 Subject: [PATCH] Allow Reference#dup in private type --- src/reference.cr | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/reference.cr b/src/reference.cr index 35e26d2..a09a8e5 100644 --- a/src/reference.cr +++ b/src/reference.cr @@ -44,8 +44,8 @@ class Reference # but we need to avoid the allocate invocation below raise "can't dup {{@type}}" {% else %} - dup = {{@type}}.allocate - dup.as(Void*).copy_from(self.as(Void*), instance_sizeof({{@type}})) + dup = self.class.allocate + dup.as(Void*).copy_from(self.as(Void*), instance_sizeof(self)) dup {% end %} end -- 2.7.4
tested with:
private class Foo property a = 1 end f = Foo.new p f.a p f.a = 2 b = f.dup p b.a
Sorry, something went wrong.
@david50407 Doesn't work with:
class Foo property a = 1 end class Bar < Foo end [Foo.new, Bar.new].map &.dup
This needs a compiler fix, there's no way to do it without changing the compiler.
@asterite oops, I think I know why that fails
Should we create a macro system central-discussing issue like this #2665 ?
bea18ec
No branches or pull requests
See #3858, by @makenowjust. This issue is here to keep track of the problem.
On Crystal 0.20.3, for instance:
this code causes an error:
The text was updated successfully, but these errors were encountered: