#Reuse functions defined in a class, in another class

1 messages · Page 1 of 1 (latest)

tardy monolith
#

I tried to do it this way,

import ballerina/io;

public isolated class Person {

    public final string name;

    public function init(string name) {
        self.name = name;
    }

    public function printGreeting() {
        io:println("Hello, " + self.name);
    }
}

# Description
public isolated class Student {
    *Person;

    function init(string name) {
        self.name = name;
    }

    public function printGreeting() {
        // Use Person class function implemnetation here
        return;
    }
}

public function main() {
    Student s = new ("Pratheek");
    s.printGreeting();
}

but it does not gives the desired result. I need to re use the exact code in the Person printGreeting() method in Student printGreeting() method without duplication.

#

Reuse functions defined in a class, in another class

sleek lodge
#

Object type inclusion (*Person) is a reference to only the types, not the implementation. In order to use the implementation, you would have to have a Person value.

E.g.,

public isolated class Student {
    *Person;

    private final Person person;

    function init(string name) {
        self.name = name;
        self.person = new(name);
    }

    public function printGreeting() {
        self.person.printGreeting();
        return;
    }
}
tardy monolith
#

Is this the only available solution? I thought of doing this way, but it feels like a workaround. Above code is not the actual issue I'm facing. I'll try this way on the original problem, and see if it works. @sleek lodge Thank you for the prompt response.

digital bone
#

I'm not OO-programmer but using composition instead of implementation inheritance is a normal practice and often the preferred way.

tardy monolith
frigid grove
#

@tardy monolith what you're asking for is implementation inheritance. We made an explicit design decision NOT to support that as that's is not a good way to write OO code that is easy to maintain and evolve.

YMMV but a philosophical position we took which will not change.

tardy monolith
frigid grove
#

BTW the easiest thing for you might be to put the reusable functions into a module, import that and call it from your object functions.

river nexus
#

@frigid grove I haven't looked at the Ballerina language spec in a while so I can't remember if it's supported, but in some other languages you can get some of what @tardy monolith is looking for by having a function defined as a value, and storing a reference to that function-by-value as a field within the object. Is that something Ballerina could do? Like, define the function in the module like you state, and then have it stored within the object?

frigid grove
#

Hi Ted- yes you can do that too if you like .. functions are first class values so certainly that works as well.

#

class Foo1 {
function (int x) returns float f = moduleFunc;
}

class Foo2 {
function (int x) returns float f = moduleFunc;
}

function moduleFunc (int x) returns float {
return x * 2.71828;
}

function caller() {
Foo1 f1 = new();
float res = f1.f(13);
Foo2 f2 = new();
res = f2.f(17);
}