mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
v: add a $Option comp-time type, to enable: $for f in Test.fields { $if f.typ is $Option { } }
(#17546)
This commit is contained in:
parent
09c9cbcef9
commit
ee4150f213
@ -128,6 +128,7 @@ pub enum ComptimeTypeKind {
|
|||||||
enum_
|
enum_
|
||||||
alias
|
alias
|
||||||
function
|
function
|
||||||
|
option
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct ComptimeType {
|
pub struct ComptimeType {
|
||||||
@ -148,6 +149,7 @@ pub fn (cty ComptimeType) str() string {
|
|||||||
.enum_ { '\$Enum' }
|
.enum_ { '\$Enum' }
|
||||||
.alias { '\$Alias' }
|
.alias { '\$Alias' }
|
||||||
.function { '\$Function' }
|
.function { '\$Function' }
|
||||||
|
.option { '\$Option' }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2279,6 +2279,9 @@ pub fn (t &Table) is_comptime_type(x Type, y ComptimeType) bool {
|
|||||||
.function {
|
.function {
|
||||||
return x_kind == .function
|
return x_kind == .function
|
||||||
}
|
}
|
||||||
|
.option {
|
||||||
|
return x.has_flag(.option)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -741,6 +741,7 @@ pub fn (mut f Fmt) expr(node_ ast.Expr) {
|
|||||||
.enum_ { f.write('\$Enum') }
|
.enum_ { f.write('\$Enum') }
|
||||||
.alias { f.write('\$Alias') }
|
.alias { f.write('\$Alias') }
|
||||||
.function { f.write('\$Function') }
|
.function { f.write('\$Function') }
|
||||||
|
.option { f.write('\$Option') }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -661,6 +661,7 @@ pub fn (mut f Gen) expr(node_ ast.Expr) {
|
|||||||
.enum_ { f.write('\$Enum') }
|
.enum_ { f.write('\$Enum') }
|
||||||
.alias { f.write('\$Alias') }
|
.alias { f.write('\$Alias') }
|
||||||
.function { f.write('\$Function') }
|
.function { f.write('\$Function') }
|
||||||
|
.option { f.write('\$Option') }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -12,7 +12,7 @@ const (
|
|||||||
supported_comptime_calls = ['html', 'tmpl', 'env', 'embed_file', 'pkgconfig', 'compile_error',
|
supported_comptime_calls = ['html', 'tmpl', 'env', 'embed_file', 'pkgconfig', 'compile_error',
|
||||||
'compile_warn']
|
'compile_warn']
|
||||||
comptime_types = ['Map', 'Array', 'Int', 'Float', 'Struct', 'Interface', 'Enum',
|
comptime_types = ['Map', 'Array', 'Int', 'Float', 'Struct', 'Interface', 'Enum',
|
||||||
'Sumtype', 'Alias', 'Function']
|
'Sumtype', 'Alias', 'Function', 'Option']
|
||||||
)
|
)
|
||||||
|
|
||||||
pub fn (mut p Parser) parse_comptime_type() ast.ComptimeType {
|
pub fn (mut p Parser) parse_comptime_type() ast.ComptimeType {
|
||||||
@ -55,6 +55,9 @@ pub fn (mut p Parser) parse_comptime_type() ast.ComptimeType {
|
|||||||
'Sumtype' {
|
'Sumtype' {
|
||||||
cty = .sum_type
|
cty = .sum_type
|
||||||
}
|
}
|
||||||
|
'Option' {
|
||||||
|
cty = .option
|
||||||
|
}
|
||||||
else {}
|
else {}
|
||||||
}
|
}
|
||||||
node = ast.ComptimeType{cty, node.pos}
|
node = ast.ComptimeType{cty, node.pos}
|
||||||
|
18
vlib/v/tests/comptime_option_field_test.v
Normal file
18
vlib/v/tests/comptime_option_field_test.v
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
struct Test {
|
||||||
|
a ?int
|
||||||
|
b ?f64
|
||||||
|
c ?string
|
||||||
|
d int
|
||||||
|
e f64
|
||||||
|
f string
|
||||||
|
}
|
||||||
|
|
||||||
|
fn test_option_comptime() {
|
||||||
|
mut opts := []string{}
|
||||||
|
$for f in Test.fields {
|
||||||
|
$if f.typ is $Option {
|
||||||
|
opts << f.name
|
||||||
|
}
|
||||||
|
}
|
||||||
|
assert opts == ['a', 'b', 'c']
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user