Kotlin extend class instance inline

35 Views Asked by At

Not even sure if this is possible in Kotlin, nor am I sure if I am explaining this correctly.

Basically, I am trying to create a property which is a valid instance of an object, and add arbitrary properties to it in the object definition.

The purpose of this is to replicate the hierarchy of my XML-based Android view in a Kotlin object.

In TypeScript, the equivalent of what I am trying to do would look like this:


private viewPane1: (Layout & { txt1: Textbox, txt2: Textbox });

The idea is that viewPane1 itself should be a valid instance of the Layout class, while txt1 and txt2 should be child properties that would be initialized separately and be specific to my class.

Is something like this possible in Kotlin?

(I know that there are "correct" ways to do this, including modern-day view binding, but this is a very legacy application so this is as good as is accessible to me).

0

There are 0 best solutions below