mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
docs: document the [noinit] attribute with an example (#15876)
This commit is contained in:
parent
3674baab23
commit
7f23abbf8c
@ -541,6 +541,16 @@ fn (mut f MDFile) check_examples() CheckResult {
|
|||||||
}
|
}
|
||||||
oks++
|
oks++
|
||||||
}
|
}
|
||||||
|
'okfmt' {
|
||||||
|
if fmt_res != 0 {
|
||||||
|
eprintln(eline(f.path, e.sline, 0, 'okfmt example is not formatted'))
|
||||||
|
eprintln(vcontent)
|
||||||
|
should_cleanup_vfile = false
|
||||||
|
errors++
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
oks++
|
||||||
|
}
|
||||||
'badsyntax' {
|
'badsyntax' {
|
||||||
res := silent_cmdexecute('${os.quoted_path(vexe)} -w -Wfatal-errors -check-syntax ${os.quoted_path(vfile)}')
|
res := silent_cmdexecute('${os.quoted_path(vexe)} -w -Wfatal-errors -check-syntax ${os.quoted_path(vfile)}')
|
||||||
if res == 0 {
|
if res == 0 {
|
||||||
@ -554,7 +564,7 @@ fn (mut f MDFile) check_examples() CheckResult {
|
|||||||
}
|
}
|
||||||
'nofmt' {}
|
'nofmt' {}
|
||||||
else {
|
else {
|
||||||
eprintln(eline(f.path, e.sline, 0, 'unrecognized command: "$command", use one of: wip/ignore/compile/cgen/failcompile/oksyntax/badsyntax/nofmt'))
|
eprintln(eline(f.path, e.sline, 0, 'unrecognized command: "$command", use one of: wip/ignore/compile/cgen/failcompile/okfmt/oksyntax/badsyntax/nofmt'))
|
||||||
should_cleanup_vfile = false
|
should_cleanup_vfile = false
|
||||||
errors++
|
errors++
|
||||||
}
|
}
|
||||||
|
@ -17,7 +17,8 @@ These are:
|
|||||||
live - Compile hot reload examples with the ´-live´ flag set and verify formatting.
|
live - Compile hot reload examples with the ´-live´ flag set and verify formatting.
|
||||||
ignore - Ignore the example, useful for examples that just use the syntax highlighting
|
ignore - Ignore the example, useful for examples that just use the syntax highlighting
|
||||||
failcompile - Known failing compilation. Useful for examples demonstrating compiler errors.
|
failcompile - Known failing compilation. Useful for examples demonstrating compiler errors.
|
||||||
oksyntax - Should parse and be formatted but may not compile. Useful for partial examples.
|
okfmt - Should only be formatted, but it can refer to invalid modules, missing functions etc. Useful for partial examples.
|
||||||
badsyntax - Known bad syntax, it should not even parse.
|
oksyntax - Should parse, and be formatted, but may not compile. Useful for partial examples.
|
||||||
|
badsyntax - Known bad syntax, it should NOT even parse.
|
||||||
wip - Like ignore; a planned feature; easy to search.
|
wip - Like ignore; a planned feature; easy to search.
|
||||||
nofmt - Disable fmt verification for individual code blocks.
|
nofmt - Disable fmt verification for individual code blocks.
|
||||||
|
48
doc/docs.md
48
doc/docs.md
@ -139,7 +139,7 @@ To do so, run the command `v up`.
|
|||||||
|
|
||||||
<!--
|
<!--
|
||||||
NB: there are several special keywords, which you can put after the code fences for v:
|
NB: there are several special keywords, which you can put after the code fences for v:
|
||||||
compile, cgen, live, ignore, failcompile, oksyntax, badsyntax, wip, nofmt
|
compile, cgen, live, ignore, failcompile, okfmt, oksyntax, badsyntax, wip, nofmt
|
||||||
For more details, do: `v check-md`
|
For more details, do: `v check-md`
|
||||||
-->
|
-->
|
||||||
|
|
||||||
@ -2139,6 +2139,52 @@ assert book.author.name == 'Samantha Black'
|
|||||||
assert book.author.age == 24
|
assert book.author.age == 24
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### `[noinit]` structs
|
||||||
|
|
||||||
|
V supports `[noinit]` structs, which are structs that cannot be initialised outside the module
|
||||||
|
they are defined in. They are either meant to be used internally or they can be used externally
|
||||||
|
through _factory functions_.
|
||||||
|
|
||||||
|
For an example, consider the following source in a directory `sample`:
|
||||||
|
|
||||||
|
```v oksyntax
|
||||||
|
module sample
|
||||||
|
|
||||||
|
[noinit]
|
||||||
|
pub struct Information {
|
||||||
|
pub:
|
||||||
|
data string
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn new_information(data string) !Information {
|
||||||
|
if data.len == 0 || data.len > 100 {
|
||||||
|
return error('data must be between 1 and 100 characters')
|
||||||
|
}
|
||||||
|
return Information{
|
||||||
|
data: data
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
Note that `new_information` is a _factory_ function. Now when we want to use this struct
|
||||||
|
outside the module:
|
||||||
|
|
||||||
|
```v okfmt
|
||||||
|
import sample
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
// This doesn't work when the [noinit] attribute is present:
|
||||||
|
// info := sample.Information{
|
||||||
|
// data: 'Sample information.'
|
||||||
|
// }
|
||||||
|
|
||||||
|
// Use this instead:
|
||||||
|
info := sample.new_information('Sample information.')!
|
||||||
|
|
||||||
|
println(info)
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
### Methods
|
### Methods
|
||||||
|
|
||||||
```v
|
```v
|
||||||
|
Loading…
Reference in New Issue
Block a user