Skip to content

Commit d364167

Browse files
authored
doc: clarify sum type smart cast vs static type (#27371)
1 parent 9e3e02d commit d364167

1 file changed

Lines changed: 7 additions & 0 deletions

File tree

doc/docs.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1913,6 +1913,13 @@ println(u_name) // John
19131913

19141914
You can check the current type of a sum type using `is` and its negated form `!is`.
19151915

1916+
Note that the variable's declared type stays the sum type itself. In the example
1917+
below, `x` has the static type `Alphabet`, even though the value it holds is an
1918+
`Abc`. Inside an `is` check (or a `match` branch) the compiler automatically
1919+
*smart casts* `x` to the matched variant, so within that block `x` has the static
1920+
type `Abc` and you can access its fields directly and safely, without writing an
1921+
explicit `as` cast.
1922+
19161923
You can do it either in an `if`:
19171924

19181925
```v cgen

0 commit comments

Comments
 (0)