vlang
  • 目录
    • V语言学习笔记
    • 目录
    • 安装
    • 开发工具
    • 快速总览
    • 模块
    • 基本类型
    • 变量
    • 常量
    • 枚举
    • 数组
    • 字典
    • 流程控制
    • 函数
    • 结构体
    • 访问控制
    • 方法
    • 注解
    • 接口
    • 泛型
    • 类型别名
    • 联合类型
    • 联合体
    • 错误处理
    • 运算符重载
    • 内置json支持
    • 内置sql支持
    • 并发
    • 内存管理
    • 代码测试
    • 文档生成
    • 编译时代码
    • 包管理器
    • 单个V文件
    • V shell script
    • 不安全代码
    • 集成C代码库
    • 集成汇编代码
    • 裸机环境
    • 生成wasm
    • GUI开发
    • web开发
    • 数据库开发
  • builtin
  • strings
  • arrays
  • maps
  • datatypes
  • strconv
  • os
  • runtime
  • time
  • math
  • json
  • encoding
  • compress
  • toml
  • flag
  • term
  • log
  • io
  • readline
  • reflection
  • net
  • net.http
  • eventbus
  • regex
  • crypto
  • rand
  • sync
  • x
  • db.pg
  • db.mysql
  • db.mssql
  • db.sqlite
  • orm
  • ui
  • sokol
  • gg
  • gx
  • fontstash
  • stbi
  • clipboard
  • V抽象语法树
  • V语言服务
  • V编译器源代码
  • 生成C代码
  • 生成js代码
  • 生成go代码
  • 生成native代码
  • 解释器直接运行
  • 附录1 关键字
  • 附录2 运算符
  • 附录3 编码风格
  • 附录4 V编译器命令行使用
  • 附录5 V调试及错误定位
  • 附录6 V和Go基本语法参照
  • 附录7 V和Zig基本语法参照
由 GitBook 提供支持
在本页
  • 编译器内置注解
  • 模块注解
  • 枚举注解
  • 结构体注解
  • 结构体字段注解
  • 函数注解
  • 变量注解
  • 自定义注解

这有帮助吗?

  1. 目录

注解

上一页方法下一页接口

最后更新于1年前

这有帮助吗?

V语言可以对模块,枚举,结构体,结构体字段,函数/方法,变量增加注解。

注解的格式统一是:@[注解名] 或 [注解名:注解扩展内容]。

编译器内置注解

模块注解

  • @[deprecated]

    参考:

  • @[deprecated_after]

    参考:

枚举注解

  • @[flag]注解

    参考:

结构体注解

结构体注解统一放在结构体定义前。

  • @[typedef]

    参考:

  • @[heap]

    参考:

  • @[noinit]

    参考:

  • @[params]

    参考:

  • @[table]

    参考:

  • @[packed]

    参考:

  • @[autostr: allowrecurse]

    参考:

结构体字段注解

结构体字段注解统一放在字段定义后。

  • @[required]

  • @[deprecated]

  • @[json]

  • @[toml]

  • @[skip]

  • @[row]

函数注解

