Skip to content

Commit

Permalink
docs: document the [noinit] attribute with an example (#15876)
Browse files Browse the repository at this point in the history
  • Loading branch information
hungrybluedev committed Sep 25, 2022
1 parent 3674baa commit 7f23abb
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 4 deletions.
12 changes: 11 additions & 1 deletion cmd/tools/vcheck-md.v
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,16 @@ fn (mut f MDFile) check_examples() CheckResult {
}
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' {
res := silent_cmdexecute('${os.quoted_path(vexe)} -w -Wfatal-errors -check-syntax ${os.quoted_path(vfile)}')
if res == 0 {
Expand All @@ -554,7 +564,7 @@ fn (mut f MDFile) check_examples() CheckResult {
}
'nofmt' {}
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
errors++
}
Expand Down
5 changes: 3 additions & 2 deletions cmd/v/help/check-md.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ These are:
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
failcompile - Known failing compilation. Useful for examples demonstrating compiler errors.
oksyntax - Should parse and be formatted but may not compile. Useful for partial examples.
badsyntax - Known bad syntax, it should not even parse.
okfmt - Should only be formatted, but it can refer to invalid modules, missing functions etc. Useful for partial examples.
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.
nofmt - Disable fmt verification for individual code blocks.
48 changes: 47 additions & 1 deletion doc/docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
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`
-->

Expand Down Expand Up @@ -2139,6 +2139,52 @@ assert book.author.name == 'Samantha Black'
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

```v
Expand Down

0 comments on commit 7f23abb

Please sign in to comment.