www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Scoped Operator Overloading

reply "Carl" <carl email.com> writes:
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.
Jun 01 2013
next sibling parent reply "Morning Song" <onetruequinn hotmail.com> writes:
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.
Jun 01 2013
parent reply "Carl" <carl email.com> writes:
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...
Jun 01 2013
parent "Carl" <carl email.com> writes:
 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?
Jun 01 2013
prev sibling parent "Michael" <pr m1xa.com> writes:
In D a private method in same module can act like C++ friend 
method.
Jun 02 2013