Creating a child class instance doesn't invoke the parent class new() function

7 views
Skip to first unread message

Yegappan Lakshmanan

unread,
Jan 14, 2023, 9:52:12 PM1/14/23
to vim_dev
Hi,

When creating an instance of a child class, the parent class new() function is
not invoked. How do we invoke the parent class new() function from a child
class new() function to perform some initialization?

Regards,
Yegappan

Bram Moolenaar

unread,
Jan 15, 2023, 3:19:29 PM1/15/23
to vim...@googlegroups.com, Yegappan Lakshmanan
At this moment you can't. The simplest is to move the initializations
into a separate function and call it from any new() function where it is
to be used.

I could not find a simple and consistent way of calling a constructor
of the parent class. In some languages it is mandatory, often there are
restrictions and requirements for the order in which things happen.
Keep in mind there may be several constructors to choose from.

In most cases this should be fine, but perhaps sometimes you want to
make sure that the parent class always gets initialzed in a certain way,
and extending it should not make it possible to skip that. Is that
important enough to add a rule for?

These things quickly get complicated. For example the TypeScript
documentation says: "Each derived class that contains a constructor
function must call super() which will execute the constructor of the
base class. What’s more, before we ever access a property on this in a
constructor body, we have to call super(). This is an important rule
that TypeScript will enforce."

The simplest would be that the parent constructor is always called
first. However, it usually has arguments and those need to be passed
somehow.

Is there a language where this works nicely and we do it like that in
Vim?

--
hundred-and-one symptoms of being an internet addict:
19. All of your friends have an @ in their names.

/// Bram Moolenaar -- Br...@Moolenaar.net -- http://www.Moolenaar.net \\\
/// \\\
\\\ sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ ///
\\\ help me help AIDS victims -- http://ICCF-Holland.org ///

Christopher Plewright

unread,
Jan 15, 2023, 5:27:45 PM1/15/23
to vim_dev
On Monday, 16 January 2023 at 07:19:29 UTC+11 Bram Moolenaar wrote:

The simplest would be that the parent constructor is always called
first. However, it usually has arguments and those need to be passed
somehow.

Is there a language where this works nicely and we do it like that in
Vim?


Java uses the special keyword function "super()".    Also, I like how the Java compiler silently inserts a call to the super() constructor (the default no-argument constructor) into the child class constructor automatically if the developer doesn't manually include any call to super.   This is a nice convenience.
The developer can manually write the call to super() at the top of their child class constructor for clarity, and then the compiled byte code looks identical to the silently inserted call to super().  

But, as you said, often the developer wants to include arguments in the constructor, so in java, its up to the developer to write the call to super(...args.list..), and in that case the compiler doesn't silently insert a call to the zero-arg constructor. 

In case the parent class does not have a no-arg constructor, for example, it only has an argument list constructor, and if the child constructor constructor does not manually call that super(arg-list) constructor - then the compiler throws a warning.

That works OK, but the compiler needs to be able to check.

One idea, perhaps, for vim9, instead of introducing the super() keyword function,  is to enable the developer to just write a call to  ParentClassName.new(..args.if.any..),  without needing a left hand side assignment.   I think this is clear and explicit.
Reply all
Reply to author
Forward
0 new messages