# Feature to disable empty parameters resolving into unset function arguments

**URL:** <https://discourse.cmake.org/t/feature-to-disable-empty-parameters-resolving-into-unset-function-arguments/6085>\
**Category:** Code\
**Created:** [July 15, 2022, 10:02am UTC](https://discourse.cmake.org/t/feature-to-disable-empty-parameters-resolving-into-unset-function-arguments/6085 "2022-07-15T10:02:18Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![gabyx](https://discourse.cmake.org/user_avatar/discourse.cmake.org/gabyx/32/2107_2.png) [@gabyx](https://discourse.cmake.org/u/gabyx)\
**Post date:** [July 15, 2022, 10:02am UTC](https://discourse.cmake.org/t/feature-to-disable-empty-parameters-resolving-into-unset-function-arguments/6085/1 "2022-07-15T10:02:18Z")

</div>

I would love to see some progression in CMake syntaxt such that it gets more safe and better to use.

Such as to disable `set(A ${var})` which accidentially unsets variable `A` if `var` is empty.  
Instead the the `set` should always resolve into setting an empty string, and not unsetting a variable.  
That would greatly improve errors, e.g.

```cmake

set(var ${SOME_GLOBAL}) # global might be unset

if(var STREQUAL "")
  # this if is never reached if `SOME_GLOBAL` is empty...
endif()

```

Also it would be nice if any command evaluating a `var-name` argument would FATAL\_ERROR when this variable is not defined. e.g.

```cmake

if(missingVar STREQUAL "asd") # Direct error here: Variable `missingVar` is undefined.
endif()

```

---

<div class="post-metadata">

**Author:** ![trmagma](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/t/74df32/32.png) [@trmagma](https://discourse.cmake.org/u/trmagma)\
**Post date:** [July 15, 2022, 12:54pm UTC](https://discourse.cmake.org/t/feature-to-disable-empty-parameters-resolving-into-unset-function-arguments/6085/2 "2022-07-15T12:54:09Z")

</div>

You should use “${SOME\_GLOBAL}” instead of ${SOME\_GLOBAL}. So the value is set to the empty string instead of unsettling the value. In general it is good practice (and even sometimes necessary) to enclose variable access with “” (avoid splitting at white spaces resp. complete elimination if there are only white spaces)

You can check with if ( DEFINED ) if a variable is set.  
Just check out:

set ( a )  
set ( b “”)

if ( DEFINED a )  
message ( “a defined” )  
endif()

if ( DEFINED b )  
message ( “b defined” )  
endif()

---

<div class="post-metadata">

**Author:** ![mbee](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/m/50afbb/32.png) [@mbee](https://discourse.cmake.org/u/mbee)\
**Post date:** [August 21, 2022, 11:31am UTC](https://discourse.cmake.org/t/feature-to-disable-empty-parameters-resolving-into-unset-function-arguments/6085/3 "2022-08-21T11:31:04Z")

</div>

> You should use “${SOME\_GLOBAL}”

This is not enough. For example:

```auto
function(ttt ret)
   set(inner "")
   set(${ret} "${inner}" PARENT_SCOPE) 
endfunction()

ttt(ttt_ret)
message(${ttt_ret})

```

Now play with different versions of this code. For example:

- replace `"${inner}"` by `${inner}`
- initialize `ret` before using `ttt`; call `ttt(${ret})`. Vary:
  - `set(ret "") # in principle an unset, with at times quite interesting ramifications`
  - `set(ret "x")`

- make `ret` a read parameter of `ttt` and call `ttt("")`
- set `inner` to something not empty
- embed the message parameter in quotes
- combine all this in endless ways; nest your functions

More fun:

- instead/additionally to function parameter passing use `if`
- add `string(JSON ...)` to the game

Enjoy! (this is simply a big mud ball of incomprehensible corner cases)

p.s.: Did I mention lists?

---

<div class="post-metadata">

**Author:** ![gabyx](https://discourse.cmake.org/user_avatar/discourse.cmake.org/gabyx/32/2107_2.png) [@gabyx](https://discourse.cmake.org/u/gabyx)\
**Post date:** [August 21, 2022, 6:04pm UTC](https://discourse.cmake.org/t/feature-to-disable-empty-parameters-resolving-into-unset-function-arguments/6085/4 "2022-08-21T18:04:35Z")

</div>

Hm. Jeah it would be nice to close these mud holes such that devs do not get more frustrated about such basic things… Bit I habe no idea given the cirrcumstances about backward compat and policies. It needs a bold step to get rid of these akward scenarios…

---

<div class="post-metadata">

**Author:** ![benthevining](https://discourse.cmake.org/user_avatar/discourse.cmake.org/benthevining/32/1924_2.png) [@benthevining](https://discourse.cmake.org/u/benthevining)\
**Post date:** [August 21, 2022, 9:10pm UTC](https://discourse.cmake.org/t/feature-to-disable-empty-parameters-resolving-into-unset-function-arguments/6085/5 "2022-08-21T21:10:03Z")

</div>

```cmake
if(var STREQUAL "")

```

It would be much better to do this instead:

```cmake
if(NOT var)
    ...
endif()

```

---

<div class="post-metadata">

**Author:** ![hsattler](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/h/59ef9b/32.png) [@hsattler](https://discourse.cmake.org/u/hsattler)\
**Post date:** [August 22, 2022, 7:37am UTC](https://discourse.cmake.org/t/feature-to-disable-empty-parameters-resolving-into-unset-function-arguments/6085/6 "2022-08-22T07:37:31Z")

</div>

That’s not equal, the latter also matches some possibly valid values of `var`.

---

<div class="post-metadata">

**Author:** ![benthevining](https://discourse.cmake.org/user_avatar/discourse.cmake.org/benthevining/32/1924_2.png) [@benthevining](https://discourse.cmake.org/u/benthevining)\
**Post date:** [August 22, 2022, 7:44am UTC](https://discourse.cmake.org/t/feature-to-disable-empty-parameters-resolving-into-unset-function-arguments/6085/7 "2022-08-22T07:44:07Z")

</div>

You could also do

```cmake
if(NOT DEFINED var)
...
endif()

```
