Re: Filling an array at compile time

2022-02-09 Thread bauss via Digitalmars-d-learn

On Wednesday, 9 February 2022 at 16:37:22 UTC, Ali Çehreli wrote:

On 2/9/22 01:07, bauss wrote:

> It will not run at compile-time because csvText is a runtime
variable.
> It should be enum to be accessible at compile-time.

Yes. For the sake of completeness, any expression needed at 
compile time will be (attempted to be) executed at compile 
time. For example, an expression used as a template parameter 
will be executed at compile time as well.


> The append operation
> will be executed at runtime, which means that even if the
loop runs at
> compile-time then you're effectively not winning anything and
it
> basically just becomes a loop-unroll manually done.

That's not true. It all depends on how the expression is 
needed. If for example, the variable were defined as enum, the 
compiler had to execute the code at compile time to compute its 
value.


> The solution would be to create a function that returns a
string [...]
> And then simply using mixin

That is unnecessary and hurts readability. :/ Most programmers 
see string mixins as a last resort.


In any case, some people may find a compile-time file parsing 
example that I included in a presentation:


  https://youtu.be/dRORNQIB2wA?t=3157

Ali


Thank you Ali for the clarifications


Re: Filling an array at compile time

2022-02-09 Thread Ali Çehreli via Digitalmars-d-learn

On 2/9/22 08:37, Ali Çehreli wrote:

> In any case, some people may find a compile-time file parsing example
> that I included in a presentation:

That should mean "may find interesting". And I've just realized that the 
chapter link is off in that video. This is the beginning of that section:


  https://www.youtube.com/watch?v=dRORNQIB2wA=3065s

Ali



Re: Filling an array at compile time

2022-02-09 Thread Ali Çehreli via Digitalmars-d-learn

On 2/9/22 01:07, bauss wrote:

> It will not run at compile-time because csvText is a runtime variable.
> It should be enum to be accessible at compile-time.

Yes. For the sake of completeness, any expression needed at compile time 
will be (attempted to be) executed at compile time. For example, an 
expression used as a template parameter will be executed at compile time 
as well.


> The append operation
> will be executed at runtime, which means that even if the loop runs at
> compile-time then you're effectively not winning anything and it
> basically just becomes a loop-unroll manually done.

That's not true. It all depends on how the expression is needed. If for 
example, the variable were defined as enum, the compiler had to execute 
the code at compile time to compute its value.


> The solution would be to create a function that returns a string [...]
> And then simply using mixin

That is unnecessary and hurts readability. :/ Most programmers see 
string mixins as a last resort.


In any case, some people may find a compile-time file parsing example 
that I included in a presentation:


  https://youtu.be/dRORNQIB2wA?t=3157

Ali



Re: Filling an array at compile time

2022-02-09 Thread user1234 via Digitalmars-d-learn

On Wednesday, 9 February 2022 at 10:25:34 UTC, bauss wrote:
Is it guaranteed that the value is initialized at compiletime 
however?


yes, D guarentees this at 100%.




Re: Filling an array at compile time

2022-02-09 Thread Vindex via Digitalmars-d-learn

On Wednesday, 9 February 2022 at 10:01:15 UTC, Anonymouse wrote:

On Wednesday, 9 February 2022 at 08:12:52 UTC, Vindex wrote:

[...]


I would do this.

```
import std;

alias Record = Tuple!(string, string, string);

static immutable string[][] table = () {
string[][] table;
string csvText = import("file.csv");

foreach (record; csvReader!Record(csvText)) {
table ~= [record[0], record[1], record[2]];
}

return table;
}();

pragma(msg, table);  // Available at compile-time

void main() {
writeln(table);
}
```

And then `-J{path}` to tell the compiler where to find 
`file.csv`.


```
dmd -J. csv.d
```


Thanks! It does indeed work at compile time.


Re: Filling an array at compile time

2022-02-09 Thread bauss via Digitalmars-d-learn

On Wednesday, 9 February 2022 at 10:01:15 UTC, Anonymouse wrote:

