SUMSEC Article

Aliases

Aliases

Aliases

An alias is an alternative name for an existing QL entity.

别名是现有QL实体的替代名称。

Once you’ve defined an alias, you can use that new name to refer to the entity in the current module’s namespace.

一旦你定义了一个别名,你就可以使用这个新名字来引用当前模块命名空间中的实体。


Defining an alias定义别名

You can define an alias in the body of any module. To do this, you should specify:

您可以在任何模块的主体中定义别名。要做到这一点,您应该指定:

  1. The keyword module, class, or predicate to define an alias for a module, type, or non-member predicate respectively.

    关键字,或分别定义模块类型非成员谓词的别名。module``class``predicate

  2. The name of the alias. This should be a valid name for that kind of entity. For example, a valid predicate alias starts with a lowercase letter.

    别名的名字。这应该是此类实体的有效名称。例如,有效的谓词别名以小写字母开头。

  3. A reference to the QL entity. This includes the original name of the entity and, for predicates, the arity of the predicate.

    对QL实体的引用。这包括实体的原始名称,对于谓词,包括谓词的原名。

You can also annotate an alias. See the list of annotations available for aliases.

您还可以注释别名。请参阅可用于别名的注释列表。

Note that these annotations apply to the name introduced by the alias (and not the underlying QL entity itself). For example, an alias can have different visibility to the name that it aliases.

请注意,这些注释适用于别名引入的名称(而不是基础 QL 实体本身)。例如,别名可以与它所别名的名称具有不同的可见性。


Module aliases模块别名

Use the following syntax to define an alias for a module:

使用以下语法定义模块的别名:

module ModAlias = ModuleName;

For example, if you create a new module NewVersion that is an updated version of OldVersion, you could deprecate the name OldVersion as follows:

例如,如果您创建了更新版本的新模块,则可以将名称弃用如下:NewVersion``OldVersion``OldVersion

deprecated module OldVersion = NewVersion;

That way both names resolve to the same module, but if you use the name OldVersion, a deprecation warning is displayed.

这样,两个名称都会解决到同一模块,但如果您使用该名称,将显示弃用警告。OldVersion


Type aliases类型别名

Use the following syntax to define an alias for a type:

使用以下语法定义一种类型的别名:

class TypeAlias = TypeName;

Note that class is just a keyword. You can define an alias for any type—namely, primitive types, database types and user-defined classes.

请注意,这只是一个关键字。您可以为任何类型定义别名,即原始类型数据库类型和用户定义的类class

For example, you can use an alias to abbreviate the name of the primitive type boolean to bool:

例如,可以使用别名将基元类型boolean的名称缩写为bool

class bool = boolean;

Or, to use a class OneTwo defined in a module M in OneTwoThreeLib.qll, you could create an alias to use the shorter name OT instead:

或者,如果要使用OneTwoThreeLib.qll中M模块中定义的OneTwo类,你可以创建一个别名,使用更短的名字OT来代替。

import OneTwoThreeLib

class OT = M::OneTwo;

...

from OT ot
select ot

image-20210316160600475


Predicate aliases谓词别名

Use the following syntax to define an alias for a non-member predicate:

使用以下语法来定义非成员谓词的别名:

predicate PredAlias = PredicateName/Arity;

This works for predicates with or without result.

这适用于有结果或无结果的谓词。

For example, suppose you frequently use the following predicate, which calculates the successor of a positive integer less than ten:

例如,假设你经常使用下面的谓词,它计算一个小于10的正整数getSuccessor:

int getSuccessor(int i) {
  result = i + 1 and
  i in [1 .. 9]
}

You can use an alias to abbreviate the name to succ:

你可以用别名来缩写名字succ

predicate succ = getSuccessor/1;

示例:

int getSuccessor(int i) {
    result = i + 1 and
    i in [1 .. 9]
}

predicate succ = getSuccessor/1;

from int i
select succ(i)

image-20210316161029513

As an example of a predicate without result, suppose you have a predicate that holds for any positive integer less than ten:

另一个没有结果的谓词的例子,假设你有一个对任何小于10的正整数都成立的谓词。

predicate isSmall(int i) {
  i in [1 .. 9]
}

You could give the predicate a more descriptive name as follows:

您可以给谓词一个更描述性的名称如下:

predicate lessThanTen = isSmall/1;

示例:

predicate isSmall(int i) {
    i in [1 .. 9]
}

predicate lessThanTen = isSmall/1;

from int i
where lessThanTen(i)
select i

image-20210316162008023

评论

使用 GitHub 账号留言,讨论将同步为仓库 Issues