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

Deprecate typesafe variadic class arguments #19890

Open
dlangBugzillaToGithub opened this issue Mar 19, 2021 · 6 comments · May be fixed by #20673
Open

Deprecate typesafe variadic class arguments #19890

dlangBugzillaToGithub opened this issue Mar 19, 2021 · 6 comments · May be fixed by #20673

Comments

@dlangBugzillaToGithub
Copy link

dlangBugzillaToGithub commented Mar 19, 2021

anonymous4 reported this on 2021-03-19T19:22:10Z

Transferred from https://issues.dlang.org/show_bug.cgi?id=21736

Description

struct A
{
	int a;
}
int f(A a...){ return a.a; }
static assert(f(4)==4); //not callable

Typesafe variadic functions are supported with classes, but not with structs. Is there a reason for this limitation?

@Lars-Kristiansen
Copy link

Lars-Kristiansen commented Jan 9, 2025

They are supported, the problem is that you rely on implicit construction of an A instance which does not exist in D.

The correct way to write this is

struct A
{
	int a;
}
int f(A a...){ return a.a; }
static assert(f(A(4))==4); 

I think this one can be closed as invalid.

@dkorpel
Copy link
Contributor

dkorpel commented Jan 9, 2025

They are supported, the problem is that you rely on implicit construction of an A instance which does not exist in D.

Implicit construction is supported for class arguments of a typesafe variadic function.

class A
{
    this(int a) {this.a = a;}
	int a;
}
int f(A a...){ return a.a; }
static assert(f(4) == 4);

See: https://dlang.org/spec/function.html#typesafe_variadic_functions

For other types [than arrays and classes], the argument is passed by value.

So yes, the current compiler behavior is correct, but it makes a variadic struct argument completely useless. You might as well just write:

-int f(A a...){ return a.a; }
+int f(A a){ return a.a; }

Asking why arrays and classes are supported here, but not structs, is a valid enhancement request.

However, Walter has tried to deprecate typesafe variadic arguments before, since D has so many different kinds of variadic functions that it's getting really complex. I think most D users don't even know implicit construction of classes exists, I haven't seen them being used. Perhaps instead of adding typesafe variadic structs, we should deprecate typesafe variadic classes instead. (Removing typesafe variadic arrays received some pushback, since it is being used)

@dkorpel dkorpel reopened this Jan 9, 2025
@thewilsonator
Copy link
Contributor

That is wild!

we should deprecate typesafe variadic classes instead.

Yes.

@Lars-Kristiansen
Copy link

Implicit construction is supported for class arguments of a typesafe variadic function.

sorry if I mislead people with my attempt to get this issue closed but in first place, why ? usually implicit construction is not a thing.

@Lars-Kristiansen
Copy link

Lars-Kristiansen commented Jan 9, 2025

Perhaps instead of adding typesafe variadic structs, we should deprecate typesafe variadic classes instead

yes, in a first time, then eventually later the whole "type safe variadics"-thing.

@Lars-Kristiansen
Copy link

Lars-Kristiansen commented Jan 9, 2025

Anyway, after playing with that feature, that's just silly... as you have to define a ctor. I dont understand why this was added to the language. Is that a D1 fossil ?

@dkorpel dkorpel linked a pull request Jan 16, 2025 that will close this issue
@dkorpel dkorpel changed the title Support struct in typesafe variadic function Deprecate typesafe variadic class arguments Jan 23, 2025
@dkorpel dkorpel marked this as a duplicate of #17744 Jan 23, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants