CMakePP / CMakePPLang

Object-oriented extension to the CMake language.
http://cmakepp.github.io/CMakePPLang/
Apache License 2.0
11 stars 4 forks source link

Can't Call Base Class's Methods #125

Open ryanmrichard opened 9 months ago

ryanmrichard commented 9 months ago

Describe the bug I have a simple two class hierarchy, Foo which derives from Bar. In Bar I define a method to_str meant to print Bar. In Foo I try to override to_str so that it first calls Bar::to_str then prints Foo's additional state; instead I get infinite recursion.

To Reproduce MWE:

cpp_class(Bar)
    cpp_constructor(CTOR Bar str)
    function("${CTOR}" self _ctor_value)
        Bar(SET "${self}" _value "${_ctor_value}")
    endfunction()

    cpp_member(to_str Bar desc)
    function("${to_str}" self _ts_result)
        Bar(GET "${self}" _ts_value "_value")
        set("${_ts_result}" "Value = ${_ts_value}" PARENT_SCOPE)
    endfunction()

    cpp_attr(Bar _value)
cpp_end_class()

cpp_class(Foo Bar)
    cpp_constructor(CTOR Foo str)
    function("${CTOR}" self _ctor_value)
        # Related, is possible to call the base ctor?
        Foo(SET "${self}" _other_value "${_ctor_value}")
    endfunction()

    cpp_member(to_str Foo desc)
    function("${to_str}" self _ts_result)
        Bar(to_str "${self}" _ts_base)
        Foo(GET "${self}" _ts_value "_other_value")
        set(
            "${_ts_result}"
            "${_ts_base}\nOther Value = ${_ts_value}"
            PARENT_SCOPE
        )
    endfunction()

    cpp_attr(Foo _other_value)
cpp_end_class()

Bar(CTOR my_bar "hello")
Bar(to_str "${my_bar}" as_a_string)
message("${as_a_string}") # Prints "Value = hello"

Foo(CTOR my_foo "world")
Foo(to_str "${my_foo}" as_a_string)
message("${as_a_string}") #Crashes here

Expected behavior I would expect the code to print "Value = \nOther Value = world"

Additional context N/A

AutonomicPerfectionist commented 9 months ago

I think we would need to introduce a super() construct for this to work. Currently, to support polymorphism the subclass completely replaces the vtable entry (terminology for other languages, don't remember what we call it) for the to_str method so that you can pass a Foo into something expecting a Bar and still get the overridden functionality.

ryanmrichard commented 9 months ago

I think introducing super() to solve this is a great idea!