CMakePP / CMakePPLang

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

Member Function with Boolean Output Parameter Error #54

Closed zachcran closed 2 years ago

zachcran commented 2 years ago

Describe the bug When a class member function is defined with a boolean output variable, an overload error is thrown saying there is no suitable overload of the function because the blank output variable is deteremined to be of type desc, not bool. For example, a method defined as cpp_member(foo ExampleClass bool) called as ExampleClass(foo "${my_obj}" return_value) will throw the error No suitable overload of foo(exampleclass, desc).

It seems that this is an issue any time the parameter that should be a bool does not actually contain a valid bool.

To Reproduce Steps to reproduce the behavior:

  1. Create a class, say ExampleClass, with a method of the signature cpp_member(foo ExampleClass bool). The function does not have to do anything, but you could set the return value to TRUE and return it for a full example.
  2. Try to call foo() as if you are getting a return value.
  3. See the error

Expected behavior I would expect a boolean value to be returned without errors. I am pretty sure that cpp_member(foo ExampleClass bool) is a valid function signature.

Additional context I do not see this error for input boolean parameters if the input variable is a valid boolean. Here is a complete program to reproduce the issue:

include(cmakepp_lang/cmakepp_lang)

cpp_class(ExampleClass)

    cpp_constructor(CTOR ExampleClass)
    function("${CTOR}" self)
        message("-- ExampleClass is being constructed.")
    endfunction()

    cpp_member(foo ExampleClass bool)
    function("${foo}" self input_value)
        message("-- Received: ${input_value}")
    endfunction()

    cpp_member(bar ExampleClass bool)
    function("${bar}" self return_value)
        message("-- Returning TRUE")
        set(return_value TRUE)

        cpp_return("${return_value}")
    endfunction()

cpp_end_class()

# Instantiate the class
ExampleClass(CTOR my_class)

# Call the function with an boolean input parameter
ExampleClass(bar "${my_class}" TRUE)
# Prints "-- Received: TRUE"

# Call the function with an boolean input parameter using a variable
set(input_var OFF)
ExampleClass(foo "${my_class}" "${input_var}")
# Prints "-- Received: OFF"

# Call the function with an boolean input parameter using a variable with a bad bool value
# set(input_var "something_else")
# ExampleClass(foo "${my_class}" "${input_var}")
# Error: No suitable overload of bar(exampleclass, desc)

# Call the function with a boolean output parameter
ExampleClass(bar "${my_class}" returned_bool)
# Error: No suitable overload of bar(exampleclass, desc)
ryanmrichard commented 2 years ago

Does this work for any type? I'd have assumed that the type of returns is always going to be desc since you need to provide the name of the variable to assign the result to, not the type of the result. You have to document what the result's type is.

FWIW, because of how CMake itself works the type system ends up being a bit underwhelming (lots of desc and str). That said, I found its main use to be catching when you're given a list and weren't expecting one or vice versa.

zachcran commented 2 years ago

Ah, I was probably just thinking about it wrong then. It makes sense that the type of returns would always be desc with your explanation. I was thinking about the type that the variable would be holding, which you would just put in the documentation. It is probably worthwhile to add that explanation explicitly in the documentation, so I'll do that when I get a chance and tie it to this issue to close it.