Skip to content
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: fix struct init with multi option fn type (fix #19478) #19491

Merged
merged 1 commit into from Oct 2, 2023

Conversation

yuyi98
Copy link
Member

@yuyi98 yuyi98 commented Oct 2, 2023

This PR fix struct init with multi option fn type (fix #19478).

  • Fix struct init with multi option fn type.
  • Add test.
// CycleMenuItem is a menu item that can be cycled through by pressing a button.
pub struct CycleMenuItem {
mut:
	label  string
	values []string
	on     CycleMenuItemEvents
}

// CycleMenuItemEvents is a set of events that can be invoked by a
// `CycleMenuItem`.
[params]
pub struct CycleMenuItemEvents {
__global:
	click ?fn (string) bool
	cycle ?fn (string) bool
}

// CycleMenuItem.new creates a new `CycleMenuItem` with the given label, values,
// and function to invoke when the item is selected.
[inline]
pub fn CycleMenuItem.new(label string, values []string, events CycleMenuItemEvents) CycleMenuItem {
	return CycleMenuItem{
		label: label
		values: values
		on: events
	}
}

fn main() {
	item := CycleMenuItem.new('FPS', ['30', '60', '90', '120', '144', '165', 'unlimited'],
		click: fn (value string) bool {
			if value == 'unlimited' {
				return true
			} else {
				return false
			}
		}
	)
	func := item.on.click?
	ret := func('unlimited')
	println(ret)
	assert ret
}

PS D:\Test\v\tt1> v run .
true

Copy link
Member

@spytheman spytheman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very elegant. Excellent work.

@spytheman spytheman merged commit e3caae9 into vlang:master Oct 2, 2023
46 checks passed
@yuyi98 yuyi98 deleted the fix_option_fn_type branch October 2, 2023 14:46
Wertzui123 pushed a commit to Wertzui123/v that referenced this pull request Oct 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

C error from optional anonymous functions and if str_var == 'some_string' comparison
2 participants