Coder Social home page Coder Social logo

Comments (5)

dhruvjariwala123 avatar dhruvjariwala123 commented on July 2, 2024

Also this example :
void main() {
MyClass myClass = MyClass();
print(myClass.i);
print(myClass.j);
}

class MyClass {
//Should not allow to initialize by themselves.
late int i = this.i;
// And also should not allow to initialize by others who are dependent on it.
late int j = this.k;
late int k = this.j;
}

from sdk.

lrhn avatar lrhn commented on July 2, 2024

An initializer not being self referential is probably a good warning.

I can write a successfull program with a self reference, so it's not necessarily a bug, but I can't find up with any reasonable reason for writing a program like that, so giving a warning unconditionally seems appropriate.

Marking as analyzer.
We could try to disallow the self reference in the language, but to do it properly, we'd want to catch mutually recursive declarations too, which is not trivial. A heuristic warning if the declaration contains direct self reference matches an analyzer warning better.

We can detect self references in constants because we actually evaluate them at compile time.

(A "successful" self-referential initializer could be:

class Weird {
  final int _defaultLevel;
  Weird(int defaultLevel) : _defaultLevel = defaultLevel;
  late String _levelString = (this._level, this._levelString).$2; // Initialize level if it isn't.
  late int _level = (_levelString = _defaultLevel.toString(), _defaultLevel).$2;
}

Here the initialil this._level will assign to _levelString, which it can then successfully read.
Just never do something like that, it's too hard to read.)

from sdk.

dhruvjariwala123 avatar dhruvjariwala123 commented on July 2, 2024

Thank you sir for updating the correct title for me :)
But, the analyzer needs to give a compile time error, not a warning. because the program is crashing due to a stack overflow situation.

from sdk.

lrhn avatar lrhn commented on July 2, 2024

Stack overflow is a runtime error. There are lots of ways to write programs with runtime errors, that the analyzer cannot recognize at compiler time.

The analyzer doesn't generally introduce compile time errors, it only reports errors that are specified in the language specification.
And I don't see us trying to catch this error in the specification.
For "this is probably an error"-cases, the analyzer reports a warning. You can always change the diagnostic to an error if you want to.

from sdk.

dhruvjariwala123 avatar dhruvjariwala123 commented on July 2, 2024

ok sir, thanks for replying to my issue 😊.

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.