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

Support splat for varargs #1352

Closed
ygorpontelo opened this issue Aug 12, 2024 · 12 comments
Closed

Support splat for varargs #1352

ygorpontelo opened this issue Aug 12, 2024 · 12 comments
Assignees
Labels
Accepted Accepted Request Enhancement Request New feature or request Implemented Needs Verification Check if this issue is resolved
Milestone

Comments

@ygorpontelo
Copy link

ygorpontelo commented Aug 12, 2024

In the case of the string::tformat function, it accepts a variable number of arguments to pass to the format string. Would be nice to be able to unpack an array when calling it, for example:

String[] parts = {"one", "two"};
String result = string::tformat("%s %s", ...parts);

Currently, i get this error when trying: Error: Splat may not be used with raw varargs. Trying to cast the parts array to any[] also doesn't seem to work.

@lerno lerno added the Enhancement Request New feature or request label Aug 12, 2024
@lerno
Copy link
Collaborator

lerno commented Aug 12, 2024

This is correct, as the compiler currently works but as an enhancement, this could be made to work in the general case, which will also increase the usefulness of the splat.

@lerno lerno self-assigned this Aug 12, 2024
@lerno lerno added the In Progress This task is currently being worked on label Aug 16, 2024
@lerno lerno added this to the 0.6.3 milestone Sep 7, 2024
lerno added a commit that referenced this issue Sep 7, 2024
@lerno lerno added Implemented Needs Verification Check if this issue is resolved and removed In Progress This task is currently being worked on labels Sep 10, 2024
@lerno
Copy link
Collaborator

lerno commented Sep 10, 2024

This should be completely supported now.

@LukyGuyLucky
Copy link

This should be completely supported now.

Sorry,no,it doesn't work with the latest c3c.Error: Splat may not be used with raw varargs if the length is not known.

@ygorpontelo
Copy link
Author

Just tested this, got the same error as LukyGuyLucky. The length should be know since i was using a fixed array, unless that is not the length it is referring to.

@lerno
Copy link
Collaborator

lerno commented Sep 18, 2024

module test;
extern fn void test(...);

fn void main()
{
	int[2] y;
	test(...y);
}

This fails?

@lerno
Copy link
Collaborator

lerno commented Sep 18, 2024

If you do:

extern fn void test(...);

fn void main()
{
	int[] y =1, 2 };
	test(...y);
}

Then this will fail, as you are splatting a runtime slice into a raw vararg.

However, this will succeed:

extern fn void test(...);

fn void main()
{
	int[] y =1, 2 };
	test(...y[:2]);
}

Because then the compiler will know the length.

@ygorpontelo
Copy link
Author

I thought int[] y = {1, 2} was the same as int[2] y = {1,2} just a different notation. Is that distinction useful? Feels like a pitfall to me.

@lerno
Copy link
Collaborator

lerno commented Sep 18, 2024

int[*] y = { 1, 2 } is the same as int[2] y = { 1, 2 }. int[] y = { 1, 2 } creates a slice, the former two are int[2]

@lerno
Copy link
Collaborator

lerno commented Sep 18, 2024

I've improved the error message now though, and enabled:

fn void test2(int a, int b) { }
fn void main()
{
  int[2] x = { 1, 2 };
  test2(...x);
}

@ygorpontelo
Copy link
Author

Didn't know about the [*] notation. With the new message and the improvement above this looks pretty solid, i'm eager to test it.

@lerno lerno added the Accepted Accepted Request label Sep 20, 2024
@Caleb-rg
Copy link

@lerno Can confirm in the latest that this is working:

fn void test2(int a, int b) {
  io::printfn("a = %s, b = %s", a, b); // a = 5, b = 22
}

fn void main()
{
  int[*] x = { 5, 22 };
  test2(...x);
}

Adding three values errors correctly:
Error: This argument would exceed the number of parameters, did you add too many arguments?

Adding one value errors correctly:
Expected 1 more argument after this one, did you forget it?

@lerno
Copy link
Collaborator

lerno commented Sep 25, 2024

Thank you!

@lerno lerno closed this as completed Sep 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Accepted Accepted Request Enhancement Request New feature or request Implemented Needs Verification Check if this issue is resolved
Projects
None yet
Development

No branches or pull requests

4 participants