Crystal: Iterator doesn't seem to be matching type aliases

Created on 20 Mar 2019  路  6Comments  路  Source: crystal-lang/crystal

Trying to use an Iterator to reduce memory consumption of a large result pulled back from a database (the nodes go through a few transformations before being serialized as JSON) and getting this error:

instantiating 'Iterator::Map(Indexable::ItemIterator(Array(Neo4j::Type), Neo4j::Type), Neo4j::Type, DatabaseMetric)#next()'
in /usr/local/Cellar/crystal/0.27.2/src/iterator.cr:722: type must be Neo4j::Type, not (Array(Neo4j::Type) | Bool | Float64 | Hash(String, Neo4j::Type) | Int16 | Int32 | Int64 | Int8 | Neo4j::Failure | Neo4j::Ignored | Neo4j::LatLng | Neo4j::Node | Neo4j::Path | Neo4j::Point2D | Neo4j::Point3D | Neo4j::Relationship | Neo4j::Success | Neo4j::UnboundRelationship | String | Time | Nil)

In this case, Neo4j::Type is an alias for all of those types, including Nil.

I unfortunately don't have a reduced example for this yet. I'll see if I can find one soon.

bug topicsemantic

Most helpful comment

@jgaskins Sorry for all the above noise, it was a really simple bug to fix. It'll probably be available in the next release.

All 6 comments

My advice is to avoid using recursive aliases. They are slightly broken. You can use wrapper structs to achieve more or less the same result. That will probably make it work (I don't know if you control the Neo4j library).

@jgaskins can you try doing exp.as(Neo4j::Type) with the return value of the iterator?

Otherwise, without some reproducible code we can't help.

Actually, it's super easy to reproduce:

alias Rec = Int32 | Array(Rec)

a = [] of Rec
a << a

p a.each.map(&.itself).to_a

And simpler:

alias Rec = Int32 | Array(Rec)

a = uninitialized Rec

f = ->(x : Rec) {}
f.call(a.itself)

I think this can be fixed, it seems to be a problem with Proc type matching and recursive aliases.

@jgaskins Sorry for all the above noise, it was a really simple bug to fix. It'll probably be available in the next release.

Nice work! I was going to try to get a reduced example after work today but you beat me to it! :joy: Thanks!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

nabeelomer picture nabeelomer  路  3Comments

RX14 picture RX14  路  3Comments

TechMagister picture TechMagister  路  3Comments

pbrusco picture pbrusco  路  3Comments

Papierkorb picture Papierkorb  路  3Comments