Skip to content

Things that share a name

Audience: everyone who edits models · ~5 min read · Prerequisites: Completion menu, Find, Replace, Find Usages and Rename

What it is

Two different things can have the same name. A variable expr and a function expr in one file. A define("expr", 10) in one root and another define("expr", 30) in a second root of the same model. A building block of a language that is simply displayed as expr. When you write expr somewhere, the editor has to know which one you mean.

Where the plain name is enough, nothing changes: your files look exactly as before. Where it is not, the reference gets a short qualifier that says which one:

You see It means
expr The only thing called expr that fits here
exprˑfunction The function expr, not the define
exprˑdefine The define called expr in this root
other_root·exprˑdefine The define called expr in the root named other root
shared_lib·main·first_root·exprˑdefine A declaration in an attached library project

A qualifier leaves out whatever the reference shares with what it points at. A reference never names its own root, and never names its own project; when two declarations are equally close and of the same kind, the editor asks you to qualify.

A qualifier is part of the name. It is saved in the file as ordinary text, so it survives a reload and git diffs read normally. A qualifier is relative to where the reference sits, so a reference copied to a place where its target is not visible shows the "no declaration matches" error instead of silently pointing elsewhere. There are no hidden ids.

When you'd use it

  • Two declarations in your model really do share a name, and you want to refer to one of them.
  • The editor shows a warning that a name is ambiguous.
  • You copy a block of code between roots and a name now clashes.

How to do it

  1. Put the cursor in the cell where the reference goes and press Ctrl+Space.
  2. Type the name. If several declarations share it, each is listed on its own row with its kind and where it lives, for example Define in second_root and Function.
  3. Pick the one you mean. The editor writes the shortest qualifier that selects exactly that one; nothing is added when the plain name is enough.
  4. To see what a qualified reference refers to, double-click it. A note says which declaration it is.

If you type a name that is also the short name of a building block (a language whose block is called expr next to your variable expr), the menu stays open so you can choose, instead of inserting the block.

What the editor does for you

  • Adding a clashing declaration. If you add something that would silently take over an existing reference, that reference is rewritten to keep pointing at what it meant.
  • Removing a clash. When the other declaration goes away, the qualifier is dropped again.
  • Renaming. Rename changes only the declaration you are on and the references that mean it. A qualified reference keeps its qualifier.
  • Find usages and go to definition follow the declaration, not the text.
  • Running and generating code. A function and a value that share a name no longer overwrite each other when the program runs, and the generated code gives them different names.

What can go wrong

  • "… is ambiguous here". An unqualified name could mean more than one thing. Re-pick it from the completion menu.
  • "… is qualified, but no declaration matches". The thing it named was renamed, moved or deleted in a way the editor could not follow, for example in a file that was edited outside the editor. Re-pick the reference. The editor never silently points it at something else.
  • A reference to a declaration in an attached library project starts with that project's name. If that library is detached, or renamed, the reference becomes an error — it is never silently pointed at another project. When two attached libraries have the same name, each is named with a short tag of its folder (for example shared_lib_k3f9a) so detaching one can never re-point a reference at the other.

Two declarations with the same name

In a language where a declaration may not be repeated (the Name Clash sample's define), two declarations of the same kind in one scope cannot be told apart, even by a qualifier. So the editor stops you making them:

  • Renaming a declaration to a name another declaration of the same kind already has in that scope is refused. The name snaps back and an error explains why.
  • Pasting a declaration whose name is taken gets a numbered name (f becomes f_2), and so do the references inside the pasted text that mean it.

Declarations of different kinds, or in different scopes, may share a name freely. Languages where redeclaring a name is legal rebinding, such as Python, are not affected; each concept chooses. Clashes that arrive some other way (a live-session merge, an import, a file edited outside the editor) are still flagged by the duplicate-declaration check.

Try it

Open the sample Name Clash (same-named references) from the samples menu. Its README walks through each case.