From 3c85aa9e53cbaae5fae83f9fd29de3bdb107c7aa Mon Sep 17 00:00:00 2001 From: Ivan Bobev Date: Fri, 6 Nov 2020 20:56:09 +0200 Subject: Make `{.requiresInit.}` to work for distinct types (#15869) Make `requiresInit` pragma to work for distinct types in addition to objects. Tagging of distinct types with `requiresInit` pragma was already supported, but its impact wasn't applied. Now its behavior when applied on distinct types is as follows. Given the following distinct type definitions: ```nim type DistinctObject {.requiresInit, borrow: `.`.} = distinct MyObject DistinctString {.requiresInit.} = distinct string ``` The following code blocks will fail to compile: ```nim var foo: DistinctFoo foo.x = "test" doAssert foo.x == "test" ``` ```nim var s: DistinctString s = "test" doAssert s == "test" ``` But these ones will compile successfully: ```nim let foo = DistinctFoo(Foo(x: "test")) doAssert foo.x == "test" ``` ```nim let s = "test" doAssert s == "test" ``` --- doc/manual.rst | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) (limited to 'doc/manual.rst') diff --git a/doc/manual.rst b/doc/manual.rst index d9f9cc9b9..4e5b861b1 100644 --- a/doc/manual.rst +++ b/doc/manual.rst @@ -2697,6 +2697,36 @@ the variable has been initialized and does not rely on syntactic properties: x = a() # use x +`requiresInit` pragma can also be applyied to `distinct` types. + +Given the following distinct type definitions: + +.. code-block:: nim + type + DistinctObject {.requiresInit, borrow: `.`.} = distinct MyObject + DistinctString {.requiresInit.} = distinct string + +The following code blocks will fail to compile: + +.. code-block:: nim + var foo: DistinctFoo + foo.x = "test" + doAssert foo.x == "test" + +.. code-block:: nim + var s: DistinctString + s = "test" + doAssert s == "test" + +But these ones will compile successfully: + +.. code-block:: nim + let foo = DistinctFoo(Foo(x: "test")) + doAssert foo.x == "test" + +.. code-block:: nim + let s = "test" + doAssert s == "test" Let statement ------------- -- cgit 1.4.1-2-gfad0