Scoped Operator Overloading
Morning Song
onetruequinn at hotmail.com
Sat Jun 1 22:52:07 PDT 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.
More information about the Digitalmars-d-learn
mailing list