cast(public)

dsimcha dsimcha at yahoo.com
Fri Jul 17 12:55:43 PDT 2009


== Quote from Nick Sabalausky (a at a.a)'s article
> "dsimcha" <dsimcha at yahoo.com> wrote in message
> news:h3q5td$2jqa$1 at digitalmars.com...
> >I know I've probably mentioned this one here before, but it was buried in
> >long
> > threads.
> >
> > Could we put a feature in the language that allows private member
> > variables to
> > be cast to public?  The idea is that, if a class/struct designer makes
> > something private, they're saying it's a bad idea to mess with it, and
> > that
> > you do so at your own risk.  However, I think there needs to be a back
> > door to
> > cowboy this one, because otherwise private/protected is just too
> > restrictive
> > for a language like D.  It would work something like this:
> >
> > struct Foo {
> >    private uint bar;
> > }
> >
> > void main() {
> >    Foo foo;
> >    foo.bar++;  // error
> >    (cast(public) foo.bar)++;  // Works.
> > }
> I don't see a real legitimate point to this. If you need something from a
> module not provided by a public interface (or protected sub-classing) than
> that needs to be properly added to the module's interface. Otherwise you're
> just asking for your code to be broken (in a way that may *or* may not be
> fixable) the moment the module you're hacking into is updated. Why hack it,
> when you could just make a proper added feature? Sure, there may be
> source-not-available stuff, but if you need some extra feature from a
> library that doesn't have source available, and the lib's developers aren't
> receptive to your need, then you're just simply using the wrong library
> anyway.

What if you want to make the class/struct do something that is enough of a corner
case that making a formal interface to do what you want would bloat the API for
the other 99.9% of use cases?  IMHO at a more general level sometimes the best
thing to do when trying to solve corner cases is to just hack it, so that you can
simplify the more normal cases.



More information about the Digitalmars-d mailing list