language-lawyeruml

UML Namespaces and "isDistinguishableFrom"


This operation is described on p. 91 of the UML 2.5.1 specification under section 7.8.9.7 "Operations" for the NamedElement description. I am trying to understand how it can be done as specified:

The query isDistinguishableFrom() determines whether two NamedElements may logically co-exist within a Namespace. By default, two named elements are distinguishable if (a) they have types neither of which is a kind of the other or (b) they have different names.

Which programming languages exist where two identical names can coexist in the same scope just because they have different types? Are there any?

Doesn't work in C++ apparently:

#include <iostream>

int main() {
  int a = 4711;
  const char *a = "Hello, Strange World";
  ++a;
  std::cout << a << std::endl;
  return 0;
}
  
/////////

bob@my_laptop:~/code$ g++ -c strange.cpp 
strange.cpp: In function ‘int main()’:
strange.cpp:6:15: error: conflicting declaration ‘const char* a’
    6 |   const char *a = "Hello, Strange World";
      |               ^
strange.cpp:5:7: note: previous declaration as ‘int a’
    5 |   int a = 4711;
      |       ^

Solution

  • If you read further you'll find the OCL specification for this same constraint.

    body: (self.oclIsKindOf(n.oclType()) or n.oclIsKindOf(self.oclType())) implies
    ns.getNamesOfMember(self)->intersection(ns.getNamesOfMember(n))->isEmpty()
    

    In this formal expression we see that the types mentioned in the plaintext explanation refer to .oclType()

    If we then check the OCL specification we find

    oclType() : Classifier
    Evaluates to the type of which self is an instance.
    post: self.oclIsTypeOf(result)

    Since this OCL constraint is written in the metamodel of UML it is clear that this refers to the Meta-type of the element, and not the classifier known as type.

    So this means that within a namespace it is allowed to have

    Since neither Class, Association or Operation are a KindOf each other.

    It is not allowed to have

    Since Enumeration is a KindOf Datatype

    This corresponds pretty much to the way the naming constraints are implemented in most programming languages.