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

Allow variadic functions with variable types #21462

Open
2 tasks
susugagalala opened this issue May 8, 2024 · 5 comments
Open
2 tasks

Allow variadic functions with variable types #21462

susugagalala opened this issue May 8, 2024 · 5 comments
Labels
Feature Request This issue is made to request a feature.

Comments

@susugagalala
Copy link

susugagalala commented May 8, 2024

Describe the feature

Allow defining V fn hello(i int, f f64, ...) or declaring external C functions as such.

Use Case

At the moment it seems V does not support defining/declaring a function where the variadic part has variable types, like what C and some other programming languages allow.

Instead of defininng/declaring a series of functions:

fn hello(i int, f f64, x int, y string)
fn hello(i int, f f64, y string)
fn hello(i int, f f64, p &Peter)
...and so on...

I'd like to only define/declare a single function:

fn hello(i int, f f64, ...)

Proposed Solution

As mentioned, allow defining V functions like fn hello(i int, f f64, ...) and declaring external (eg. C) functions as such.

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

Environment details (OS name and version, etc.)

Mac OS X, 10.15.7

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.

@susugagalala susugagalala added the Feature Request This issue is made to request a feature. label May 8, 2024
@susugagalala
Copy link
Author

If it is decided that V should not allow defining V functions with variable variadic types for strong typing reasons, it is understandable. But V should at least allow declaring external C functions as such, otherwise how would I declare an external C function defined as such: void hello(int i, double f, ...) where the call could be hello(1, 2.3, a_struct) or hello(1, 2.3, "peter", 4) or hello(1, 2.3, 4, 5.6, 7) ?

@thesombady
Copy link
Contributor

thesombady commented May 8, 2024

You can circumvent this by stating that the argument is a struct

struct Args {
    x int = 0
    y f32 = 0.0
    p &Person = unsafe { nil }
    d f32 = 0.0
}

fn Hello(args Args) {
    println(args.x)
}

fn main() {
    Hello(x: 1.0)
    Hello(x: 1.0, y: 0.0, p: &Person{}, d: 3.1415)
}

With this you can define the behaviour of how the function should act with different values.
https://docs.vlang.io/structs.html#trailing-struct-literal-arguments

@JalonSolov
Copy link
Contributor

You could also create a sum type. You can do variadic sum types.

@thesombady
Copy link
Contributor

Yep something like this would work

type Arg = f32 | int | Person | string

fn Hello(arg1 Arg, arg2 Arg, arg3 Arg, arg4 Arg) {}

@JalonSolov
Copy link
Contributor

Or, to match the request...

type Arg = f32 | int | Person | string

fn Hello(args ...Arg) {}

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

No branches or pull requests

3 participants