On Wednesday, 9 February 2022 at 08:12:52 UTC, Vindex wrote:
Will the loop (foreach) run at compile time? How can I make it 
work at compile time?


```
import std.csv, std.stdio;

alias Record = Tuple!(string, string, string);

immutable string[][] table;

shared static this() {
string csvText = import("file.csv");
foreach (record; csvReader!Record(csvText)) {
table ~= [record[0], record[1], record[2]];
}
}

void main() {
writeln(table):
}
```


I would do this.

```
import std;

alias Record = Tuple!(string, string, string);

static immutable string[][] table = () {
string[][] table;
string csvText = import("file.csv");

foreach (record; csvReader!Record(csvText)) {
table ~= [record[0], record[1], record[2]];
}

return table;
}();

pragma(msg, table);  // Available at compile-time

void main() {
writeln(table);
}
```

And then `-J{path}` to tell the compiler where to find 
`file.csv`.


```
dmd -J. csv.d
```


Is it guaranteed that the value is initialized at compiletime 
however?


Something being available at compiletime isn't the same as being 
initialized at compiletime only.


If it's guaranteed then that's indeed the best solution.


Re: Filling an array at compile time

2022-02-09 Thread Anonymouse via Digitalmars-d-learn

On Wednesday, 9 February 2022 at 08:12:52 UTC, Vindex wrote:
Will the loop (foreach) run at compile time? How can I make it 
work at compile time?


```
import std.csv, std.stdio;

alias Record = Tuple!(string, string, string);

immutable string[][] table;

shared static this() {
string csvText = import("file.csv");
foreach (record; csvReader!Record(csvText)) {
table ~= [record[0], record[1], record[2]];
}
}

void main() {
writeln(table):
}
```


I would do this.

```
import std;

alias Record = Tuple!(string, string, string);

static immutable string[][] table = () {
string[][] table;
string csvText = import("file.csv");

foreach (record; csvReader!Record(csvText)) {
table ~= [record[0], record[1], record[2]];
}

return table;
}();

pragma(msg, table);  // Available at compile-time

void main() {
writeln(table);
}
```

And then `-J{path}` to tell the compiler where to find `file.csv`.

```
dmd -J. csv.d
```


Re: Filling an array at compile time

2022-02-09 Thread bauss via Digitalmars-d-learn

On Wednesday, 9 February 2022 at 08:12:52 UTC, Vindex wrote:
Will the loop (foreach) run at compile time? How can I make it 
work at compile time?


```
import std.csv, std.stdio;

alias Record = Tuple!(string, string, string);

immutable string[][] table;

shared static this() {
string csvText = import("file.csv");
foreach (record; csvReader!Record(csvText)) {
table ~= [record[0], record[1], record[2]];
}
}

void main() {
writeln(table):
}
```


It will not run at compile-time because csvText is a runtime 
variable. It should be enum to be accessible at compile-time.


The second issue is that your foreach should probably be static 
foreach.


The third issue is how you're creating "table". The append 
operation will be executed at runtime, which means that even if 
the loop runs at compile-time then you're effectively not winning 
anything and it basically just becomes a loop-unroll manually 
done.


The solution would be to create a function that returns a string 
that is equivalent to the contents of the array you want to 
create, as if you wrote the content yourself.


And then simply using mixin to "set" the value of the table by 
calling that function.


Ex.

```
string getInt() { return "immutable a = 10;"; }

mixin(getInt);

// The variable a is accessible here ...

```


Re: Filling an array

2016-03-13 Thread user42 via Digitalmars-d-learn

On Saturday, 12 March 2016 at 18:33:16 UTC, Alex wrote:

On Saturday, 12 March 2016 at 16:37:25 UTC, user42 wrote:

On Saturday, 12 March 2016 at 14:33:19 UTC, Alex wrote:

/snip


I thought this was supposed to halt with an error rather than 
compile and set all members to 1.
The syntax, to me anyways, doesn't really communicate the 
intention of: set all members to 1.

//arr[] = 1;


Whereas the following does

