Difference between revisions of "DIP22"

From D Wiki
Jump to: navigation, search
m (internal is internal)
 
(11 intermediate revisions by 4 users not shown)
Line 1: Line 1:
 
{| class="wikitable"
 
{| class="wikitable"
 
!Title:
 
!Title:
!'''Private symbol visibility'''
+
!'''Private symbol (in)visibility'''
 
|-
 
|-
 
|DIP:
 
|DIP:
Line 7: Line 7:
 
|-
 
|-
 
|Version:
 
|Version:
|1
+
|3
 
|-
 
|-
 
|Status:
 
|Status:
Line 16: Line 16:
 
|-
 
|-
 
|Last Modified:
 
|Last Modified:
|2013-01-28
+
|2013-12-20
 
|-
 
|-
|Author:
+
|Authors:
 
|Михаил Страшун (m.strashun gmail.com) (Dicebot)
 
|Михаил Страшун (m.strashun gmail.com) (Dicebot)
 +
|-
 +
|
 +
|Martin Nowak
 
|-
 
|-
 
|Links:
 
|Links:
Line 26: Line 29:
  
 
== Abstract ==
 
== Abstract ==
There are two important issues with current protection attribute design:
+
This proposal attempts to solve one of important issues with current protection attribute design: senseless name clashes between '''private''' and '''public''' symbols. So change of '''private''' related name resolution rules is proposed.
 +
 
 +
== Rationale ==
  
* Senseless name clashes between '''private''' and '''public''' symbols
+
'''private''' is an encapsulation tool. If it is not intended to be used by "outsiders", it should not interfere with them at all. It creates no new limitations and reduces amount of code breakage by changes in other modules.
* No way to specify internal linkage storage class
+
 
 +
== Description ==
  
This DIP addresses both of them with two decoupled proposals:
+
* Private restricts the visibility of a symbol.
* Change of '''private''' related name resolution rules
 
* Definition of '''static''' storage class for module-scope symbols.
 
  
== Rationale ==
+
  A '''private''' symbol will not interact with other modules.
 +
  In case look-up for a symbol fails the compiler might suggest '''private''' symbols similar to how spell checking works.
  
=== private is private ===
+
* The least protected symbol determines the visibility for overloads.
'''private''' is an encapsulation tool. If it is not intended to be used by "outsiders", it should not be shown to them at all. It creates no new limitations and reduces amount of code breakage by changes in other modules.
 
  
=== static is internal ===
+
  After overload resolution an additional access check will be performed. Thereby overload resolution remains independent of look-up origin.
If '''private''' is a source code level encapsulation tool then '''static''' is object file / binary level encapsulation tool. It allows even more struct separation of implementation-specific code and cleans up object files from unneeded symbols, providing more freedom for compiler optimizations.
 
  
== Description ==
+
* Meta programming tools like <code>__traits</code> and .tupleof can access '''private''' symbols.
  
=== private-related changes ===
+
  This is necessary for some generic code, e.g. serialization.
  
* All changes are same for both classes and modules: it breaks no code but leaves consistent approach.
+
* All changes apply for modules as well as for classes.
* Compiler errors upon access to '''private''' symbol are changed from ''"%s is not accessible"'' to ''"undefined identifier %s"''
 
* Overload resolution takes place after protection attribute questions are settled. '''private''' symbols do not take part in overload resolution.
 
* As privates are already non-virtual, override resolution does not need to change in this regard.
 
* All <code>__traits</code> still show private symbols. Those are advanced user tools and having this may be essential to library code.
 
  
=== new meaning for static ===
+
  Protection has module granularity so looking up '''private'''
 +
  members of a base class from a different module follows
 +
  the same rules as accessing other '''private''' symbols from
 +
  a different module.
 +
  Additionally '''protected''' allows access from derived classes
 +
  but not from other modules.
  
Currently static storage class is no-op for global symbols. It is a big luck as we can use it in the way similar to plain C:
+
* Alias protection overrides the protection of the aliased symbol.
  
* All global static symbols are implicitly private
+
  A '''public''' alias to a '''private''' symbol makes the symbol
* It is forbidden to leak static symbols outside of the module, i.e.
+
  accessibly through the alias. The alias itself needs to be
<syntaxhighlight lang="D">
+
  in the same module, so this doesn't impair protection control.
static struct Internal { }
 
void func1(Internal) { } // compile error
 
private void func2(Internal) { } // compile error, private still leaks its symbols to object file
 
void func3() { Internal t; } // fine, does not leak to func3 signature
 
alias Internal External; // compile error
 
static alias Internal InternalPrim; // fine
 
</syntaxhighlight>
 
* As can have just been seen, nested symbols can also be marked as '''static''' in case there were already static entities: enums, structs, classes, aliases. Variables and functions already have '''static''' defined for other usaged
 
* No '''static''' symbols can be found in resulting object file. They are not shown in traits like <code>__traits(allMembers)</code>. From the point of view of other modules they simply do not exist and may be all inlined or optimized away or whatever.
 
* Only module-level symbols can be marked with global '''static''', for example, nested struct types can't. It is minor limitation that greatly simplifies interaction with other language features like <code>alias this</code>.
 
  
 
=== other protection attribute changes ===
 
=== other protection attribute changes ===
Line 76: Line 70:
 
* '''package''' matches '''private''' changes from the point of view of other packages
 
* '''package''' matches '''private''' changes from the point of view of other packages
 
* '''extern''' stays the same
 
* '''extern''' stays the same
* '''protected''' matches '''private''' changes, descendant still treat protected symbols as '''public''' ones. It is a rare guest in idiomatic D code but does no harm and may be useful for transition from other languages.
+
* '''protected''' matches '''private''' changes, descendants still treat protected symbols as '''public''' ones.
 
 
=== other name resolution changes ===
 
 
 
* UFCS functions should also take priority over private class functions when names conflict
 
* alias this protection attribute overrides protection attribute of aliased symbol ( not transitive )
 
  
 
== Possible code breakage and solutions ==
 
== Possible code breakage and solutions ==
  
* global '''static''' had no meaning before and should not have been used like that. It may happen that is used by accident or in generic code gen. To be absolutely sure this brings no surprises I suggest to create a dmd option that will find and print all usages of '''static''' in global context. It should appear in a release _before_ this DIP release and kept for one release after, then removed.
+
No previously valid code will become illegal in normal use cases, as this proposal is more permissive than current behavior. As __traits and .tupleof will still work for '''private''' as before, any library that relies on them should not break.
 
 
* '''private''' simply replaces one error with another and allows new legal code. Code breakage very unlikely.
 
 
 
* as __traits will still work for '''private''' as before, any library that relies on them should not break
 
  
== Walters concerns ==
+
== Walter's concerns ==
[http://forum.dlang.org/thread/iakfgxjlfzrbxerxpria@forum.dlang.org?page=10#post-kb86il:241u9v:241:40digitalmars.com original comment]
+
[http://forum.dlang.org/post/kb86il$1u9v$1@digitalmars.com original comment]
  
 
1. ''what access means at module scope''
 
1. ''what access means at module scope''
Line 107: Line 92:
 
2. ''at class scope''
 
2. ''at class scope''
  
D minimal encapsulation unit is module. '''Private''' class members are, technically, '''private''' module members and thus have the same behavior. Same for '''package''' and '''public'''. '''Protected''' is only special case that takes additional parameter into consideration.
+
D minimal encapsulation unit is a module. '''Private''' class members are, technically, '''private''' module members and thus have the same behavior. Same for '''package''' and '''public'''. '''Protected''' is only special case that takes additional parameter into consideration.
  
 
3. ''at template mixin scope''
 
3. ''at template mixin scope''
Line 132: Line 117:
  
 
This document has been placed in the Public Domain.
 
This document has been placed in the Public Domain.
 +
[[Category: DIP]]

Latest revision as of 20:59, 8 August 2016

Title: Private symbol (in)visibility
DIP: 22
Version: 3
Status: Draft
Created: 2013-01-28
Last Modified: 2013-12-20
Authors: Михаил Страшун (m.strashun gmail.com) (Dicebot)
Martin Nowak
Links: Access specifiers and visibility : data gathered before creating proposal

Abstract

This proposal attempts to solve one of important issues with current protection attribute design: senseless name clashes between private and public symbols. So change of private related name resolution rules is proposed.

Rationale

private is an encapsulation tool. If it is not intended to be used by "outsiders", it should not interfere with them at all. It creates no new limitations and reduces amount of code breakage by changes in other modules.

Description

  • Private restricts the visibility of a symbol.
 A private symbol will not interact with other modules.
 In case look-up for a symbol fails the compiler might suggest private symbols similar to how spell checking works.
  • The least protected symbol determines the visibility for overloads.
 After overload resolution an additional access check will be performed. Thereby overload resolution remains independent of look-up origin.
  • Meta programming tools like __traits and .tupleof can access private symbols.
 This is necessary for some generic code, e.g. serialization.
  • All changes apply for modules as well as for classes.
 Protection has module granularity so looking up private
 members of a base class from a different module follows
 the same rules as accessing other private symbols from
 a different module.
 Additionally protected allows access from derived classes
 but not from other modules.
  • Alias protection overrides the protection of the aliased symbol.
 A public alias to a private symbol makes the symbol
 accessibly through the alias. The alias itself needs to be
 in the same module, so this doesn't impair protection control.

other protection attribute changes

  • public stays the same
  • package matches private changes from the point of view of other packages
  • extern stays the same
  • protected matches private changes, descendants still treat protected symbols as public ones.

Possible code breakage and solutions

No previously valid code will become illegal in normal use cases, as this proposal is more permissive than current behavior. As __traits and .tupleof will still work for private as before, any library that relies on them should not break.

Walter's concerns

original comment

1. what access means at module scope

"Does this symbol is ignored when doing symbol name look-up?". All protection attributes boil down to simple answer (Yes/No) depending on symbol origins and place look-up is made from. In example:

Symbol origin:               module a;
Look-up origin:              not module a;
Symbol protection attribute: private
Answer:                      No

2. at class scope

D minimal encapsulation unit is a module. Private class members are, technically, private module members and thus have the same behavior. Same for package and public. Protected is only special case that takes additional parameter into consideration.

3. at template mixin scope

No changes here. For templates look-up origin is definition module. For mixin templates - instantiation module. Other than that, usual rules apply.

4. backwards compatibility

See "Possible code breakage and solutions"

5. overloading at each scope level and the interactions with access

See "Description".

6. I'd also throw in getting rid of the "protected" access attribute completely, as I've seen debate over that being a useless idea

I have found no harm in keeping it. This will break code for sure and is irrelevant to this DIP topic.

7. there's also some debate about what "package" should mean

This is also irrelevant to this DIP. While there may be debates on meaning of package concept, meaning of package protection attribute is solid: encapsulation within set of modules belonging to same package, whatever they are.

Copyright

This document has been placed in the Public Domain.