Skip to content

checker,gen: allow using methods as function pointers#14407

Merged
medvednikov merged 2 commits intomasterfrom
pass_methods
May 15, 2022
Merged

checker,gen: allow using methods as function pointers#14407
medvednikov merged 2 commits intomasterfrom
pass_methods

Conversation

@spaceface777
Copy link
Copy Markdown
Member

No description provided.

Copy link
Copy Markdown
Contributor

@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.

Brilliant!

@spytheman
Copy link
Copy Markdown
Contributor

It could even run this:

module main

import gg
import gx

[heap]
struct App {
mut:
	gg     &gg.Context = 0
	radius f64 = 10.0
}

fn (mut app App) frame(x voidptr) {
        // NB: here x is === voidptr(app), and gg will call the method 🎉 ...
	app.gg.begin()
	app.gg.draw_circle_empty(150, 150, f32(app.radius), gx.blue)
	app.gg.draw_text(20, 20, 'radius: $app.radius')
	app.gg.end()
}

fn main() {
	mut app := &App{}
	app.gg = gg.new_context(
		bg_color: gx.white
		width: 300
		height: 300
		window_title: 'Circles'
		frame_fn: app.frame // passing a method as a callback works fine!
		user_data: app
	)
	app.gg.run()
}

@larpon
Copy link
Copy Markdown
Contributor

larpon commented May 15, 2022

Fantastic! 🥳

@medvednikov medvednikov merged commit c01a8a1 into master May 15, 2022
@spaceface777 spaceface777 deleted the pass_methods branch May 15, 2022 15:30
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.

5 participants