1
0
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:
Felipe Pena 2023-03-10 07:17:25 -03:00 committed by GitHub
parent 09c9cbcef9
commit ee4150f213
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 29 additions and 1 deletions

View File

@ -128,6 +128,7 @@ pub enum ComptimeTypeKind {
enum_
alias
function
option
}
pub struct ComptimeType {
@ -148,6 +149,7 @@ pub fn (cty ComptimeType) str() string {
.enum_ { '\$Enum' }
.alias { '\$Alias' }
.function { '\$Function' }
.option { '\$Option' }
}
}

View File

@ -2279,6 +2279,9 @@ pub fn (t &Table) is_comptime_type(x Type, y ComptimeType) bool {
.function {
return x_kind == .function
}
.option {
return x.has_flag(.option)
}
}
}

View File

@ -741,6 +741,7 @@ pub fn (mut f Fmt) expr(node_ ast.Expr) {
.enum_ { f.write('\$Enum') }
.alias { f.write('\$Alias') }
.function { f.write('\$Function') }
.option { f.write('\$Option') }
}
}
}

View File

@ -661,6 +661,7 @@ pub fn (mut f Gen) expr(node_ ast.Expr) {
.enum_ { f.write('\$Enum') }
.alias { f.write('\$Alias') }
.function { f.write('\$Function') }
.option { f.write('\$Option') }
}
}
}

View File

@ -12,7 +12,7 @@ const (
supported_comptime_calls = ['html', 'tmpl', 'env', 'embed_file', 'pkgconfig', 'compile_error',
'compile_warn']
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 {
@ -55,6 +55,9 @@ pub fn (mut p Parser) parse_comptime_type() ast.ComptimeType {
'Sumtype' {
cty = .sum_type
}
'Option' {
cty = .option
}
else {}
}
node = ast.ComptimeType{cty, node.pos}

View 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']
}