函数注解统一放在函数定义之前。

  • @[deprecated]

  • @[inline]

  • @[live]

  • @[unsafe]

  • @[trusted]

  • @[export]

  • @[weak]

  • @[if xxx平台]

  • @[if debug]

    这个注解表示该函数只有在编译时加上了-d 调试模式的时候,才会被编译生成,并且可以调用。加上@[if debug]注解后函数不可以有返回值。

    @[if debug]
    fn foo() {
    }
  • @[windows_stdcall]

    这个注解只能用于Win32 API,如果需要传递回调函数的时候使用。

    @[windows_stdcall]
    fn C.DefWindowProc(hwnd int, msg int, lparam int, wparam int)
  • @[console]

    这个注解只能用在main函数前,导入了图形库模块(比如gg,ui)后,命令行窗口就不再显示了,查看不到命令行输出,加上这个注解,命令行窗口就会出现。

    @[console]
    fn main() {}
  • @[noinline]

  • @[direct_array_access]

    加了这个注解的函数,在生成C代码时会直接使用C语言中的数组操作,省略边界检查,这样在遍历数组元素时速度会提高不少,代价是不会进行边界检查,函数是不安全的,边界检查由用户代码自己判断。

    module main
    
    fn main() {
    	direct_modification1()
    	direct_modification2()
    }
    
    fn direct_modification1() { // 不带direct_array_access
    	mut foo := [2, 0, 5]
    	foo[1] = 3
    	foo[0] *= 7
    	foo[1]--
    	foo[2] -= 2
    }
    
    @[direct_array_access] 
    fn direct_modification2() { // 带direct_array_access
    	mut foo := [2, 0, 5]
    	foo[1] = 3
    	foo[0] *= 7
    	foo[1]--
    	foo[2] -= 2
    }

    生成的C代码:

    VV_LOCAL_SYMBOL void main__main(void) {
    	main__direct_modification1();
    	main__direct_modification2();
    }
    
    VV_LOCAL_SYMBOL void main__direct_modification1(void) {
    	Array_int foo = new_array_from_c_array(3, 3, sizeof(int), _MOV((int[3]){2, 0, 5}));
    	array_set(&foo, 1, &(int[]) { 3 });
    	(*(int*)array_get(foo, 0)) *= 7;
    	(*(int*)/*ee elem_typ */array_get(foo, 1))--;
    	(*(int*)array_get(foo, 2)) -= 2;
    }
    
    // Attr: [direct_array_access]
    VV_LOCAL_SYMBOL void main__direct_modification2(void) {
    	Array_int foo = new_array_from_c_array(3, 3, sizeof(int), _MOV((int[3]){2, 0, 5}));
    	((int*)foo.data)[1] = 3;
    	((int*)foo.data)[0] *= 7;
    	((int*)foo.data)[1]--;
    	((int*)foo.data)[2] -= 2;
    }
  • @[manualfree]

  • @[noreturn]

    表示这个函数不会有返回值给函数的调用方,一般来说是函数内有存在exit,panic,无限循环for {},或调用了另一个没有返回值的函数。标记了noreturn的函数,调用方就不会等待该函数返回。

    @[noreturn]
    fn forever() {
    	for {}
    }
  • @[keep_args_alive]

    函数加上这个注解后,函数的指针参数在函数返回之前不会被GC释放。

    @[keep_args_alive]
    fn C.calc_expr_after_delay(voidptr, int, voidptr) int

变量注解

  • @[c_extern]

    使用@ [c_extern]注解,在生成的C代码中,给全局变量增加extern关键字。

自定义注解

实际上除了编译器内置的注解外,结构体,结构体字段,函数,枚举,联合类型的定义,都可以增加各种自定义注解,然后自己解析,自己使用。

@[testing]
struct StructAttrTest {
	foo string [attr1;required] //结构体字段自定义注解
	bar int [attr1; attr2; required]
}

@[testing]
struct PubStructAttrTest {
	foo string
	bar int
}

@[testing]
type Name = string

@[testing]
type SumType = int | string | bool

@[testing]
enum EnumAttrTest {
	one
	two
}

@[testing]
enum PubEnumAttrTest {
	one
	two
}

@[attrone] 
@[attrtwo] //可以同时有多个注解
enum EnumMultiAttrTest {
	one
	two
}

@[attr1;attr2] //在同一个注解中括号内,注解用分号来区隔
fn fn_attribute() {
}

@[testing]
fn pub_fn_attribute() {
}

@[attrone]
@[attrtwo]
fn fn_multi_attribute() {
}

@[attrone]
@[attrtwo]
fn fn_multi_attribute_single() {
}

参考:

参考:

参考:

参考:

参考:

参考:

参考:

参考:

参考:

参考:

参考:

参考:

参考:

参考:

参考。

具体内容可以参考:

注解的扩展性还是比较灵活的,目前结构体注解和结构体字段注解,已经可以通过$for编译时反射来获取所有的注解内容,具体内容可以参考:。

模块章节
模块章节
枚举章节
结构体章节
结构体章节
结构体章节
结构体章节
结构体章节
结构体章节
结构体章节
结构体章节-字段注解
结构体章节-字段注解
json章节
toml章节
json章节
json章节
函数章节
函数章节
函数章节
unsafe章节
unsafe章节
函数章节
函数章节
函数章节
内存管理章节-手动内存管理
变量章节
编译时反射章节