verona-coding
Patterns, pitfalls, and idioms for writing Verona source code (.v files). Use when writing library code, _builtin types, or user programs.
- License 许可
- MIT
- 所属仓库
- microsoft/verona
- 最近更新
- 2026-11-13
- Stars
- ★ 3.7k
- Forks
- 173
01 前言 Front Matter
- name
- verona-coding
- description
- Patterns, pitfalls, and idioms for writing Verona source code (.v files). Use when writing library code, _builtin types, or user programs.
03 SKILL.md 正文
Verona Coding Skill
Patterns, pitfalls, and idioms for writing Verona source code. Use this skill
when writing .v files — library code, _builtin types, or user programs.
Literal Types and Inference
- Avoid explicit literal constructors (e.g.,
u64 0,i32 3). Let type inference determine the type from context. If inference fails, that's a compiler bug to fix — report it rather than working around it. - Known inference gap: Union return types (e.g.,
usize | none) don't refine bare literals.return 0in a function returningusize | nonegivesu64, notusize. This is a compiler bug to be fixed. compare()returnsi64: comparing with bare0works — inference resolves from thei64return type ofcompare().
Operators
- No operator precedence: All infix operators evaluate left-to-right.
Operators, methods, and functions are all the same thing — you can use
non-symbolic names in the infix position too (e.g.,
a min b). - Consequence:
cap < n + 1evaluates as(cap < n) + 1. Use parens to group:cap < (n + 1). - Juxtaposition binds tighter than infix:
a(i)binds before any infix operator, sosum + a(i)works as expected. - Bool or is
|, bool and is&. Since there's no precedence, parenthesize each comparison:(c == 32) | (c == 9) | (c == 10). - Free functions need qualification:
string::is_space(c), notis_space(c). Unqualified names resolve as method calls on the first arg.
Field Access vs Method Call
- Dot consumes arguments:
self.data(i)calls methoddatawith argi, NOT field access + apply. - Zero-arg getters don't need
(): Writeself.datanotself.data(),self.sizenotself.size(). Parens are unnecessary for zero-extra-arg methods (onlyself). - Field-then-apply:
self.data()(i)— the first()is needed to disambiguate fromself.data(i)(which calls methoddatawith argi). But when chaining further:self.data.pairs ...— no parens needed ondatabecausepairsis consumed by dot, not by juxtaposition. - For local variables:
result(i)works directly (no field access ambiguity). refkeyword on functions:ref apply(...)returns aref[T], enabling both read and write through the result. Useref self.data()(index)to delegate ref access.
Lambda Syntax and Higher-Order Functions
- Lambda syntax:
(params) -> { body }. Examples:(x: i32) -> { x + 1 }(i, c) -> { ... }(types inferred from context){ body }(zero-arg lambda)
- Passing lambdas to methods: Use juxtaposition, NOT wrapping parens.
The lambda IS the argument:
NOTself.data.pairs (i, c) -> { ... }self.data.pairs((i, c) -> { ... })— the extra parens are unnecessary. - No trailing semicolon when a lambda call is a statement by itself
(the
}ends the expression). raisein a lambda is a non-local return — it exits the ENCLOSING function, not the lambda. The lambda's own return type is unaffected byraise.
String Literals and Immutability
- Verona has deep immutability via
freeze— any object can be mutable or permanently immutable. This is NOT like C++const. - String literals should be frozen, not copied. The compiler wraps ConstStr
in
string::create()and then freezes the result. The frozen string directly references the constant pool array — no copy. create()should NOT copy the input array. It just wraps it:create(data: array[u8]): string { new { data, len = data.size - 1 } }- Mutation on a frozen string is caught naturally — you can't mutate an immutable object. If you want a mutable string, explicitly create one.
- The copy-on-create approach is C++ thinking. Verona's model: keep the original, make it immutable, copy only when mutability is needed.
Character Literals
- Space literal
' 'causes parser issues in nested control flow. Use the numeric value32instead. Other char literals like'a'work fine. - Common ASCII values: space=32, tab=9, newline=10, carriage return=13.
Union Return Types
usize | noneis a union type. Callers consume it with match/else:match s.find("x") { (i: usize) -> i; } else { 99 }- Returning a loop variable from a union-return function can cause the
variable to become
dyn. Copy to a typedletfirst (see Literal Types section above).
Style
- Match expression syntax — format like control flow, not inline:
Notmatch expr { (pattern) -> body; } else { default }match expr { (pattern) -> body; } else { default }on one line. - Don't wrap expressions in unnecessary parens: only use parens when needed for grouping (e.g., operator precedence).
Performance
-
Use bulk operations over byte-by-byte loops:
array.copy_from()wrapsmemmove(handles overlapping regions). Use it for shifting data within the same array instead of while loops. -
Avoid double-moving data:
replaceshould shift the tail once to its final position and copy the replacement in, not callerasetheninsert(which shifts the tail twice). -
copy_fromfor self-overlapping copies:self.data.copy_from(dst, self.data, src, len)works correctly for overlapping regions — it'smemmove, notmemcpy. -
none— no parens needed. Writenonenotnone(). -
Don't use
()unnecessarily: if a type or value needs no arguments, omit the parens.none,true,false, notnone(),true(),false(). -
Empty string:
string(array[u8]::fill(1))— a 1-byte array containing just the null terminator, withlen=0.
Syntax Reminders
- No semicolons after closing braces of
if/while/for. - Semicolons after statements and field definitions.
new { field = val }— no class name afternew.Type(args)is sugar forType::create(args).array[u8]::fill(n)allocates an array of sizen(zero-filled).self.lencalls the getter (zero-arg method, no parens needed).f()calls a zero-arg callable value. Don't drop the parens on lambda/function values just because zero-arg methods omit them.- Prefer paren-less syntax for clear zero-arg and single-arg calls:
handle::signal,tty,self.init handler,pipe::open 0. self.len = xcalls the setter.final(self: T)receives a read-only self. Don't call mutating helpers likeself.closefrom a finalizer; inline read-only-safe cleanup instead.
Testing Patterns
- Use bitmask accumulation for multi-check tests:
var result = 0; if cond1_fails { result = result + 1 } if cond2_fails { result = result + 2 } // ...powers of 2... result // exit code 0 means all passed - Tests must be self-contained — no external dependencies, no
use "_builtin".
FFI / libuv Wrapper Lifetimes
- Tie keepalive to the real external lifetime: for libuv-backed wrappers,
use timer-style
_activate(active)plus handler-safe deferred release (_finish_handler) soffi::pin/ffi::unpinandffi::external.add/ffi::external.removetrack active handles or pending requests. - Don't rely on incidental references: a callback closure or unrelated signal watcher may accidentally keep a wrapper alive for a while, but that is not a sound lifetime strategy.
- Synchronous setup failures need their own path: if a wrapper can fail
before it becomes active (for example
pipe.open(fd)), don't route the error through an active-only dispatch helper — use a separate ungated failure callback path. - Capture sendable wrappers in FFI callbacks: when a callback must refer
back to an owned state machine, capture the outer sendable wrapper (or another
sendable handle), not a cown-owned mutable
_statedirectly.
04 同仓库更多 Skills
verona-type-inference
Verona compiler type inference model — bidirectional refinement, per-function inference, lambda/context propagation, algebraic and structural typing, generics, and debugging. Use when changing or debugging `vc/passes/infer.cc` or investigating type inference failures.
verona-conventions
Verona compiler coding conventions, C++ style, trieste patterns, and project structure. Applies when writing or reviewing Verona compiler code.
verona-coding
Patterns, pitfalls, and idioms for writing Verona source code (.v files). Use when writing library code, _builtin types, or user programs.
vbci-memory
Verona interpreter (vbci) memory management model — regions, reference counting, write barriers, dragging, cowns, and finalization. Use when working on or debugging vbci runtime memory management code.
testsuite
Verona compiler test suite infrastructure — running tests, updating golden files, verifying pass completeness, checking error codes. Use when debugging test failures, regenerating golden files, or understanding the test framework.
review
Run a principle-based code review on the current branch. Use when the user asks for a review, or at mandatory review checkpoints (after planning, after implementation before PR).