Different behaviours in the same function based on trait properties until specialization is stabilized?

30 views Asked by At

I have the following problem.

I want to write a generic function that process some geometry. The geometry may, or may not have normals. If it has defined normals, it should use one set of functionality if it does not, it should use another. For both cases the output would be identical (up to numerical differences of course).

I am trying to implement such functionality, options seem to be:

  • Implement one common functionality in one function, then implement 2 wrapping functions, one for each case. I don't like this one because I have lot's of generics, it will make integrating this functionality a nightmare as all things depending on it will need to have 2 implementations.
  • Use specialization, as of now, it is not stabilized.
  • Make a trait and have it return a dummy boolean just to check which of the two cases is the current one.
  • This is small but unecessary runtime overhead, and demands types created by the user implement the trait, which is inconvenient.

Is there some work around to get a single function to switch behaviours while not forcing the user to care explicitly about it?

0

There are 0 answers