swift - dynamicType of optional chaining not the same as assignment -
optional chaining returns always optional value.
to reflect fact optional chaining can called on nil value, result of optional chaining call optional value, if property, method, or subscript querying returns nonoptional value.
why heck in playground type not optional?
let stringoptempty: string? = "" stringoptempty?.isempty // true stringoptempty?.isempty.dynamictype // bool.type
but following code is
let isok = stringoptempty?.isempty.dynamictype isok.dynamictype // optional<bool.type>.type
tldr;
the playground sidebar/column dynamically resolve expressions in playground, values assigned variables (mutables/immutables) or "free-floating" non-assigned values.
your first example applies dynamictype
value, resolve type of specific value (true.dynamictype
: bool.type
).
your second example, on other hand, applies dynamictype
variable (an immutable, i'll use variable here differ value), must have concrete type, , hence resolve type can hold kind of wrapped values (true
or false
) nil
(here, nil
is, specifically, optional<bool.type>.none
), no matter value variable holds. hence, dynamictype
resolve optional<bool.type>.type
in second example.
details
the value displayed in playground sidebar/column follows following display rules:
for assignment expression, value shown in sidebar value assigned, e.g.
var = 4 // shows '4' = 2 // shows '2' let b: () = (a = 3) /* shows '()': _value_ assigned 'b', _result_ of assignment 'a = 3', _side effect_ 'a' assigned value '3'. */
for expression contains no assignment, value shown in sidebar the result of expression, e.g.
true // shows 'true' 1 > 3 // shows 'false' let c = 3 c // shows '3' c.dynamictype // shows 'int.type'
in first example (lines 2-3), have no assignment, , playground dynamically resolve value(/result) of expression prior resolving dynamictype
of value. since we're dealing optionals, value either value of wrapped type (in case, true
), or value type specific .none
. if playground shows e.g. result of let a: int? = nil
nil
in sidebar, value shown in fact not same .none
(nil
) let b: string = nil
- for
let a: int? = nil
, value ofa
in factoptional<int.type>.none
, - whereas
let b: string? = nil
, value ofb
optional<string.type>.none
with in mind, it's natural resolved dynamictype
of non-nil
value concrete wrapped type (in example, bool.type
naturally type of true
), whereas resolved dynamictype
of nil
value include both general optional , wrapped type information.
struct foo { let bar: bool = true } var foo: foo? = foo() /* .some<t> case (non-nil) */ foo?.bar // true <-- _expression_ resolves (results in) _value_ 'true' foo?.bar.dynamictype // bool.type <-- dynamic type of _result of expression_ true.dynamictype // bool.type <-- compare /* .none case (nil) */ foo = nil foo?.bar.dynamictype // nil <-- _expression_ resolves _value_ 'optional<foo.type>.none' optional<foo.type>.none.dynamictype // optional<foo.type>.type <-- compare
now, if assign values variable, naturally variable must have concrete type. since value assign at runtime can either .none
or .some<t>
, type of variable must 1 can hold values of both these cases, hence, optional<t.type>
(disregarding of whether variable holds nil
or non-nil
value). case you've shown in second example: dynamictype
of variable (here, immutable, using variable differ value) isok
type can hold both .none
, .some<t>
, no matter actual value of variable is, , hence dynamictype
resolves type; optional<bool.type>.type
.
wrapping expressions in parantheses escapes runtime introspection of swift playground?
interestingly, if expression wrapped in parantheses prior applying .dynamictype
, playground sidebar resolves .dynamictype
of wrapped expression type of expression, if actual value unknown. e.g., (...)
in (...).dynamictype
treated variable concrete type rather runtime-resolved value.
/* .some case (non-nil) */ foo?.bar // true (foo?.bar).dynamictype /* optional<bool>.type <-- if (...) _variable_ of unknown value */ /* .none case (nil) */ foo = nil (foo?.bar).dynamictype /* optional<bool>.type <-- if (...) _variable_ of unknown value */
we can further note lone expression wrapped in parantheses in playground not resolve @ (in sidebar). it's if escape sidebar:s runtime introspection if wrapping expressions in parantheses (which explain why dynamictype
of expressions wrapped in parantheses resolve if playground cannot make use runtime information of these expressions)
var = 4 // shows '4' (a = 2) // shows nothing; can't expand or details in sidebar
tbh, cannot explain why is, , categorize peculiarity of swift playground.
Comments
Post a Comment