简体   繁体   English

如何定义SPL中可以在文件之间共享的常量?

[英]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? 自定义运算符可以在其state子句中定义常量,但是如何定义可以在多个运算符和多个文件中使用的常量?

Ideally, I'm looking for a way to define constants in a namespace. 理想情况下,我正在寻找一种在名称空间中定义常量的方法。

You can define a trivial SPL function to return a constant: 您可以定义一个简单的SPL函数以返回常量:

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. 我没有详细研究生成的代码,但是我假设SPL或C ++编译器将内联它所需的任何内容,以确保在运行时没有实际的函数调用开销。

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. 当前无法在名称空间中定义常量,因此无法定义一次常量并将其在多个SPL文件中使用。

For a single file, here are some options: 对于单个文件,以下是一些选项:

  • use mixed-mode and define the constants in Perl code 使用混合模式并在Perl代码中定义常量
  • 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() . 另一个选择是编译时选项/参数-请参见getCompileTimeValue()和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. 对于存在于另一个文件中的SPL函数,您必须将它们作为函数参数传递,或手动使代码保持同步。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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