explicitly access library scope?

48 views
Skip to first unread message

Jim Trainor

unread,
Apr 11, 2015, 11:29:16 AM4/11/15
to mi...@dartlang.org
I have a situation were I'm implementing an interface with a getter named "x" defined in an imported class and it returns a global also named "x" that is defined in my library. I cannot make this work as is because I have no way to explicitly specify global library scope from within my own library. Am I missing something obvious? This seems to be an unresolvable naming conflict (short of renaming something).

library T;

abstract class X {
 
var get x;
}

////////////////////////////////////

library S
;

import 'package:T/T.dart' as T;

var get x => "something I want available in library S scope";

class Y implements T.X {
 
// Woops - recursive stack overflow!
 
// The getter implements T.X.x.  It should return x defined in this
 
// library. The actual result is a recursive call to this class' getter x.
 
var get x => x;
}




Lasse R.H. Nielsen

unread,
Apr 11, 2015, 11:52:14 AM4/11/15
to mi...@dartlang.org
You can introduce a private name that delegates to the global getter:

library S;
...
var get x => ...;
var get _globalX => x;

class Y ... {
  get x => _globalX;
}

Static shadowing ensures that you only shadow things in your own library, and that means you can also add the private redirection, since it is your own library.

/L


--
For other discussions, see https://groups.google.com/a/dartlang.org/
 
For HOWTO questions, visit http://stackoverflow.com/tags/dart
 
To file a bug report or feature request, go to http://www.dartbug.com/new

To unsubscribe from this group and stop receiving emails from it, send an email to misc+uns...@dartlang.org.



--
Lasse R.H. Nielsen - l...@google.com  
'Faith without judgement merely degrades the spirit divine'
Google Denmark ApS - Frederiksborggade 20B, 1 sal - 1360 København K - Denmark - CVR nr. 28 86 69 84

Jim Trainor

unread,
Apr 11, 2015, 12:02:48 PM4/11/15
to mi...@dartlang.org
Thanks.  The private library scope getter is the approach I took.

I find it odd that Dart doesn't have some way to explicitly specify library scope in order to resolve this conflict.  For example optionally using the keyword "library" in this context to resolve the scope.

class Y ... {
  get x => library.x;
}

Leaf Petersen

unread,
Apr 13, 2015, 12:31:34 PM4/13/15
to mi...@dartlang.org
I believe that you can import a library into itself and thereby give it a name.  For your example, if you add

import 'package:S/S.dart' as S;

and then refer to the global x as S.x, I think this will do what you want.

cheers,
-leaf
Reply all
Reply to author
Forward
0 new messages