Skip to content

Commit ee4150f

Browse files
authored
v: add a $Option comp-time type, to enable: $for f in Test.fields { $if f.typ is $Option { } } (#17546)
1 parent 09c9cbc commit ee4150f

File tree

6 files changed

+29
-1
lines changed

6 files changed

+29
-1
lines changed

vlib/v/ast/ast.v

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ pub enum ComptimeTypeKind {
128128
enum_
129129
alias
130130
function
131+
option
131132
}
132133

133134
pub struct ComptimeType {
@@ -148,6 +149,7 @@ pub fn (cty ComptimeType) str() string {
148149
.enum_ { '\$Enum' }
149150
.alias { '\$Alias' }
150151
.function { '\$Function' }
152+
.option { '\$Option' }
151153
}
152154
}
153155

vlib/v/ast/table.v

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2279,6 +2279,9 @@ pub fn (t &Table) is_comptime_type(x Type, y ComptimeType) bool {
22792279
.function {
22802280
return x_kind == .function
22812281
}
2282+
.option {
2283+
return x.has_flag(.option)
2284+
}
22822285
}
22832286
}
22842287

vlib/v/fmt/fmt.v

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -741,6 +741,7 @@ pub fn (mut f Fmt) expr(node_ ast.Expr) {
741741
.enum_ { f.write('\$Enum') }
742742
.alias { f.write('\$Alias') }
743743
.function { f.write('\$Function') }
744+
.option { f.write('\$Option') }
744745
}
745746
}
746747
}

vlib/v/gen/golang/golang.v

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -661,6 +661,7 @@ pub fn (mut f Gen) expr(node_ ast.Expr) {
661661
.enum_ { f.write('\$Enum') }
662662
.alias { f.write('\$Alias') }
663663
.function { f.write('\$Function') }
664+
.option { f.write('\$Option') }
664665
}
665666
}
666667
}

vlib/v/parser/comptime.v

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const (
1212
supported_comptime_calls = ['html', 'tmpl', 'env', 'embed_file', 'pkgconfig', 'compile_error',
1313
'compile_warn']
1414
comptime_types = ['Map', 'Array', 'Int', 'Float', 'Struct', 'Interface', 'Enum',
15-
'Sumtype', 'Alias', 'Function']
15+
'Sumtype', 'Alias', 'Function', 'Option']
1616
)
1717

1818
pub fn (mut p Parser) parse_comptime_type() ast.ComptimeType {
@@ -55,6 +55,9 @@ pub fn (mut p Parser) parse_comptime_type() ast.ComptimeType {
5555
'Sumtype' {
5656
cty = .sum_type
5757
}
58+
'Option' {
59+
cty = .option
60+
}
5861
else {}
5962
}
6063
node = ast.ComptimeType{cty, node.pos}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
struct Test {
2+
a ?int
3+
b ?f64
4+
c ?string
5+
d int
6+
e f64
7+
f string
8+
}
9+
10+
fn test_option_comptime() {
11+
mut opts := []string{}
12+
$for f in Test.fields {
13+
$if f.typ is $Option {
14+
opts << f.name
15+
}
16+
}
17+
assert opts == ['a', 'b', 'c']
18+
}

0 commit comments

Comments
 (0)