From 76763f51aa119e71b00200142292acf5f1fdc69c Mon Sep 17 00:00:00 2001 From: Andreas Rumpf Date: Fri, 21 Oct 2022 06:55:52 +0200 Subject: implemented strictCaseObjects (#20608) * implemented strictCaseObjects * changelog update --- doc/manual_experimental.md | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) (limited to 'doc') diff --git a/doc/manual_experimental.md b/doc/manual_experimental.md index 59d6a407b..748637478 100644 --- a/doc/manual_experimental.md +++ b/doc/manual_experimental.md @@ -1941,3 +1941,39 @@ constructors that take inheritance into account. **Note**: The implementation of "strict definitions" and "out parameters" is experimental but the concept is solid and it is expected that eventually this mode becomes the default in later versions. + + +Strict case objects +=================== + +With `experimental: "strictCaseObjects"` *every* field access is checked to be valid at compile-time. +The field is within a `case` section of an `object`. + + ```nim + {.experimental: "strictCaseObjects".} + + type + Foo = object + case b: bool + of false: + s: string + of true: + x: int + + var x = Foo(b: true, x: 4) + case x.b + of true: + echo x.x # valid + of false: + echo "no" + + case x.b + of false: + echo x.x # error: field access outside of valid case branch: x.x + of true: + echo "no" + + ``` + +**Note**: The implementation of "strict case objects" is experimental but the concept +is solid and it is expected that eventually this mode becomes the default in later versions. -- cgit 1.4.1-2-gfad0