Skip to content
This repository was archived by the owner on Apr 1, 2022. It is now read-only.

Tap Types

lianghe-houzz edited this page Jul 6, 2017 · 2 revisions

Tap support more precise types than PHP itself. For example, "12" + 34 is fine in PHP, but Tap will report an error because we don't think converting string to integer implicitly is safe. Another example is array. In classical PHP, array is almost everything except scalar type. It can be used as map, vector(list) or tuple, you won't know how it will be used unless reading the code. We made it explicit in Tap, so the interface is much clearer.

Scalar Types

  • int or integer
  • float or double - they are considered same in Tap
  • string
  • bool or boolean

Collection Types

  • vector<T> - the index key is integer starting from 0, and the elements should be of type T. Similar as vector in C++, list in Python
  • map<T1,T2> - the index key is usually int or string, all the key type should be of type T1, all the value type should be of same T2. Similar as map in C++, dict in Python.
  • tuple<T1,T2,..> - immutable tuple, the elements can be different.
  • array - the old catch-all type. Tap can't do much check for it. Try to avoid it when possible.

Note: for array literal, Tap will try to guess it's real type. // TODO: details

Union Types

Sometimes you will find it's convenient to make an function argument be "either type A or type B". In such case, you can use A|B. I personally don't think making a function accept like either string or array is a good idea. But it IS everywhere in PHP code.

One of the most important usage of union types is the ubiquitous nullable type like string|null. That is very tricky, even mature static languages like Java didn't handle it very well - you can pass null to any function who is expecting an object, and crash. For PHP, just think about how many times you see this on production: Call to a member function foo() on a non-object (null).

Tap can handle this tricky issue very well (hopefully). If you pass a null to a function who claims not prepared to accept null value, Tap will report an error. And for a variable $a whose type is string|null, I think you already see this pattern repeatedly:

if ($a !== null) {
  function_accept_string($a);  // this is ok
} else {
  echo "a is null";
}

Tap is smart enough to know the $a inside the first branch is impossible to be null. For more details about this, see Type Refine.

Special Types

  • void - should only appears as function return type when the function doesn't return anything.
  • any - the default type when there is no annotation. Tap can't do any check about it.
  • mixed

Clone this wiki locally