Coder Social home page Coder Social logo

Generic function type mismatch: `type '(Subtype) => ReturnType' is not a subtype of type '((Supertype) => ReturnType)?'` about sdk HOT 4 CLOSED

maRci002 avatar maRci002 commented on July 21, 2024
Generic function type mismatch: `type '(Subtype) => ReturnType' is not a subtype of type '((Supertype) => ReturnType)?'`

from sdk.

Comments (4)

lrhn avatar lrhn commented on July 21, 2024 1

Correct. The occurrence of T in

  final num Function(T)? method;

is contravariant, while the T in the type itself is (unsafely) covariant.

Because of that, the code that reads the property mention.method, where mention has static type B<A>, and the read expression therefore static type num Function(A), must be prepared for the value to be, fx, a num Function(A2), which is-not-a num Function(A).
To preserve soundness, the guarantee that the value of an expression satisfies the static type, the compiler inserts a type check that throws if the value isn't sound.

Basically it makes the read expression into (mention.method as num Function(A)).

Which then fails at runtime, as intended.

If it hadn't thrown there, the next statement could've been method(A()), which wouldn't be sound.

If you change the method getter to a method:

final num Function(T) _method;
num method(T value) => _method(value);

then the reading becomes type safe, because the reader is now using the same type for T, but calling the method may throw at runtime.

There is just no way to allow an actual num Function(A2) to be successfully called with a A() value.

from sdk.

eernstg avatar eernstg commented on July 21, 2024 1

See dart-lang/language#297 for some background info about the treatment of occurrences of type variables in positions that disagree with their variance.

See dart-lang/language#524 for a proposal about statically checked variance (covariance, contravariance, invariance). If we get that then you can use this:

class B<in T extends A> {
  const B({this.method});

  final num Function(T)? method;
}

.. and then B<A2> will be a supertype of B<A>. You can vote for dart-lang/language#524 if you wish to support the addition of a statically checked kind of variance to Dart.

from sdk.

maRci002 avatar maRci002 commented on July 21, 2024

I think the problem is that the mention is of type B<A>, which is actually a B<A2> type. Therefore, I get an error since I won't be able to pass an instance of A, but rather an instance of A2.

from sdk.

maRci002 avatar maRci002 commented on July 21, 2024

Thank you for the clarification. Anyway, I did something similar with manual casting:

final num Function(T) _method;
num method(A value) => _method(value as T);

But your solution is better.

from sdk.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.