Thread overview
Scoped Operator Overloading
Jun 02, 2013
Carl
Jun 02, 2013
Morning Song
Jun 02, 2013
Carl
Jun 02, 2013
Carl
Jun 02, 2013
Michael
June 02, 2013
I am writing a class to act like a database or cache. I want to enable looping with foreach, but I need two separate opApply methods. One for the internal looping through raw data and a second for looping through the cached data (strings for example).

Is there a way to scope the first opApply (looping through raw data) to only be accessible inside the class? This would be beneficial to prevent someone from accidentally looping through raw data instead of their cached objects.
June 02, 2013
On Sunday, 2 June 2013 at 05:27:08 UTC, Carl wrote:
> I am writing a class to act like a database or cache. I want to enable looping with foreach, but I need two separate opApply methods. One for the internal looping through raw data and a second for looping through the cached data (strings for example).
>
> Is there a way to scope the first opApply (looping through raw data) to only be accessible inside the class? This would be beneficial to prevent someone from accidentally looping through raw data instead of their cached objects.

If the delegates to the opApply have different parameters (I.e.
it's actually a different data type getting passed to the foreach
loop), then method overloading will take care of it; just mark
one of them private.

If they both need to get sent the same kind of data, it's a hack,
but you could try encapsulating the data for the raw one inside a
private struct. Then, inside the foreach loop, you can "unpack"
the data from the struct before using it.
std.typecons.Typedef!(T) might also help--As I understand, it'll
make a type alias that's considered by the compiler to be a
separate data type.
June 02, 2013
On Sunday, 2 June 2013 at 05:52:11 UTC, Morning Song wrote:
> On Sunday, 2 June 2013 at 05:27:08 UTC, Carl wrote:
>> I am writing a class to act like a database or cache. I want to enable looping with foreach, but I need two separate opApply methods. One for the internal looping through raw data and a second for looping through the cached data (strings for example).
>>
>> Is there a way to scope the first opApply (looping through raw data) to only be accessible inside the class? This would be beneficial to prevent someone from accidentally looping through raw data instead of their cached objects.
>
> If the delegates to the opApply have different parameters (I.e.
> it's actually a different data type getting passed to the foreach
> loop), then method overloading will take care of it; just mark
> one of them private.
>
> If they both need to get sent the same kind of data, it's a hack,
> but you could try encapsulating the data for the raw one inside a
> private struct. Then, inside the foreach loop, you can "unpack"
> the data from the struct before using it.
> std.typecons.Typedef!(T) might also help--As I understand, it'll
> make a type alias that's considered by the compiler to be a
> separate data type.

Well I will encapsulate the raw data in a struct or class if all else fails. However, when making opApply private still allowed me to use foreach outside of its scope...
June 02, 2013
From http://dlang.org/attribute.html
"Private means that only members of the enclosing class can access the member, or members and functions in the same module as the enclosing class."

It appears my original code works, opApply being private. However, I was doing my tests with a unittest block in the same module. Do people commonly use a designated module for these types of unittests? Or does D have a way of isolating a unittest from the module it is declared in?
June 02, 2013
In D a private method in same module can act like C++ friend method.