Closed fperrad closed 11 months ago
type()
does not narrow, that's expected (because it could be used it complex expressions with side-effects), that's why I added the more restricted is
operator, which reliably has the shape VAR is TYPE
if VAR is TYPE
: that narrows str
from any
to string
. By assigning to str
, however, you drop the narrowing and revert the variable back to the declared type, which is any
. Performing generalized flow typing (that works across conditions, loops, function calls with side-effects, etc) would require a much more complex dataflow analysis than Teal's current single-pass compiler does.any
type is not a very useful or convenient one (any
accepts anything, but other types don't accept any
), and isn't really intended to give you the convenience of dynamically-typed programming. Your best bet for now is to check it with is
(or hard-cast with as
if you're sure of the type) and then assign to a local with the known type.many thanks for these clarifications.