diff options
author | Juan M Gómez <info@jmgomez.me> | 2023-10-08 22:51:44 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-10-08 23:51:44 +0200 |
commit | 8ac466980f7658c37b05c6ec099537ea0e459cb9 (patch) | |
tree | 9d1aaa8382b3f635bec29dc668ec43b4cae13972 /doc | |
parent | c3774c8821cc25187252491b4514235b9a8f1aac (diff) | |
download | Nim-8ac466980f7658c37b05c6ec099537ea0e459cb9.tar.gz |
marking a field with noInit allows to skip constructor initialiser (#22802)
Co-authored-by: Andreas Rumpf <rumpf_a@web.de>
Diffstat (limited to 'doc')
-rw-r--r-- | doc/manual_experimental.md | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/doc/manual_experimental.md b/doc/manual_experimental.md index 41d463ff8..249f9367b 100644 --- a/doc/manual_experimental.md +++ b/doc/manual_experimental.md @@ -2408,6 +2408,56 @@ proc makeCppStruct(a: cint = 5, b:cstring = "hello"): CppStruct {.importcpp: "Cp # If one removes a default value from the constructor and passes it to the call explicitly, the C++ compiler will complain. ``` +Skip initializers in fields members +=================================== + +By using `noInit` in a type or field declaration, the compiler will skip the initializer. By doing so one can explicitly initialize those values in the constructor of the type owner. + +For example: + +```nim + +{.emit: """/*TYPESECTION*/ + struct Foo { + Foo(int a){}; + }; + struct Boo { + Boo(int a){}; + }; + + """.} + +type + Foo {.importcpp.} = object + Boo {.importcpp, noInit.} = object + Test {.exportc.} = object + foo {.noInit.}: Foo + boo: Boo + +proc makeTest(): Test {.constructor: "Test() : foo(10), boo(1)".} = + discard + +proc main() = + var t = makeTest() + +main() + +``` + +Will produce: + +```c++ + +struct Test { + Foo foo; + Boo boo; + N_LIB_PRIVATE N_NOCONV(, Test)(void); +}; + +``` + +Notice that without `noInit` it would produce `Foo foo {}` and `Boo boo {}` + Member pragma ============= |