Skip to content
This repository has been archived by the owner on Feb 22, 2018. It is now read-only.

Spurious error casting Comparable.compare from Comparable<dynamic> to Comparable<T> #23

Closed
jacob314 opened this issue Jan 9, 2015 · 4 comments

Comments

@jacob314
Copy link
Contributor

jacob314 commented Jan 9, 2015

/// A [Comparator] that uses a [FastFieldAccessor] to speed up comparisons based
/// on nested fields in an [Entity].
class FastComparator<S extends Entity, T> {

  final FastFieldAccessor<S, T> accessor;
  final Comparator<T> comparator;

  FastComparator(String field) : this.withComparator(field,  
  * /* DDC:severe: StaticTypeError, Type check failed: Comparable.compare ((Comparable<dynamic>, Comparable<dynamic>) → int) is not of type (T, T) → int */ 
    Comparable.compare);

  FastComparator.withComparator(String field, this.comparator) :
      accessor = new FastFieldAccessor<S, T>(field);

  int call(S a, S b) => comparator(accessor[a], accessor[b]);
}
@leafpetersen
Copy link
Contributor

Why is this spurious? The comparator field has type T * T -> int, which is completely unrelated to the type of Comparable.compare (i.e. Comparable * Comparable -> int). Nothing stops me from instantiating this class with a T which does not satisfy the Comparable interface, right? I think declaring this as T extends Comparable might solve the issue here, since we need Comparable * Comparable -> int <: T * T -> int, i.e. T <: Comparable, which is what T extends Comparable should give you.

@jacob314
Copy link
Contributor Author

Good point. Unfortunately, I can't set T extends Comparator due to the other constructor.
We could make this constructor assert
T is Comparable which is really the user's intent.

@jacob314
Copy link
Contributor Author

For now I've added the following todo to the code
// TODO(jacobr): we would like to use Comparable.compare but that method
// requires that T be a Comparator which we do not want to enforce for
// all FastComparator objects, only ones that use this constructor.

@jmesserly
Copy link
Contributor

it sounds like we landed in an okay place here

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

No branches or pull requests

3 participants