-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Another signal 11 for recursive generic type declaration #9936
Comments
I think this is essentially #3804 |
Sadly, we haven't managed to remove recursive aliases for three years now (#5155) 😢 I'd recommend everyone to not use them. They're unnecessary and the implementation broken in many ways. |
@straight-shoota if you don't want people to use recursive aliases, # Implementation of a pseudo-generic that contains itself without use of
# recursively-defined aliases, which are problematical and/or broken
# in the compiler.
macro recursive_hash(name, keytype, valuetype)
class {{name.id}}
struct Type
property value : {{valuetype.id}}
def initialize(@value)
end
end
@h = Hash({{keytype.id}}, Type).new
def []=(key, value)
@h[key] = Type.new(value)
end
end
end
recursive_hash(MyHash, Symbol, String|MyHash|Array(MyHash))
h = MyHash.new
h[:itself] = h
h[:array] = [MyHash.new]
p h.inspect |
@BrucePerens What's your specific use case here? |
@asterite The use case is an interpretive language. It has arrays which contain its programs and all of their data types, potentially including more arrays and hashes, and hashes that contain its data (and optionally more hashes and arrays). I coded it using aliases for self-containing generics: Hash and Array. The language is potentially multi-threaded and I will probably eventually add Mutex to these containers, as I am dubious about container write being atomic with regard to a simultaneous read. There were some bumps on the way, including the compiler getting a signal 11 without emitting any debugging information that would help me to understand why (a signal catcher that emits an internal stack dump might be helpful, I've not yet attempted running lldb on the compiler and the multiple threads will be a complication if I start). |
I see. Yes, in that case making your own data type (something like Ruby's VALUE in C) is probably better than using recursive alias, specially because of how buggy its implementation is. |
Yes. It is annoying that the cost of this is that I need to implement a pass-through method for every method of Hash and Array. At least I need only do it once, and I'll package the macros for others. While recursively-typed aliases might not be the solution, it would be nice for there to be a lot of repair on generics, and for there to be broader rules on the use of the generic type arguments - which are not currently in scope when used for much other than the right side of Thanks Bruce |
@BrucePerens You might take a look at https://github.com/straight-shoota/crinja/blob/7511403d058d6dc505ced2344a78491bd0fe9cc3/src/runtime/value.cr#L95 Seems like a similar role to what you're trying to do. |
@straight-shoota OK, I will do that. I see you use |
@BrucePerens Also, you can check any_hash shard. |
@Sija Thank you, there are lots of interesting lessons I need to learn in there. |
Initial code at https://github.com/BrucePerens/recursive_generic . Not ready for you to use. |
This gives a signal 11 when I attempt to compile it:
The text was updated successfully, but these errors were encountered: