Coder Social home page Coder Social logo

Comments (2)

lrhn avatar lrhn commented on September 27, 2024 3

the type argument for Foo could logically be inferred as double and int respectively based on the constructor parameters.

Not really. Since num is a valid solution, it's not obvious that using double/int is a better solution.
It is in this case, but it might as well have not been. And if there is more than one constructor, then it becomes even less obvious which type relations should be constraints, and what should not.

But how far should it go?

abstract class S<T extends num> {
  abstract final T value;
}
class T extends S {
  final value;
  T([this.value = 1]);
}

In this case one could (maybe) infer that final value; should inherit the type int to match the default value, so S should be S<int>.

But you don't know the type of 1 before you know the type of value. If the type of value was double, the 1 would have had type double too, so this is actually a cyclic dependency in the type inference algorithm.

There is a reason Dart decided to require the superclass types to be determined before trying to do type inference on members.

from language.

rubenferreira97 avatar rubenferreira97 commented on September 27, 2024

Thanks @lrhn for the clear explanation 🙏.

The inference algorithm I described can indeed become very complex when multiple constructors are involved, and in those cases (among others), I assume that might infer differently for what people would want.

Making your example clearer in practice:

abstract class S<T extends num> {
  abstract final T value;
}

class T extends S/*<int> assuming value is inferred as int */ {
  T([this.value = 1]);

  int getInt() => value;
}

void main() {
  final foo = T(2.0); // Error: value is already inferred as int, so we can't accept a double here. T cannot be both an int and a double.
}

I guess this inference algorithm would only be beneficial if, as in my example, the value is constant within the subclass. Such an edge case might not be a good reason to change the current behavior.

from language.

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.