Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

instantiate inherited class #272

Open
RMHogervorst opened this issue Jan 23, 2023 · 3 comments
Open

instantiate inherited class #272

RMHogervorst opened this issue Jan 23, 2023 · 3 comments

Comments

@RMHogervorst
Copy link

I have a base class with a initialize function.
What I'd like to do is for that initialize function to create and return a child class ( a class that inherits from the base class.)

c <- baseclass$new("blablabla") 
class(c) # "BaseClass",  "BlablablaClass"

Unfortunately this does not work, I get the baseclass only back.

Right now I have to create a function that instantiates a class but it would be so much cleaner if that could happen from the baseclass.

Is this possible?

@wch
Copy link
Member

wch commented Jan 23, 2023

Sorry, I don't think you can do this. When you call $new(), it always returns an instance of the class that $new() is called on.

Why not just call childclass$new()?

@RMHogervorst
Copy link
Author

Yeah I was afraid of that.

why not just call childclass$new?

  1. I want to have one interface, the user shouldn't care about which specific childclass is called,
  2. Legacy reasons: there used to be 1 class that loaded specific configuration on initialisation (but in a clunky way).

@RMHogervorst
Copy link
Author

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants