-
Notifications
You must be signed in to change notification settings - Fork 8
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
mergeSort fails when static type of a list does not match a runtime type #663
Comments
@natebosch @devoncarew could you please take a look? |
Looks like I haven't tried it in detail, but it looks like we'd simply need to ensure that the // `src/algorithms.dart`, line 235:
var scratchSpace = List<E>.filled(secondLength, elements[start]);
// Could be the following, except that this would be too slow:
var scratchSpace = elements.take(0).toList();
var startElement = elements[start];
for (int i = 0; i < secondLength; ++i) scratchSpace.add(startElement); (I'm commenting on this because it's yet another example of a situation where dynamically checked covariance causes a run-time type error. So I added a reference to this issue to dart-lang/language#524.) |
var scratchSpace = elements.take(secondLength).map((_) => elements[start]).toList() maybe? |
Hehe, who knows, but I suspect that the actual type argument passed to void main() {
List<num> elements = <int>[1, 2, 3];
var secondLength = 2;
var start = 0;
var scratchSpace = elements.take(secondLength).map((_) => elements[start]).toList();
print(scratchSpace.runtimeType); // 'JSArray<num>' in DartPad, should've been 'JSArray<int>'.
} |
There are problems to using
I think the type error in Heretical opinion: |
We have a package which could solve this, but it cheats and uses capabilities the language normally doesn't have. I don't think we want to spread usage of that package unless we are fairly confident that we are going to add a language level capability to replace it eventually, such as type patterns. dart-lang/language#170 |
@rakudrama wrote:
Right, the member signature and specified behavior of So we don't have (and inherently can't have) any tool which is guaranteed to yield a fresh list whose run-time type implements In any case, we could provide something like |
Would type patterns enable this? |
Yes. Type patterns is a meta-proposal in the sense that the basic idea can be applied to static types as well as run-time types, in various ways. One typical example would be that we use type patterns to bind a type variable to the run-time value of a type parameter at a given type, and that particular feature would support the following: void main() {
List<num> xs = <int>[1];
if (xs is List<final X>) { // `X` is in scope here, the value is `int`.
var newList = List<X>.filled(someLength, xs.first);
}
} |
I'd trust The And we should avoid anything that is generic in the return type, since as Erik says, then the return type will depend on the static type, not the runtime element type of the receiver, which is the only type known to be correct. The most direct approach is If that can't work for some reason, I expect |
Fixes #317 Use `sublist` on the input to get a list with the same runtime generic as the input instead of constructing a new list with the static generic.
…ection#320) Fixes dart-lang/collection#317 Use `sublist` on the input to get a list with the same runtime generic as the input instead of constructing a new list with the static generic.
https://dartpad.dev/?id=f9535fca683721dd5eb4416e364cc5df
scratchSpace is created with a function type argument, and then setRange fails, because runtime element types don't match.
The text was updated successfully, but these errors were encountered: