Proposal: A new syntax for string literal
Intro
Lists are central part of CMake.
Yet there is no native list literal in the language.
To define a list value you actually define a string with a special format, where each element is separated by a semicolon:
"a;b;c"
This is unintuitive, pretty ugly and gets messy when the list gets long and you want to line-break it.
I want to propose a new syntax for list literal which will be easily recognized by developers:
[a, b, c]
Justification
One might ask - "what do you need string literal? just define a variable as a list using set
!"
And usually it is true that you can almost get away with variables-only approach, but there are a few drawbacks:
- Variables pollutes the scope they are defined in
- There are some cases that variables don't cut it
Consider the set_target_properties
command:
set_target_properties(target1 target2 ...
PROPERTIES prop1 value1
prop2 value2 ...)
What if I want value1
or value2
to be a list?
A developer might wrongfully do this:
set_target_properties(target1 PROPERTIES
PUBLIC_HEADER foo.h bar.h)
But it would fail with an error about wrong number of arguments, since bar.h
is treated as a property name, to which no value has passed.
Even scarier is the fact that passing odd number arguments after the property name will just succeed:
set_target_properties(target1 PROPERTIES
PUBLIC_HEADER foo.h bar.h baz.h)
But PUBLIC_HEADER
of target1
will not contain bar.h
nor baz.h
. Instead you'll have a new property named bar.h
with a value set to baz.h
...
Even a variable won't help here - unless you wrap it in a double-quotes:
set_target_properties(target1 PROPERTIES
PUBLIC_HEADER "${MY_HEADERS}")
In short - native list literal will simplify the use of CMake and will improve readability:
set_target_properties(target1 PROPERTIES
PUBLIC_HEADER [foo.h, bar.h, baz.h])