fill(arr, 1);


Well, this was not the question. As stated here:
https://dlang.org/spec/arrays.html
in the section "array setting", it is possible to set an array 
in such a manner. And my question was, why a specific array 
behaves not as expected.
So, either there is a problem with filling an array, or, there 
is a problem with implicit conversion of a Nullable!T to its 
underlying type.


Learned something new. I guess I missed that detail when I read 
that page.




Re: Filling an array

2016-03-12 Thread Alex via Digitalmars-d-learn

On Saturday, 12 March 2016 at 19:35:30 UTC, ag0aep6g wrote:

On 12.03.2016 16:44, Mike Parker wrote:

arr[] = cast(Nullable!uint)1;


Nicer than a cast: construct a Nullable!int.

arr[] = Nullable!uint(1);


ok... so... this makes the error very strange, then... almost 
senseless...


Re: Filling an array

2016-03-12 Thread ag0aep6g via Digitalmars-d-learn

On 12.03.2016 16:44, Mike Parker wrote:

arr[] = cast(Nullable!uint)1;


Nicer than a cast: construct a Nullable!int.

arr[] = Nullable!uint(1);


Re: Filling an array

2016-03-12 Thread Alex via Digitalmars-d-learn

On Saturday, 12 March 2016 at 16:37:25 UTC, user42 wrote:

On Saturday, 12 March 2016 at 14:33:19 UTC, Alex wrote:

/snip


I thought this was supposed to halt with an error rather than 
compile and set all members to 1.
The syntax, to me anyways, doesn't really communicate the 
intention of: set all members to 1.

//arr[] = 1;


Whereas the following does

fill(arr, 1);


Well, this was not the question. As stated here:
https://dlang.org/spec/arrays.html
in the section "array setting", it is possible to set an array in 
such a manner. And my question was, why a specific array behaves 
not as expected.
So, either there is a problem with filling an array, or, there is 
a problem with implicit conversion of a Nullable!T to its 
underlying type.


Re: Filling an array

2016-03-12 Thread user42 via Digitalmars-d-learn

On Saturday, 12 March 2016 at 14:33:19 UTC, Alex wrote:

/snip


I thought this was supposed to halt with an error rather than 
compile and set all members to 1.
The syntax, to me anyways, doesn't really communicate the 
intention of: set all members to 1.

//arr[] = 1;


Whereas the following does

fill(arr, 1);




Re: Filling an array

2016-03-12 Thread Alex via Digitalmars-d-learn

On Saturday, 12 March 2016 at 15:44:00 UTC, Mike Parker wrote:

On Saturday, 12 March 2016 at 14:33:19 UTC, Alex wrote:


//arr[] = 1;

The question is, why the commented out line throws the error:
Error: cannot implicitly convert expression (1) of type int to 
Nullable!uint[],

while the line after that works.


Looks like a bug somewhere. The work around is to cast:

arr[] = cast(Nullable!uint)1;

I suggest you file this in the bug tracker [1] if it isn't 
there already. Just use he minimal code that shows the problem:


void main()
{
import std.typecons;
Nullable!uint[] arr;
arr.length = 5;
arr[] = 1;
}

[1] https://dlang.org/bugstats.php


Thanks!
Bug filed under
https://issues.dlang.org/show_bug.cgi?id=15792


Re: Filling an array

2016-03-12 Thread Mike Parker via Digitalmars-d-learn

On Saturday, 12 March 2016 at 14:33:19 UTC, Alex wrote:


//arr[] = 1;

The question is, why the commented out line throws the error:
Error: cannot implicitly convert expression (1) of type int to 
Nullable!uint[],

while the line after that works.


Looks like a bug somewhere. The work around is to cast:

arr[] = cast(Nullable!uint)1;

I suggest you file this in the bug tracker [1] if it isn't there 
already. Just use he minimal code that shows the problem:


void main()
{
import std.typecons;
Nullable!uint[] arr;
arr.length = 5;
arr[] = 1;
}

[1] https://dlang.org/bugstats.php