-
-
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
Proposal for fix-typed recursive array #10461
Comments
FWIW the first example works if you correctly type the array. E.g. a = [1,2,3] of Int32 | Array(Int32)
a.push([4,5,6])
a.push(7)
a # => [1, 2, 3, [4, 5, 6], 7] Otherwise the type of |
Yes, I know. |
@wolfgang371 yeah, that exact limitation pushed me into creating any_hash shard, which allows pretty dynamic (and type recursive) manipulation - downside of that is the need to cast the returned values to the original type... perhaps you'll find some inspiration there, if not please ignore this message ;) |
@wolfgang371 It's my understanding that that would never work because Crystal isn't dynamically typed. So it's kinda expected you'd have to manually define the type of your ivars, e.g. Of which, I think that would be best suited to a shard. |
What's the use case anyway? |
As for use cases: I use the arbitrary ruby arrays e.g. for trees or tree like data structures. |
@Blacksmoke16 (#10461 (comment) above): since in ruby this is part of the language the shard idea somehow was a blind spot to me. |
Kay, that's a use case but IMO not a good one. Using generic, nested arrays for this isn't really a good idea. Neither in Crystal nor Ruby, but Ruby lets you get away with it. Maybe that's okay for Ruby. But in Crystal you're way better of just using a special data type to represent an actual tree. Your |
Motivation: while this works in crystal...
... the following does not work (but does in ruby):
Moreover, both is not available in a flexible way in crystal for instance/class variables since they need to have a fixed type.
So I tried to come up with something generic. Since this is not absolutely straightforward to me and to my knowledge not yet available in the crystal lib, maybe you want to add something like this?
Some tests:
BTW: first I tried something based on
alias
(https://crystal-lang.org/reference/syntax_and_semantics/alias.html) but then I learned on the one hand side they might get thrown out again (#5155) and on the other had some troubles on my first experiment based on the provided example.Another note: I also tried to implement the complementary method
to_a
. But then I realized this is not possible with crystal as it is since this already makes the compiler break:The text was updated successfully, but these errors were encountered: