Can override methods/constants but not fields?

Monkey Forums/Monkey Bug Reports/Can override methods/constants but not fields?

MikeHart(Posted 2014) [#1]
This makes no sense to me. When I implement an interface in the extended class, I can override methods of the base class and constants, but not fields. I get this error:

Error : Field 'name' in class obj1 overrides existing declaration in class baseObj





ImmutableOctet(SKNG)(Posted 2014) [#2]
This is by design. You're literally adding a field with the same name, which isn't allowed.

Your options are:

* Having a normal field, then using constructors to assign that field's values. This isn't really a good idea when the value is always the same per-class.
* Making a method/property which returns the name. (The best option if it's a single value per-class)
* Making a constant and overloading everywhere. (Not really the best approach without further design changes)
* Doing the same as the constant option, but with a global variable for the sake of optimization on some targets.

I'm not one for in-line assignment of fields, but I have been guilty of it in the past (Too many constructors). That being said, it's not really meant to be used like this.

My suggestion (Assuming you just want one name per-class) is to make a global or constant variable containing the name you want, then just have the classes implement a property which returns said variable per-class.

Here's an example; my 'vector' module's 'Vector2D' class. I was using a constant before, which was done for the sake of long-term memory efficiency, you might want to use globals (Though, generic/template classes will make one per generated class). I'm currently just using globals outside of my classes as a cache. Also notice how I use this setup on this line, and this line.

EDIT: By the way, just curious, but why are you using 'Self' in front of method calls? I thought the point was to be unambiguous about field or property names (Also done with conflicting local names). That's just how I took it.


MikeHart(Posted 2014) [#3]
By design? But why only when i add an interface?

Using self became a habit of mine when i was dealing with name conflicts in the past.


marksibly(Posted 2014) [#4]
This was due to one of the targets - probably html5, which I think wiped out super class fields when you declared one with the same name in a subclass.

It's probably fixable with some (more) ugly name mangling, but it's low priority.


ImmutableOctet(SKNG)(Posted 2014) [#5]
@MikeHart: This happens with or without an interface, that interface wasn't causing this particular issue. And as my original post was referring to, this isn't even the most optimal (Or straight forward) way of doing this.


MikeHart(Posted 2014) [#6]
-