fn test_comptime_if_test() {
mut i := 0
$if test { //如果在测试函数中,则执行
i++
}
$if !test { //非测试函数中
i--
}
assert i == 1
}
判断平台是32位还是64位
fn main() {
mut x := 0
$if x32 {
println('system is 32 bit')
x = 1
}
$if x64 {
println('system is 64 bit')
x = 2
}
}
判断平台使用的字节序是小字节序,还是大字节序
fn main() {
mut x := 0
$if little_endian { //小字节序
println('system is little endian')
x = 1
}
$if big_endian { //大字节序
println('system is big endian')
x = 2
}
}
// FieldData holds information about a field. Fields reside on structs.
pub struct FieldData {
pub:
name string // the name of the field f
typ int // the internal TypeID of the field f,
unaliased_typ int // if f's type was an alias of int, this will be TypeID(int)
//
attrs []string // the attributes of the field f
is_pub bool // f is in a `pub:` section
is_mut bool // f is in a `mut:` section
//
is_shared bool // `f shared Abc`
is_atomic bool // `f atomic int` , TODO
is_option bool // `f ?string` , TODO
//
is_array bool // `f []string` , TODO
is_map bool // `f map[string]int` , TODO
is_chan bool // `f chan int` , TODO
is_enum bool // `f Enum` where Enum is an enum
is_struct bool // `f Abc` where Abc is a struct , TODO
is_alias bool // `f MyInt` where `type MyInt = int`, TODO
//
indirections u8 // 0 for `f int`, 1 for `f &int`, 2 for `f &&int` , TODO
}
pub struct FieldData {
pub:
name string // the name of the field f
typ int // the internal TypeID of the field f,
unaliased_typ int // if f's type was an alias of int, this will be TypeID(int)
//
attrs []string // the attributes of the field f
is_pub bool // f is in a `pub:` section
is_mut bool // f is in a `mut:` section
//
is_shared bool // `f shared Abc`
is_atomic bool // `f atomic int` , TODO
is_optional bool // `f ?string` , TODO
//
is_array bool // `f []string` , TODO
is_map bool // `f map[string]int` , TODO
is_chan bool // `f chan int` , TODO
is_struct bool // `f Abc` where Abc is a struct , TODO
is_alias bool // `f MyInt` where `type MyInt = int`, TODO
//
indirections u8 // 0 for `f int`, 1 for `f &int`, 2 for `f &&int` , TODO
}
获取结构体选项类型字段值
struct FixedStruct {
a int
b string
c ?int
d ?string
}
struct Encoder {}
fn main() {
fixed := FixedStruct{123, '456', 789, '321'}
println(fixed.a.str())
println(fixed.c?.str()) //返回选项类型字段的值
println(fixed.b.int())
println(fixed.d?.int()) //返回选项类型字段的值
e := Encoder{}
e.encode_struct(fixed)
}
fn (e &Encoder) encode_struct[T](val T) {
$for field in T.fields {
mut value := val.$(field.name) //动态获取结构体字段的值
$if field.is_option {
println('>> ${value ?.str()}') //动态输出结构体选项字段的值
println(val.$(field.name) ?.str()) //动态输出结构体选项字段的值
}
}
}
module main
fn kind[T]() {
$if T is $int {
println('int')
}
$if T is $float {
println('float')
}
$if T is $array {
println('array')
}
$if T is $map {
println('map')
}
$if T is $struct {
println('struct')
}
$if T is $interface {
println('interface')
}
$if T is $enum {
println('enum')
}
$if T is $sumtype {
println('sumtype')
}
$if T is $function {
println('function')
}
$if T is $alias {
println('alias')
}
$if T is $option {
println('option')
}
}
fn kind_detail[T]() {
$if T is u8 {
println('u8')
}
$if T is int {
println('int')
}
$if T is ?int {
println('?int')
}
// $if T is !int {
// println('!int')
// }
$if T in [u8, int] {
println('u8 or int')
}
$if T !in [u8, int] {
println('not u8 or int')
}
$if T in [int, $int] {
println('int or Int')
}
$if T in [$sumtype, $map] {
println('Sumtype or Map')
}
$if T in [Abc, Def] {
println('Abc or Def')
}
}
struct Abc {}
interface Def {}
enum Xyz {
x
y
z
}
type Sumtype = Abc | f32 | int
struct GenericStruct[T] {
x T
}
pub fn (s GenericStruct[T]) m() {
$if T is $int {
println('Int in method')
}
$if T is $array {
println('Array in method')
} $else {
println('other')
}
}
fn main() {
// int
kind[i8]()
kind[i16]()
kind[int]()
kind[i64]()
// int
kind[u8]()
kind[u16]()
kind[u32]()
kind[u64]()
// float
kind[f32]()
kind[f64]()
// array
kind[[]int]()
// map
kind[map[string]string]()
// struct
kind[Abc]()
// interface
kind[Def]()
// enum
kind[Xyz]()
// sumtype
kind[Sumtype]()
// generic struct
s1 := GenericStruct[int]{}
s1.m()
s2 := GenericStruct[[]string]{}
s2.m()
s3 := GenericStruct[f32]{}
s3.m()
kind_detail[Abc]()
}