digitalmars.D.learn - How to fix "typesafe variadic function parameter"?
- Andrey Zherikov (19/19) May 24 2022 How to fix this?
- Adam Ruppe (6/7) May 24 2022 return S(s.dup);
- Andrey Zherikov (4/11) May 24 2022 That works, thank you!
- Steven Schveighoffer (9/24) May 24 2022 What is happening is that you have an `auto` return function, which
How to fix this? ```d struct S { string[] s; } auto foo(string[] s...) // Error: typesafe variadic function parameter `s` of type `string[]` cannot be marked `return` { return S(s); } void main() { import std.stdio: writeln; writeln(foo("Hello D")); } ``` This successfully compiles with dmd 2.099.1 but fails with 2.100.0 I tried to add `return`/`ref` but no luck.
May 24 2022
On Tuesday, 24 May 2022 at 22:46:55 UTC, Andrey Zherikov wrote:return S(s);return S(s.dup); The variadic lives in a temporary array that expires at the end of the function. So copying it out to the GC lets it live on. Your code was wrong on 2.099 too, but the compiler didn't tell you.
May 24 2022
On Tuesday, 24 May 2022 at 22:51:50 UTC, Adam Ruppe wrote:On Tuesday, 24 May 2022 at 22:46:55 UTC, Andrey Zherikov wrote:That works, thank you! Can this error message be improved some way so this fix becomes obvious?return S(s);return S(s.dup); The variadic lives in a temporary array that expires at the end of the function. So copying it out to the GC lets it live on. Your code was wrong on 2.099 too, but the compiler didn't tell you.
May 24 2022
On 5/24/22 6:54 PM, Andrey Zherikov wrote:On Tuesday, 24 May 2022 at 22:51:50 UTC, Adam Ruppe wrote:What is happening is that you have an `auto` return function, which infers attributes. The parameter is being returned, which means it infers `return` on the parameter. Then decides that it's not legal. The compiler error message should be more specific about inferred attribute errors and why they occurred, because they are super-confusing. I believe there are some recent gains in this space. But it's not 100% yet. -SteveOn Tuesday, 24 May 2022 at 22:46:55 UTC, Andrey Zherikov wrote:That works, thank you! Can this error message be improved some way so this fix becomes obvious?return S(s);return S(s.dup); The variadic lives in a temporary array that expires at the end of the function. So copying it out to the GC lets it live on. Your code was wrong on 2.099 too, but the compiler didn't tell you.
May 24 2022