much better return type for Iterable.sequence() #6804
Comments
Should the implementation of That is, for spread arguments that are subtypes of |
Yeah, I suppose we could make that work. It's a good idea. |
It appears that the reified type check costs about 1.5 μs in a tight loop. |
Which is about 7 times slower than calling |
So to even make a start on this issue, I need some nice person like @tombentley or @FroMage to do this bit for me:
Which means:
The "real" type in (2) is always going to be either |
Er, if I have
then I get error "specified expression must be assignable to declared type of 'x2': '[Element+]|[]&{Element+}' is not assignable to '[Element+]'" What am I doing wrong? |
@tombentley what is your definition of |
We were using this function: [Element+] | []&Iterable<Element,Absent>
sequence<Element,Absent>
(Iterable<Element,Absent> elements)
given Absent satisfies Null {
if (nonempty sequence = elements.sequence()) {
return sequence;
}
else {
assert (is Iterable<Element,Absent> empty = []);
return empty;
}
} I don't think the signature should stop working if you turned it into a method. |
Oh, I see, this problem only occurs if |
Ah, OK, let me see then... |
It is a limitation of the code at I think we could improve that code by replacing type parameters with |
As a workaround, @tombentley can use the return type |
@jvasileff OK, I have removed that limitation, but in fact the return type |
Oh no, not right, it's |
Actually I like writing the function this way: alias Empty<Absent> => [] & Iterable<Anything,Absent>;
[Element+]|Empty<Absent> sequence<Element,Absent>
(Iterable<Element,Absent> elements)
given Absent satisfies Null {
if (nonempty sequence = elements.sequence()) {
return sequence;
}
else {
assert (is Iterable<Anything,Absent> e = []);
return e;
}
} |
That's really nice! (Although, |
Type functions FTW. |
@tombentley where are we at on this one? |
You want to do this for both |
I was going to leave the toplevel |
It appears this change would be source incompatible, in that subclasses of
Funnily enough, if I add the suggested
|
@gavinking I'd like your feedback about this problem ^^ before proceeding further. |
It looks like the (confusingly worded) error is complaining about the signature. Try this? shared actual default [Element+]|[] sequence() => this of [Element+]|[]; |
@gavinking I pushed some changes to the At the java level though we will use raw Anyway, have an experiment on the branch and let me know if you need anything else doing. |
@tombentley Yes, I agree that it's not perfectly source-compatible, but that it is binary compatible. If we think the loss of source-compatibility is a real problem, then I can certainly do something to hack that in the typechecker (replacing the error with a warning in this special case). But my sense is that there's not much code out there that refines |
OK, this change is now source- and binary- backward compatible. I have merged the branch! Thanks @tombentley! |
Amazing, great! |
Today, @jvasileff figured out the solution to a problem that has bugged us for years — how to assign the correct return type to
Iterable.sequence()
for known-nonempty streams.The return type is:
There would be one big problem with making this change, and one much smaller problem:
Iterable.sequence()
, which at the Java level would widen fromSequential<? extends Element>
toObject
.[]
,""
, or{}
, callingsequence()
would result in a reified check of the type argumentAbsent
.Problem (1) is rather easy to fix with a little hack to the compiler backend to preserve the previous return type
Sequential<? extends Element>
.Problem (2) is not so easy to fix, but may not be a big deal.
The text was updated successfully, but these errors were encountered: