Forcing unique names (enhancing the Calculator tutorial) (including answer by Vaclav Pech)

How can we force uniqueness of the input?
In the Calculator tutorial it is possible to add more than one input with the same name, e.g.
input width
input height
input width

Answer (by Vaclav Pech):
A non-typesystem rule would perhaps be the best fit here.
The checking rule would compare the names of the other input fields to the current input field and raise an error it the name already exists. You may take inspiration from BaseLanguage, for example:
checking rule check_BaseMethodDeclarationParameterUniqueNames {            
  applicable for concept = BaseMethodDeclaration as baseMethodDeclaration  
  overrides false            

  do {         
    set<string> names = new hashset<string>;
    baseMethodDeclaration.parameter.forEach({~it =>
      if (names.contains(it.name)) {
        error "Parameter names must be unique" -> it;
      } else {
        names.add(it.name);
      }
    });
  }            
}   

Also the robot Kaja sample project uses checking rules, such as RoutineUniqueness, to ensure name uniqueness for scripts, libraries and routines.

Please sign in to leave a comment.