typesystem
Module implementing core rules in Julia's dispatch system. For reference, check Jeff Bezanson's PhD thesis at https://github.com/JeffBezanson/phdthesis/blob/master/main.pdf
Sig
Represent a Tuple type with extra features, mostly used for method signature dispatching
Source code in gamma/dispatch/typesystem.py
issubtype(_type, _super)
Check if _type
is a subtype of _super
.
Arguments are either Type
(including parameterized Type) or Sig
(signature).
Follow rules in 4.2.2 in Bezanson's thesis.
Generic (aka parametric) types are invariant. For instance:
issubtype(List[int], List[object]) == False
issubtype(List[int], List[int]) == True
issubtype(list, List[object]) == False
The exceptions are
typing.Any
is treated likeobject
-
Tuple
: these are covariant. Eg:issubtype(Tuple[Foo], Tuple[Super]) == True
whereFoo -> Super
-
Union
: match if there's a covariant intersection, including non-union typesissubtype(Foo, Union[str, Super]) == True
whereFoo -> Super
issubtype(str, Union[str, Super]) == True
-
typing.Type
: is covariant on the type argument.type
andtyping.Type
are treated astyping.Type[object]
issubtype(typing.Type[str], typing.Type[object]) == True
issubtype(typing.Type[str], type) == True
Since Python don't tag container instances with the type paramemeters
(eg. type([1,2,3]) == list
) this means that we can't dispatch lists as we would
with arrays in Julia. The multiple dispatch system must then erase method signature
generic information for such container types.
See the parametric
module for a way to declare and dispatch on parametric types.
Also, we don't currently support the equivalent of UnionAll
. This is not an issue
since we don't support parametric dispatch, ie. TypeVar
s in method signatures.
Source code in gamma/dispatch/typesystem.py
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 |
|
pad_varargs(a, b)
Extract Tuple args and pad with object
, accounting for varargs
Source code in gamma/dispatch/typesystem.py
signatures_from(func)
Parse a callable to extract the dispatchable type tuple.