Search code examples
umlfunction-pointers

How to represent a function/method type in a UML class diagram


Is there a standard way to represent a named function type in a UML class diagram?

By named function type, I mean a function or method type with a certain signature (parameters, return type, possibly generic parameters), to which an alias has been assigned, which is then referenced throughout the class model.

The concept is known in various different languages, e.g.:

C#:

public delegate string GetTextFunc(int value, bool modify);

TypeScript:

type GetTextFunc = (value: number, modify: boolean) => string;

(Yes, there are differences in implementation, but for the sake of a class model, these should be irrelevant.)

How do I represent such a type in a UML class diagram? As so far, I could not find any standardized answer, I have thought about shoehorning them into a class element while making use of custom stereotypes to express the meaning, e.g:

function type as UML class element

However, in this question, I am explicitly looking for the standardized way to do this, if there is any.


Solution

  • There is no standard way to represent functions or function types in UML.

    But a function type describes a class of functions, so a class is definitively the way to go. The use of a non-standard stereotype «function» would the facilitate to distinguish this very special kind of class (see also this other answer on a related but not identical question).

    Regarding the parameters, your way to go could be defended. However, semantically it is questionable since they are not really public properties, and exist only for the time of one specific invocation. A more consistent approach would be to have no attributes (except perhaps private static ones, as these are the only ones who may stand for a function independently of the calls) and use instead a pseudo operation (like operator()(...) in C++) with the exact signature of your function. Semantically this would be consistant with the use of inheritance between a particular function and its function type. It would also be consistent with all the semantics of sequence diagrams.

    Keep in mind that operations are named elements, so you would have to name the pseudo-operation, even if in your code there would be no such name.