# Namespaced function names in CMake

**URL:** <https://discourse.cmake.org/t/namespaced-function-names-in-cmake/8187>\
**Category:** Code\
**Created:** [May 23, 2023, 2:19pm UTC](https://discourse.cmake.org/t/namespaced-function-names-in-cmake/8187 "2023-05-23T14:19:08Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Barry\_Revzin](https://discourse.cmake.org/user_avatar/discourse.cmake.org/barry_revzin/32/1283_2.png) [@Barry\_Revzin](https://discourse.cmake.org/u/Barry_Revzin)\
**Post date:** [May 23, 2023, 2:19pm UTC](https://discourse.cmake.org/t/namespaced-function-names-in-cmake/8187/1 "2023-05-23T14:19:08Z")

</div>

I can declare a qualified function:

```cmake
function(my::func)
    message(STATUS "Hi!")
endfunction()

```

And I can call it qualified… this way, which works as expected:

```cmake
cmake_language(CALL my::func)

```

but not this way

```cmake
my::func() # Parse error. Expected a command name, got unquoted argument with text "my::func"

```

The same idea is true for using a `.` instead of `::`, any punctuation basically.

Is this a fundamental parser limitation that it cannot interpret `my::func` as a command name?

Basically it’s just a question of whether I need to be declaring all my functions `my::func` (or `my.func`) or resorting to the C-style `my_func` to avoid collisions. Since CMake encourages colons in targets, I think it’d be nice if we could do it for functions too.

---

<div class="post-metadata">

**Author:** ![Barry\_Revzin](https://discourse.cmake.org/user_avatar/discourse.cmake.org/barry_revzin/32/1283_2.png) [@Barry\_Revzin](https://discourse.cmake.org/u/Barry_Revzin)\
**Post date:** [May 23, 2023, 2:51pm UTC](https://discourse.cmake.org/t/namespaced-function-names-in-cmake/8187/2 "2023-05-23T14:51:47Z")

</div>

For what it’s worth, just making this change in the lexer:

```diff
- [A-Za-z_][A-Za-z0-9_]* {
+ ([A-Za-z_][A-Za-z0-9_]*::)*[A-Za-z_][A-Za-z0-9_]* {
     lexer->token.type = cmListFileLexer_Token_Identifier;
     cmListFileLexerSetToken(lexer, yytext, yyleng);
     lexer->column += yyleng;

```

Lets `my::func` be usable as an identifier and thus makes `my::func(1 2 3)` work as desired.

---

<div class="post-metadata">

**Author:** ![craig.scott](https://discourse.cmake.org/user_avatar/discourse.cmake.org/craig.scott/32/20_2.png) [@craig.scott](https://discourse.cmake.org/u/craig.scott)\
**Post date:** [May 23, 2023, 10:22pm UTC](https://discourse.cmake.org/t/namespaced-function-names-in-cmake/8187/3 "2023-05-23T22:22:21Z")

</div>

I’d recommend sticking with `myproj_do_something` form for function names, at least for now. The ability to both define a function using `myproj::do_something` and being able to call it using `cmake_language(CALL)` was something I didn’t expect. But that does give me some more things to think about for a proposal I put up a little while back related to project namespaces: [https://gitlab.kitware.com/cmake/cmake/-/issues/22687](https://gitlab.kitware.com/cmake/cmake/-/issues/22687) (this is a strong reason to avoid trying to use `myproj::do_something` right now, it may gain meaning if that proposal moves forward and is expanded to cover functions as well).
