-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
cgen: allow unwrapping of x as string
expr
#22953
Open
Delta456
wants to merge
3
commits into
vlang:master
Choose a base branch
from
Delta456:unwrap_option
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+60
−22
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
|
@@ -32,7 +32,7 @@ fn (mut g Gen) unwrap_generic(typ ast.Type) ast.Type { | |
} | ||
} | ||
} else if g.table.sym(typ).kind == .struct { | ||
// resolve selector `a.foo` where `a` is struct[T] on non generic function | ||
// resolve selector `a.foo` where `a` is struct[T] on non generic function | ||
sym := g.table.sym(typ) | ||
if sym.info is ast.Struct { | ||
if sym.info.generic_types.len > 0 { | ||
|
@@ -145,3 +145,27 @@ fn (mut g Gen) dot_or_ptr(val_type ast.Type) string { | |
'.' | ||
} | ||
} | ||
|
||
@[inline] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it is too long for |
||
fn (mut g Gen) unwrap_option_type(typ ast.Type, name string, is_auto_heap bool) { | ||
g.write('/*opt*/') | ||
styp := g.base_type(typ) | ||
if is_auto_heap { | ||
g.write('(*(${styp}*)${name}->data)') | ||
} else { | ||
type_sym := g.table.sym(typ) | ||
if type_sym.kind == .alias { | ||
// Alias to Option type | ||
parent_typ := (type_sym.info as ast.Alias).parent_type | ||
if parent_typ.has_flag(.option) { | ||
g.write('*((${g.base_type(parent_typ)}*)') | ||
} | ||
g.write('(*(${styp}*)${name}.data)') | ||
if parent_typ.has_flag(.option) { | ||
g.write('.data)') | ||
} | ||
} else { | ||
g.write('(*(${styp}*)${name}.data)') | ||
} | ||
} | ||
} |
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,23 @@ | ||
fn get_opt(a int) ?string { | ||
if a < 0 { | ||
return none | ||
} | ||
return 'success' | ||
} | ||
|
||
fn get_opt_int(a int) ?int { | ||
if a < 0 { | ||
return none | ||
} | ||
return 12 | ||
} | ||
|
||
fn test_option_unwrap_as_cast() { | ||
x := get_opt(1) | ||
d := get_opt_int(12) | ||
dump(d as int == 12) | ||
dump('${x as string}' == 'success') | ||
|
||
assert d as int == 12 | ||
assert x as string == 'success' | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What happens if it is not a IdentVar?