Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Language enhancement: qualifier for top level scope #2288

Closed
DartBot opened this issue Mar 24, 2012 · 8 comments
Closed

Language enhancement: qualifier for top level scope #2288

DartBot opened this issue Mar 24, 2012 · 8 comments
Labels
area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). P2 A bug or feature request we're likely to work on type-enhancement A request for a change that isn't a bug

Comments

@DartBot
Copy link

DartBot commented Mar 24, 2012

This issue was originally filed by zundel@google.com


This is a request to add a special qualifier, similar to 'this' and 'super' to specify access to a symbol a the library scope. I don't care what the name is, but it could be something like "global"

foo() {
  return "global";
}

class A {
  foo() {
    return "class A";
  }
  bar() {
    print (foo()); // prints "class A"
    print (global.foo()); // prints "global"
  }
}

The justification for this request is that currently, the only way to specify a field or method at the top level (library) scope is to use the identifier with no qualifiers. Another instance field can shadow the top level identifier and give no direct way. This will return a warning, so we do have coverage for this problem, but nevertheless, there is no way to defensively code around this issue the way you can with 'this.foo' or 'super.foo' to make it explicit that you do not mean for a symbol to resolve to the library scope.

A private discussion with Gilad convinces me that this is not an essential feature, assuming one pays attention to the warnings. You can work aroun d this by creating a delegate method at the top level with a different name, for example.

@DartBot
Copy link
Author

DartBot commented Mar 24, 2012

This comment was originally written by zundel@google.com


I did cook up one case where a programmer can be caught off guard and no warnings will be emitted by an errant library upgrade. Assume that a developer is using two third party libraries, libA and libB

libA.dart:
foo() {
  return "libA";
}

libB.dart:
class B {
  foo() {
    return "libB";
  }
}

user code:

import ("libA.dart");
import ("libB.dart");

class myClass extends B {
  String libAFeature() {
    return foo(); // resolves to top level foo() in libA.dart
  }
}

Now, at some point, libA is updated so that foo() is renamed.
libA.dart:

newFoo(int a) {
  return "libA";
}

Now, if you run the usercode libAfeature(), the invocation of foo() now resolves to the superclass method for 'foo()'. Its all well and good to say that libA shouldn't have it renamed its top level method, but that is in a library that the user code doesn't control.

The proposed 'global' qualifier would have provided a way for the library user to protect themselves. Another way the user could have protected themselves from this kind of change is to prefix the library.

@gbracha
Copy link
Contributor

gbracha commented Apr 12, 2012

I think the cleanest solution to this would be in the context of first class libraries and/or class nesting. In the meantime, we should leave this open until a comprehensive approach presents itself.


Set owner to @gbracha.
Added Accepted label.

@gbracha
Copy link
Contributor

gbracha commented May 24, 2012

Added this to the Later milestone.

@DartBot
Copy link
Author

DartBot commented Jun 21, 2013

This comment was originally written by waprave...@gmail.com


The obvious thing is to search the class hierarchy for the method and if it doesn't exist there then search the top level.

This

void go() {
  print("Hello from Global");
}

class Base
{
  void go() {
    print("Hello from Global.Base");
  }
}

class Test extends Base {
  void readySet()
  {
    go();
  }
}

void main() {
  Test t = new Test();
  
  t.readySet();
}

and this

void go() {
  print("Hello from Global");
}

class Base
{
  void go() {
    print("Hello from Global.Base");
  }
}

class Test extends Base {
  void readySet()
  {
    super.go();
  }
}

void main() {
  Test t = new Test();
  
  t.readySet();
}

should produce the same result.

@kasperl
Copy link

kasperl commented Jul 10, 2014

Removed this from the Later milestone.
Added Oldschool-Milestone-Later label.

@kasperl
Copy link

kasperl commented Aug 4, 2014

Removed Oldschool-Milestone-Later label.

@DartBot
Copy link
Author

DartBot commented Oct 7, 2014

This comment was originally written by @seaneagan


Might make sense to use the "as" keyword on "library":

library foo as foo;

import 'package:bar/bar.dart'; // Has a top-level bar.

var baz;

class Bar {
  var bar, baz;

  get bloop => [bar, foo.bar, baz, foo.baz];
  
}

@DartBot DartBot added Type-Enhancement area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). labels Oct 7, 2014
@kevmoo kevmoo added P2 A bug or feature request we're likely to work on type-enhancement A request for a change that isn't a bug and removed accepted labels Feb 29, 2016
@lrhn lrhn changed the title Language enhnacement: qualifier for top level scope Language enhancement: qualifier for top level scope Nov 1, 2017
@lrhn
Copy link
Member

lrhn commented Oct 14, 2019

No current plans to add this feature. A top-level scope identifier should not be used defensively, because that would mean writing it everywhere. It should at most be used when necessary.

A partial workaround is (or should be):

import "" as top;

which makes the current library's export scope available.
It does not give access to imported libraries, but those can also be imported with a prefix directly.
It does not give access to private top-level declarations. Those should be entirely under the control of the library author, so they can be renamed without breaking anyone.

@lrhn lrhn closed this as completed Oct 14, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). P2 A bug or feature request we're likely to work on type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

No branches or pull requests

5 participants