dolfinx icon indicating copy to clipboard operation
dolfinx copied to clipboard

Replace sentinel arguments and return values with std::optional

Open jhale opened this issue 1 year ago • 1 comments

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

jhale avatar Sep 17 '24 13:09 jhale

  • ~~set_bc: make $x_0$ optional (this will require some more changes, but should be able to remove a quite drastic code duplication problem)~~ (see #3434)
  • locate_entities_boundary and exterior_facet_indices combinable - see https://github.com/FEniCS/dolfinx/pull/3283
  • (locate_dofs_topological)

schnellerhase avatar Sep 19 '24 11:09 schnellerhase

This has been done for a number of interfaces and is recommended in the future.

jhale avatar Jul 21 '25 20:07 jhale