简体   繁体   中英

How does one define constants in SPL that can be shared across files?

A Custom operator can define constants in its state clause, but how does one define a constant that can be used across multiple operators, and across multiple files?

Ideally, I'm looking for a way to define constants in a namespace.

You can define a trivial SPL function to return a constant:

namespace my.name.space;
...
float64 MAX_LATITUDE() {return 90.0;}
...
composite MyMainComposite
{
...

This is automatically available throughout the namespace. Other than the parentheses, it works just like any constant wherever you use it. I haven't looked at the generated code in detail but I'm assuming that the SPL or C++ compiler will inline whatever it needs to, ensuring that there is no actual function call overhead at runtime.

There is currently no way to define constants in a namespace, so there is no way to define a constant once and use it in multiple SPL files.

For a single file, here are some options:

  • use mixed-mode and define the constants in Perl code
  • use composite parameter expressions:

   composite MyMainComposite {
     param
        expression<float64> $TimerInterval : 4.0;  // 4 seconds

Another option is compile-time options/parameters – see getCompileTimeValue() and getCompileTimeListValue() .

For SPL functions that exist in another file, you'll have to pass them as function arguments, or manually keep your code in sync.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM