# Using a function keyword in a generator expression

**URL:** <https://discourse.cmake.org/t/using-a-function-keyword-in-a-generator-expression/4601>\
**Category:** Code\
**Created:** [December 6, 2021, 9:30am UTC](https://discourse.cmake.org/t/using-a-function-keyword-in-a-generator-expression/4601 "2021-12-06T09:30:55Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![EosPengwern](https://discourse.cmake.org/user_avatar/discourse.cmake.org/eospengwern/32/3904_2.png) [@EosPengwern](https://discourse.cmake.org/u/EosPengwern)\
**Post date:** [December 6, 2021, 9:30am UTC](https://discourse.cmake.org/t/using-a-function-keyword-in-a-generator-expression/4601/1 "2021-12-06T09:30:55Z")

</div>

What’s the correct syntax for using a function argument as a conditional operator in a generator expression? The example below encapsulates what I’m trying to achieve, but the syntax here doesn’t work, and neither do various different (and less readable) versions that I’ve tried:

```auto
    function(set_SWIG_flags)

        set (prefix SWIGLIB)
        set (noValues PY2 PY3)
        cmake_parse_arguments(${prefix} "${noValues}" "${singleValues}" "${multiValues}" ${ARGN})

        set(CMAKE_SWIG_FLAGS -c++
                             -builtin
                             $<SWIGLIB_PY2:-classic>
                             PARENT_SCOPE)

    endfunction()

```

I’m sure this must be possible, but I’d appreciate a push in the right direction.

---

<div class="post-metadata">

**Author:** ![marc.chevrier](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/m/ecb155/32.png) [@marc.chevrier](https://discourse.cmake.org/u/marc.chevrier)\
**Post date:** [December 6, 2021, 10:37am UTC](https://discourse.cmake.org/t/using-a-function-keyword-in-a-generator-expression/4601/2 "2021-12-06T10:37:45Z")

</div>

The variable must be expanded:

```cmake
$<$<BOOL:${SWIGLIB_PY2}>:-classic>

```

But, here, genex are not required. You can also do:

```cmake
set(MY_SWIG_FLAGS -c++ -builtin)
if(SWIG_PY2)
  list(APPEND MY_SWIG_FLAGS -classic)
endif()
set(CMAKE_SWIG_FLAGS ${MY_SWIG_FLAGS} PARENT_SCOPE)

```

---

<div class="post-metadata">

**Author:** ![EosPengwern](https://discourse.cmake.org/user_avatar/discourse.cmake.org/eospengwern/32/3904_2.png) [@EosPengwern](https://discourse.cmake.org/u/EosPengwern)\
**Post date:** [December 6, 2021, 10:52am UTC](https://discourse.cmake.org/t/using-a-function-keyword-in-a-generator-expression/4601/3 "2021-12-06T10:52:01Z")

</div>

Thank you; I think your second suggestion is actually much better. I was trying to be too clever for my own good.
