[Issue 10258] New: Add hasAccess trait
d-bugmail at puremagic.com
d-bugmail at puremagic.com
Mon Jun 3 15:53:21 PDT 2013
http://d.puremagic.com/issues/show_bug.cgi?id=10258
Summary: Add hasAccess trait
Product: D
Version: D2
Platform: All
OS/Version: All
Status: NEW
Severity: enhancement
Priority: P2
Component: DMD
AssignedTo: nobody at puremagic.com
ReportedBy: andrej.mitrovich at gmail.com
--- Comment #0 from Andrej Mitrovic <andrej.mitrovich at gmail.com> 2013-06-03 15:53:20 PDT ---
One of the most common implementation problems in my template code is that I
rarely properly check for access issues. Take the following for example:
b.d:
-----
module b;
import std.conv;
import std.stdio;
string[] getValues(T)(T var)
{
string[] values;
foreach (member; __traits(allMembers, T))
values ~= to!string(__traits(getMember, var, member));
return values;
}
struct S
{
int x;
private int y;
}
void main()
{
S s;
writeln(getValues(s));
}
-----
All is well and good. But now try moving the S struct and main function into
another module:
-----
module a;
import b;
import std.stdio;
struct S
{
int x;
private int y;
}
void main()
{
S s;
writeln(getValues(s));
}
-----
And now we get: Error: struct test.S member y is not accessible
This is a commonly overlooked problem because unittests for templates are
usually written right alongside them, and "private" extends to the entire
module.
We currently have a "getProtection" attribute, but it's awkward to use it
because if a symbol has a package or protected attribute we still won't know
whether we actually have access to it *from the call point*.
Using is(typeof()) to check accessibility is a hacky workaround, so instead I
propose that we implement a new trait, called hasAccess. It would be used like
so:
-----
string[] getValues(T)(T var)
{
string[] values;
foreach (member; __traits(allMembers, T))
{
// new
static if (__traits(hasAccess, __traits(getMember, var, member))
{
values ~= to!string(__traits(getMember, var, member));
}
}
return values;
}
-----
The compiler would then check if the current scope has access to a symbol and
return true or false.
The true benefit of the 'hasAccess' trait is that it will automatically work
for templates, mixin templates, and string mixin code.
--
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
More information about the Digitalmars-d-bugs
mailing list