Sdk: Eliminate writing underscore(_) every time for private data member.

Created on 9 May 2015  路  3Comments  路  Source: dart-lang/sdk

_This issue was originally filed by krupals...@gmail.com_


While declaring a 'private to library' data member in Dart, I have to prefix it everywhere with underscore(_). But Why it is required after declaration? Can't we access private data members without having underscore as prefix all the time?

For example: if I write ,

void main() {
  
  int _i;

}
I have to write underscore every time I have to access i after declaration like:

void main() {
  
  int _i;
  
  for (_i = 0; _i < 5; _i++) {
    print('hello ${_i + 1}');
  }
}

Instead, it should be written as :

void main() {
  
  int _i;

  for (i = 0; i < 5; i++) {
    print('hello ${i + 1}');
  }
}

i has already been declared as private, there is no need to write underscore every time.

What I mean to say is that If I declare a data member as private once, it should be used without underscore(_) prefix so that I can freely declare any variable private once and after that use it without _ prefix.

Dart SDK version: 1.10
Operating system : Windows 7 32 bit
Issue type : Enhancement

area-language closed-not-planned type-enhancement

Most helpful comment

any update regarding visibility model in Dart? It is really bad to write underscores and there are hundread of users who are waiting for change.

All 3 comments

That probably won't work.

The problem is that in Dart, you don't know at compile time what a variable refers to.

Example:
  class C {
    var _i; // aka "private i;"
    foo(object) => object.i;
  }
Should the foo function look up the private "i" name or a public "i" name on "object"?

Basically, it's not going to work.


_Added Area-Language, Triaged labels._

_This comment was originally written by krupalshah...@gmail.com_


Then this is bad about the language. I will have to remember all the time which variables are private and which are public. Don't you think it should be improved?

any update regarding visibility model in Dart? It is really bad to write underscores and there are hundread of users who are waiting for change.

Was this page helpful?
0 / 5 - 0 ratings