docs: delete constraining-types.rst

(cherry picked from commit 142106bc99)
This commit is contained in:
james
2020-02-10 15:06:48 +00:00
parent afbb70af47
commit 1f84722d2f
2 changed files with 0 additions and 74 deletions

View File

@@ -10,5 +10,4 @@ Advanced QL
Topics on advanced uses of QL. These topics assume that you are familiar with QL and the basics of query writing.
- :doc:`Choosing appropriate ways to constrain types <constraining-types>`
- :doc:`Monotonic aggregates in QL <monotonic-aggregates>`

View File

@@ -1,73 +0,0 @@
Constraining types
==================
Type constraint methods
-----------------------
.. pull-quote::
Note
The examples below use the CodeQL library for Java. All libraries support using these methods to constrain variables, the only difference is in the names of the classes used.
There are several ways of imposing type constraints on variables:
- Use an ``instanceof`` test, for example:
.. code-block:: ql
import java
from Type t
where t instanceof Class
select t
- Use a cast, for example:
.. code-block:: ql
import java
from Type t
where t.(Class).getASupertype().hasName("List")
select t
- Set the variable equal to another variable with the required type using exists, for example:
.. code-block:: ql
import java
from Type t
where exists(Class c |
c = t
and c.getASupertype().hasName("List")
)
select t
- Pass the variable to a predicate that expects a variable of the required type, for example:
.. code-block:: ql
import java
predicate derivedFromList(Class c) {
c.getASupertype().hasName("List")
}
from Type t
where derivedFromList(t)
select t
All of these methods constrain the variable ``t`` to have values of type ``Class``.
Choosing between the methods
----------------------------
These methods have advantages and disadvantages depending on the extent to which you need to work with the variable as the new type:
- ``instanceof`` only checks that the variable has the required type.
- A cast gives you one, and only one, access to the variable as the new type.
- Creating a new variable with ``exists`` or a predicate parameter allows repeated access to the variable as the new type.
- Note that due to the ability to overload predicates in a class, predicates that belong to a class must be supplied with arguments of the exact type they specify, and so this technique will not work in that situation.