Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cgen: fix comptime for in field selector (#12457)
- Loading branch information
Showing
2 changed files
with
48 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
fn print_field_values<T>(s T) { | ||
mut value_list := []string{} | ||
|
||
$for field in T.fields { | ||
println(s.$(field.name)) | ||
value_list << s.$(field.name).str() | ||
} | ||
assert value_list.len == 4 | ||
assert value_list[0] == 'Simon' | ||
assert value_list[1] == 'simon1234' | ||
assert value_list[2] == 'simon@gmail.com' | ||
assert value_list[3] == '15' | ||
} | ||
|
||
struct Foo { | ||
name string | ||
password string | ||
email string | ||
age int | ||
} | ||
|
||
fn test_comptime_for_in_field_selector() { | ||
bar := Foo{ | ||
name: 'Simon' | ||
password: 'simon1234' | ||
email: 'simon@gmail.com' | ||
age: 15 | ||
} | ||
print_field_values<Foo>(bar) | ||
} |