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

add .args to compile time method #20926

Closed
2 tasks
jcweaver997 opened this issue Feb 29, 2024 · 4 comments · Fixed by #22229
Closed
2 tasks

add .args to compile time method #20926

jcweaver997 opened this issue Feb 29, 2024 · 4 comments · Fixed by #22229
Labels
Feature Request This issue is made to request a feature.

Comments

@jcweaver997
Copy link

jcweaver997 commented Feb 29, 2024

Describe the feature

I would like to do:

$for arg in method.args {

}

Use Case

For vgdextension and working with godot, we need a way for godot to call our methods. In order to avoid code generation, we need to be able to loop through args at compile time. We need to build structures defining our methods and create closures that are called by godot that transform the arguments and properly call our methods that are defined in V.

Proposed Solution

add .args to compile time method

Other Information

No response

Acknowledgements

  • I may be able to implement this feature request
  • This feature might incur a breaking change

Version used

V 0.4.4 70c277d

Environment details (OS name and version, etc.)

V full version: V 0.4.4 1733dab.70c277d
OS: windows, Microsoft Windows 11 Pro v26058 64-bit
Processor: 24 cpus, 64bit, little endian,

getwd: C:\Users\jcwea\Documents\git\jvnet
vexe: C:\Users\jcwea\Documents\git\v\v.exe
vexe mtime: 2024-02-29 15:47:47

vroot: OK, value: C:\Users\jcwea\Documents\git\v
VMODULES: OK, value: C:\Users\jcwea.vmodules
VTMP: OK, value: C:\Users\jcwea\AppData\Local\Temp\v_0

Git version: git version 2.40.0.windows.1
Git vroot status: weekly.2024.09-6-g70c277d4
.git/config present: true

CC version: Error: 'cc' is not recognized as an internal or external command,
operable program or batch file.

thirdparty/tcc status: thirdparty-windows-amd64 b99a453d

Note

You can use the 👍 reaction to increase the issue's priority for developers.

Please note that only the 👍 reaction to the issue itself counts as a vote.
Other reactions and those to comments will not be taken into account.

@jcweaver997 jcweaver997 added the Feature Request This issue is made to request a feature. label Feb 29, 2024
@MCausc78
Copy link
Contributor

There is?

@jcweaver997
Copy link
Author

There is not:

src/main.v:13:22: error: unknown kind `args`, available are: `methods`, `fields`, `values`, `variants` or `attributes`
   11 | fn register[T](){
   12 |     $for method in T.methods {
   13 |         $for arg in method.args {
      |                            ~~~~
   14 |         }
   15 |     }
module main

struct Struct1 {
	num f64
}

fn (mut s Struct1) do_thing(a f32) bool {
	return false
}

fn register[T](){
	$for method in T.methods {
		$for arg in method.args {
			println("${arg.name}")
		}
	}
}

pub fn main() {
	register[Struct1]()
}

@felipensp
Copy link
Member

It is accessible in another way, like:

fn register[T]() {
	$for method in T.methods {
		args := method.args
		for arg in args {
			match arg.typ {
				typeof[f32]().idx {
					println('arg [${arg.name}] is f32')
				}
				else {}
			}
		}
	}
}

@MCausc78
Copy link
Contributor

MCausc78 commented Mar 3, 2024

It is accessible in another way, like:

fn register[T]() {
	$for method in T.methods {
		args := method.args
		for arg in args {
			match arg.typ {
				typeof[f32]().idx {
					println('arg [${arg.name}] is f32')
				}
				else {}
			}
		}
	}
}

what if we want check whether method.args len is 1 at compile time, and do f() or f(0) depending on args?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feature Request This issue is made to request a feature.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants