From 876b75a6aa25088f7c98422c8764beb53981c262 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=A9mie=20Astor?= Date: Mon, 18 Apr 2022 18:44:37 +0200 Subject: [PATCH] Improve union docs --- docs/Reference/Types/Unions.mdr | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/docs/Reference/Types/Unions.mdr b/docs/Reference/Types/Unions.mdr index 9c39ad55..e5447641 100644 --- a/docs/Reference/Types/Unions.mdr +++ b/docs/Reference/Types/Unions.mdr @@ -1,13 +1,24 @@ -# Union types +# Tagged Union -Union store their component in the same memory space. -For more infomations, see [here](https://en.wikipedia.org/wiki/Union_type). +Union store their component in the same memory space, +but only one element can be used at a time ```gwion,editable union U { - int a; + int i; float f; Object o; }; -``` +#! create an union with field `i` set to `1` +new U(i, 1) => var U u; + +<<< u.i >>>; + +#! set field f to 2.4 +2.4 => u.f; +<<< u.f >>>; + +#! this will trigger an invalid access error +<<< u.i >>>; +``` -- 2.43.0