From 98a48ecfb9a5fee4695f8d70d32942bf577abc68 Mon Sep 17 00:00:00 2001 From: joe-conigliaro Date: Fri, 19 Jun 2020 01:37:41 +1000 Subject: [PATCH] docs: add sum type cast & match information --- doc/docs.md | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/doc/docs.md b/doc/docs.md index e676caeda0..c9a0eebd3e 100644 --- a/doc/docs.md +++ b/doc/docs.md @@ -1177,6 +1177,40 @@ To check whether a sum type is a certain type, use `is`: println(expr is IfExpr) ``` +To cast a sum type to one of it's variants you use `as`: + +```v +bin_expr := expr as BinaryExpr +``` + +You can also use match to determine the variant & and cast to it at the same time. +There are 3 ways to access the cast variant inside a match branch: +- the `it` variable +- the shadowed match variable +- using `as` to specify a variable name + +```v + fn binary_expr(bx BinaryExpr) {...} + fn unary_expr(ux UnaryExpr) {...} + fn if_expr(ix IfExpr) {...} + + // using `it` + match expr { + BinaryExpr { binary_expr(it) } + ... + } + // using the shadowed variable, in this case `expr` + match expr { + UnaryExpr { unary_expr(expr) } + ... + } + // using `as` so specify a variable + match expr as actual_expr { + IfExpr { if_expr(actual_expr) } + ... + } +``` + ## Option/Result types and error handling ```v