| Thread overview | ||||||
|---|---|---|---|---|---|---|
|
September 26, 2015 Why getting private member fails using getMember trait in a template? | ||||
|---|---|---|---|---|
| ||||
Suppose we have, two modules:
module testOne;
import std.traits;
template getMember(alias T, string member) {
alias getMember = Identity!(__traits(getMember, T, member));
}
module app;
import testOne;
import std.traits;
class TestOne {
private {
int property;
}
public {
int func() {
return 0;
}
}
}
template getMember(alias T, string member) {
alias getMember = Identity!(__traits(getMember, T, member));
}
void main() {
pragma(msg, fullyQualifiedName!(__traits(getMember, TestOne, "property")));
pragma(msg, fullyQualifiedName!(app.getMember!(TestOne, "property")));
pragma(msg, fullyQualifiedName!(testOne.getMember!(TestOne, "property")));
}
First two statements execute and I get fully qualified name, while the third one fails with next error (dmd version v2.067.1):
src/testOne.d(6): Error: class app.TestOne member property is not accessible
src/app.d(26): Error: template instance testOne.getMember!(TestOne, "property") error instantiating
src/app.d(26): while evaluating pragma(msg, fullyQualifiedName!(testOne.getMember!(TestOne, "property")))
| ||||
September 29, 2015 Re: Why getting private member fails using getMember trait in a template? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Alexandru Ermicioi | On Saturday, 26 September 2015 at 10:10:39 UTC, Alexandru Ermicioi wrote:
> Suppose we have, two modules:
>
> module testOne;
>
> [...]
So, is this behavior correct?
If yes, then why?
| |||
September 30, 2015 Re: Why getting private member fails using getMember trait in a template? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Alexandru Ermicioi | On Tuesday, 29 September 2015 at 09:40:41 UTC, Alexandru Ermicioi wrote:
> On Saturday, 26 September 2015 at 10:10:39 UTC, Alexandru Ermicioi wrote:
>> Suppose we have, two modules:
>>
>> module testOne;
>>
>> [...]
>
> So, is this behavior correct?
> If yes, then why?
Yes, because private members aren't accessible from another module. If they need to be accessed, then they need to be public.
Atila
| |||
September 30, 2015 Re: Why getting private member fails using getMember trait in a template? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Atila Neves | On Wednesday, 30 September 2015 at 07:57:59 UTC, Atila Neves wrote:
> On Tuesday, 29 September 2015 at 09:40:41 UTC, Alexandru Ermicioi wrote:
>> On Saturday, 26 September 2015 at 10:10:39 UTC, Alexandru Ermicioi wrote:
>>> Suppose we have, two modules:
>>>
>>> module testOne;
>>>
>>> [...]
>>
>> So, is this behavior correct?
>> If yes, then why?
>
> Yes, because private members aren't accessible from another module. If they need to be accessed, then they need to be public.
>
> Atila
As a workaround, you should be able to determine the index of the member (i.e. the how-many-th member it is in your struct/class), and then use .tupleof to access it, which circumvents access checks.
| |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply