Andrew Tomazos
2018-11-29 04:57:55 UTC
It seems to me that a function that takes a struct combined with C++20
designated initializers gets us very close to named parameters:
struct FooParameters {
int bar;
float baz;
};
float foo(FooParameters params) {
return (params.bar + 3) * params.baz;
}
int main() {
foo({bar: 4, baz: 3.2});
}
If we added some syntactic sugar that...
1. Removed the need to give the parameter struct type a name
2. Introduced the parameter struct members into the function definition
scope.
...we would get pretty close:
float foo(struct { int bar; float baz }) {
return (bar + 3) * baz;
}
int main() {
foo({bar: 4, baz:3.2});
}
I'd need to take a look to see if it is ambiguous, but I think we can drop
the struct keyword from the above:
float foo({int bar; float baz}) {
return (bar + 3) * baz;
}
int main() {
foo({bar: 4, baz:3.2});
}
This would be equivalent to:
struct __A { int bar; float baz };
float foo(__A __a) {
auto&& [bar,baz] = __a;
return (bar + 3) * baz;
}
int main() {
foo({bar: 4, baz:3.2});
}
From a teachability perspective its pretty easy to explain that "a
brace-enclosed function parameter is the body of an anonymous struct, the
members of which are introduced into the function definition scope."
This also seems easy to implement.
Thoughts? Worth pursuing?
designated initializers gets us very close to named parameters:
struct FooParameters {
int bar;
float baz;
};
float foo(FooParameters params) {
return (params.bar + 3) * params.baz;
}
int main() {
foo({bar: 4, baz: 3.2});
}
If we added some syntactic sugar that...
1. Removed the need to give the parameter struct type a name
2. Introduced the parameter struct members into the function definition
scope.
...we would get pretty close:
float foo(struct { int bar; float baz }) {
return (bar + 3) * baz;
}
int main() {
foo({bar: 4, baz:3.2});
}
I'd need to take a look to see if it is ambiguous, but I think we can drop
the struct keyword from the above:
float foo({int bar; float baz}) {
return (bar + 3) * baz;
}
int main() {
foo({bar: 4, baz:3.2});
}
This would be equivalent to:
struct __A { int bar; float baz };
float foo(__A __a) {
auto&& [bar,baz] = __a;
return (bar + 3) * baz;
}
int main() {
foo({bar: 4, baz:3.2});
}
From a teachability perspective its pretty easy to explain that "a
brace-enclosed function parameter is the body of an anonymous struct, the
members of which are introduced into the function definition scope."
This also seems easy to implement.
Thoughts? Worth pursuing?
--
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+***@isocpp.org.
To post to this group, send email to std-***@isocpp.org.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/CAB%2B4KHLe6zDPRreF2q0zQptUgrO0-_u8VrAOA7Du%2B_KHxhGUnw%40mail.gmail.com.
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+***@isocpp.org.
To post to this group, send email to std-***@isocpp.org.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/CAB%2B4KHLe6zDPRreF2q0zQptUgrO0-_u8VrAOA7Du%2B_KHxhGUnw%40mail.gmail.com.