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

Add Hash/Indexable#dig/dig? #6719

Merged
merged 3 commits into from
Sep 22, 2018
Merged

Add Hash/Indexable#dig/dig? #6719

merged 3 commits into from
Sep 22, 2018

Conversation

Sija
Copy link
Contributor

@Sija Sija commented Sep 14, 2018

Followup to #3536.

@j8r
Copy link
Contributor

j8r commented Sep 14, 2018

It would be also great to have #dig(keys : Enumerable), where keys represents a variable sequence of keys.

@RX14
Copy link
Contributor

RX14 commented Sep 14, 2018

Thought we decided against this. It was .dig or JSON::Any and JSON::Any won because my proposal forgot that JSON::Type included more than Hash and Array. I proposed it could be solved by making JSON.parse return Hash | Array because the 95% usecase for json is arrays or hashes at the top level, but that idea wasn't well recieved.

@j8r that method would just produce insane types.

@Sija
Copy link
Contributor Author

Sija commented Sep 14, 2018

@RX14 I understood differently, also according to your own #3536 (comment).
@j8r That would forgo all compile-time safety, ditto what @RX14 said.

I still see it as a very handy addition to stdlib and see no reason not to implement it.

@asterite
Copy link
Member

I was against dig because I thought the implementation would make the return type be the union of everything. But this implementation is brilliant.

a = {1 => [[1, 2], [3, 4]]}

p typeof(a.dig(1, 0)) # => Array(Int32)
p a.dig(1, 0) # => [1, 2]

p typeof(a.dig(1, 0, 1)) # => Int32
p a.dig(1, 0, 1) # => 2

So I see it as a very nice, convenient way, to traverse deep structures. Now we would just need to add it to JSON::Any and YAML::Any... or remove those ugly Any wrappers altogether (I'd start by adding dig/dig? to those types, and eventually decide how to remove them).

@Sija
Copy link
Contributor Author

Sija commented Sep 14, 2018

So I see it as a very nice, convenient way, to traverse deep structures.

Exactly.

... or remove those ugly Any wrappers altogether (I'd start by adding dig/dig? to those types, and eventually decide how to remove them).

Yes, please! 🎉

@asterite
Copy link
Member

@Sija Do you want to add dig/dig? to Any then? At least that way this PR will be "complete".

@Sija
Copy link
Contributor Author

Sija commented Sep 14, 2018

@asterite Sure, will do.

@codenoid
Copy link
Contributor

hi @Sija , long time no c

@Sija
Copy link
Contributor Author

Sija commented Sep 14, 2018

@asterite Done.
@codenoid Indeed, good to c u back! :)

@Sija
Copy link
Contributor Author

Sija commented Sep 17, 2018

Is it GTG now? CI fails seems not related, btw.

@Sija
Copy link
Contributor Author

Sija commented Sep 22, 2018

Hello? Is this got stalled for a reason?

Copy link
Member

@bcardiff bcardiff left a comment

Choose a reason for hiding this comment

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

GTG on my side. Maybe NamedTuple should also have #dig to traverse them directly.

@Sija
Copy link
Contributor Author

Sija commented Sep 22, 2018

@bcardiff I like it 👍, it's here in the last commit.

@bcardiff
Copy link
Member

@Sija CircleCI had a bad week in their servers.

In darwin is still failing because it trying to compile the compiler with llvm 7, which is still not supported. The dependency of the formula is llvm plain and that leads to llvm 7 right now.

@RX14 RX14 added this to the 0.27.0 milestone Sep 22, 2018
@RX14 RX14 merged commit 19338f0 into crystal-lang:master Sep 22, 2018
ezrast pushed a commit to ezrast/crystal that referenced this pull request Oct 2, 2018
* Add Hash/Indexable#dig/dig?

* Add JSON/YAML::Any#dig/dig?

* Add NamedTuple#dig/dig?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants