int test1()
{
static immutable string[] optsldc=["a","a"];
Put(optsldc);
return 0;
}
void Put(string item)
{
}
void Put(in string[] items...)
{
assert(items.length!=0);
foreach(item;items)Put(item);
}
int main(string[] args)
{
return test1();
}
This code crashes when run.
Comment #1 by dfj1esp02 — 2017-12-17T14:07:07Z
Look like it prefers variadic overload. Is it intended?
Comment #2 by b2.temp — 2017-12-17T19:07:39Z
It prefers "in string[]" over "string". Add the "in" parameter storage class to the overload that takes a string and it works.
---
void Put(in string[] items...)
{
foreach(item;items) Put(item);
}
void Put(in string item) { }
void test1()
{
immutable string[] optsldc=["a","a"];
Put(optsldc);
}
void main()
{
return test1();
}
---
The problem is, firstly, that the overload rules for this case are not specified.
https://dlang.org/spec/function.html#overload-sets
Comment #3 by dfj1esp02 — 2017-12-18T08:38:49Z
Originally it was a collection code. Well, const works too:
---
void put(const string item)
{
}
void put(in string[] items...)
{
assert(items.length!=0);
foreach(item;items)put(item);
}
---
Comment #4 by dfj1esp02 — 2017-12-18T08:46:08Z
So the container code would be like
---
import std.traits;
struct Container(T)
{
T c;
//argument type
static if(isAssignable!(T,const(T)))
alias AT=const(T);
else
alias AT=T;
void put(AT item)
{
c=item;
}
void put(scope AT[] items...)
{
foreach(item;items)put(item);
}
}
---
Comment #5 by dfj1esp02 — 2017-12-18T14:13:58Z
I should note that if assert in the original example is commented, ldc optimizes the whole function away, and the code runs fine, but the collection is not filled.
Comment #6 by robert.schadek — 2024-12-13T18:55:37Z