[Gambas-user] How to stop "Greedy" constructors consuming parameters
Benoît Minisini
gambas at ...1...
Mon Apr 29 16:34:40 CEST 2013
Le 20/04/2013 13:18, Bruce a écrit :
> On Sat, 2013-04-20 at 11:36 +0200, Fabien Bodard wrote:
>> The check is not done for the "_new" special method, as the inheritance of
>> _new is special (see "inheritance and constructor" in the "Gambas object
>> model" documentation on the wiki).
>
> Yep, I know that, but Ian's comment was about overrides for "normal"
> methods. Which still for the sake of sanity I could never understand the
> need for a total ban on signature changes.
>
> I can appreciate that the return type should to some extent be
> "prevented" from overrides, although even that is questionable in cases
> like abstract factories. As I said at the time, I could not see, nor
> have experienced a situation where gambas has failed where a method is
> overridden in a child class that has a requirement for a different
> signature.
>
> In particular, where an ancestral non-instantiable root class has no
> implementation of some method "XXXX" and the root class is abstract, it
> is fairly easy to specify a method in that class that must be
> overridden. In other words a stub that looks something like this
>
> Public Sub XXXX(...)
> Error.Raise(Subst("Incomplete override &1", Object.Class(Me).Name))
> End
>
> So now I have declared a method in the root class, which MUST be
> overridden in the instantiable child classes, by some method which may
> require {0,1,2,... many} parameters. As of the fix for Issue 78 this
> doesn't work "nicely", because every override method in a child class
> MUST use the signature "Public Sub XXXX(...)"
>
> The solution I "suggested" to Ian is based on optionally compiling out
> the signature check in gbx-class.c which as I said has not failed for
> about 2 years. I would really love to see an example of where this
> approach could fail because I am still running and promulgating a
> modified version of Gambas locally and to all the clients that has this
> patch in it.
>
> If this sounds like a bit of a rave then I apologise, but "for the sake
> of sanity"...
>
> Anyway, so much for the coffee break, back to rewriting the 64 classes
> to use Ian's "_new()/Init()" approach. Grrrr!
>
> cheers
> Bruce
>
The signature check is actually a bug fix.
Let's suppose that class B inherits class A, and that A has a method
foo() which is reimplemented in B with a different signature.
Then, calling X.foo() may lead to a crash if X is declared as an A and
is actually a B, because of an internal optimization that replaces an
expensive table symbol lookup by a direct method access.
If *all* of the classes of your hierarchy wants a 'Source' and 'Name'
argument, then these arguments must be defined in the root class
constructor, not in each one!
If theses twwo arguments must be handled differently between classes,
then add a public method to handle them. Prefix its name with an
underscore, to indicate that it must not be called outside of the
implementation.
' Root class
Public Sub _new(Source as String[], Name as String)
_Init(Source, Name)
End
' Any child class
Public Sub _Init(Source as String[], Name as String)
...
End
Does it fit your needs?
--
Benoît Minisini
More information about the User
mailing list