FEniCS / dolfinx

Next generation FEniCS problem solving environment
https://fenicsproject.org
GNU Lesser General Public License v3.0
787 stars 182 forks source link

Replace sentinel arguments and return values with std::optional #3413

Open jhale opened 2 months ago

jhale commented 2 months ago

Describe new/missing feature

std::optional provides a modern way to express passing or returning value or not computed.

We are currently using many sentinel function arguments and return values including -1, nullptr and empty strings that require the user to read the docstring of the function to understand the argument or return value. These can be replaced with std::optional which is very explicit. An additional advantage is that nanobind has support for std::optional which results in consistent behaviour between the Python (typing.Optional) and C++ interfaces and a reduction in Python wrapper code complexity.

A first step would be identifying all typing.Optional arguments in Python which typically leads to a sentinel in C++.

Examples of use are here:

https://github.com/FEniCS/dolfinx/pull/3328 https://github.com/FEniCS/dolfinx/pull/3322

Suggested user interface

No response

schnellerhase commented 2 months ago