Thread overview | |||||
---|---|---|---|---|---|
|
November 20, 2017 One liner alternatives for initing and pushing some values into array | ||||
---|---|---|---|---|
| ||||
I need to init and push some values into a array something like: //DMD64 D Compiler 2.072.2 import std.stdio; import std.array; void main() { bool a = true; bool b = false; bool c = false; bool[] boolList; auto boolListAppender = boolList.appender(); boolListAppender ~= [ a, b, c]; } Live example : http://rextester.com/TJLIXU71426 My question is about bool[] boolList; auto boolListAppender = boolList.appender(); boolListAppender ~= [ a, b, c]; I could do the same in one line with C++: bool boolList[] = { a, b, c }; D alternative(at least my D alternative) seem long. Is there any better way for initialing and pushing values at the same time. Erdem |
November 20, 2017 Re: One liner alternatives for initing and pushing some values into array | ||||
---|---|---|---|---|
| ||||
Posted in reply to kerdemdemir | On Monday, 20 November 2017 at 08:37:32 UTC, kerdemdemir wrote: > I need to init and push some values into a array something like: > > //DMD64 D Compiler 2.072.2 > import std.stdio; > import std.array; > > void main() > { > bool a = true; > bool b = false; > bool c = false; > > bool[] boolList; > auto boolListAppender = boolList.appender(); > boolListAppender ~= [ a, b, c]; > } > > Live example : http://rextester.com/TJLIXU71426 > > My question is about > > bool[] boolList; > auto boolListAppender = boolList.appender(); > boolListAppender ~= [ a, b, c]; > > > I could do the same in one line with C++: > > bool boolList[] = { a, b, c }; > Change the braces to brackets. > D alternative(at least my D alternative) seem long. Is there any better way for > initialing and pushing values at the same time. > > Erdem |
November 20, 2017 Re: One liner alternatives for initing and pushing some values into array | ||||
---|---|---|---|---|
| ||||
Posted in reply to kerdemdemir | On Monday, 20 November 2017 at 08:37:32 UTC, kerdemdemir wrote:
> bool boolList[] = { a, b, c };
>
> D alternative(at least my D alternative) seem long. Is there any better way for
> initialing and pushing values at the same time.
>
> Erdem
Your use of the appender method suggest to me you want to be able to append, as opposed to just initialise.
module test;
import std.stdio;
import std.array;
void main()
{
bool[] boolList;
bool a = true;
bool b = false;
bool c = false;
boolList ~= [ a, b, c ];
bool d = true;
bool e = false;
bool f = false;
boolList ~= [ d, e, f ];
// ...
}
|
Copyright © 1999-2021 by the D Language Foundation