Archived

This forum has been archived. Please start a new discussion on GitHub.

Minor RFE : Additional accessor metadata for use with protected

We are in the process of converting many of our classes classes to "protected" for immutability (among other application-centric reasons).

The most straightforward way would be an ["immutable"] or ["const"] directive at the class level, which would require there to be a getter generated for each field. However, there may be some internal reason that classes can't be immutable in Ice.

So, at the moment we're looking to use a combination of ["java:getset"] and ["protected"] in Java. The combination provides a convenient solution. First, "getset" can be used to have the accessors in place while developers upgrade. "protected" can then be added to force use of the accessors. And finally, "getset" can be removed and only the getters implemented to have immutable types.

I haven't used the C# mapping extensively, but I imagine the "property" directive would allow for a similar migration path.


Would it be possible to have similar directives in the other languages?


We began using Ice before "protected" was added and the cost of migrating to non-public fields has been fairly high. Having accessors generated automatically with the "protected" metadata costs little initially (other than the extra method invocation) with the option of manually implementing the accessors later for immutability or even "laziy-loaded" semantics.

One note: when using "protected" in the python mapping, a __getattr__ method can also be generated in addition to the accessors to prevent any code changes:
      def __getattr__(self, name):
          """
          Reroutes all access to object.field through object.getField() or object.isField()
          """
          field  = "_" + name
          capitalized = name[0].capitalize() + name[1:]
          getter = "get" + capitalized
          questn = "is" + capitalized
          if hasattr(self, field):
              if hasattr(self, getter):
                  method = getattr(self, getter)
                  return method()
              elif hasattr(self, questn):
                  method = getattr(self, questn)
                  return method()
          raise AttributeError("'%s' object has no attribute '%s' or '%s'" % (self.__class__.__name__, getter, questn))