Question about compile-time functions.

2016-12-13 Thread Bauss via Digitalmars-d-learn
If a function is only called during compile-time will it be 
available at runtime?


Re: Dynamic arrays with static initialization and maybe a bug with sizeof

2016-12-13 Thread Xavier Bigand via Digitalmars-d-learn

Le 13/12/2016 23:44, Johan Engelen a écrit :

On Tuesday, 13 December 2016 at 21:27:57 UTC, Xavier Bigand wrote:

Hi,

I have the following code snippet :
voidset()
{
GLfloat[]data = [
-1.0f, -1.0f, 0.0f,
1.0f, -1.0f, 0.0f,
0.0f,  1.0f, 0.0f,
];

glBindVertexArray(mVAO);
glBufferData(GL_ARRAY_BUFFER, data.sizeof, cast(void*)data,
GL_STATIC_DRAW);
}


And I ask my self about the memory management of data, as my data
array is statically initialized is it allocated on stack?


Note that if you can define the data array as immutable, you save on
heap memory allocation + copying (LDC, from -O0):
https://godbolt.org/g/CNrZR7

-Johan



Thank you for the tips.


Re: DRY version of `static if(__traits(compiles, expr)) fun(expr)`

2016-12-13 Thread Timon Gehr via Digitalmars-d-learn

On 14.12.2016 00:00, Timothee Cour via Digitalmars-d-learn wrote:

what's the best (and DRY) way to achieve:

```
static if(__traits(compiles, expr))
  fun(expr);
```

ie, without repeating the expression inside expr?

eg:

```
static if(__traits(compiles, foo.bar[2])){
  counter++;
  writeln(" expr = ", foo.bar[2]);
}

```



I usually do

enum code = q{expr};
static if(__traits(compiles,mixin(code)))
fun(mixin(code));



Re: Accessing members through pointers to structs (also, CTFE associative arrays)

2016-12-13 Thread Ali Çehreli via Digitalmars-d-learn

On 12/13/2016 01:36 PM, Ali wrote:


Now about that second part of my problem 


I'm not entirely sure whether this should work but I think the problem 
is with mutating the 'frequencies' member of an immutable element of 
'rooms'. The error message means that those non-const expressions cannot 
be shared by a member of an immutable AA.


I moved the frequencies to 'data' and hte following worked. Used 'shared 
static this' so that the data is populated per application instead of 
per thread. Additionally, the 'd.room.name.writeln' expression works 
just fine with DMD64 D Compiler v2.072.1.


import std.algorithm: splitter, map;
import std.array: array;
import std.typecons: Tuple;
import std.stdio: writeln;

static immutable Room[] rooms = import("data.txt").splitter.map!parse.array;

struct Room {
string name;
}

static Tuple!(const(Room*), "room", int[char], 
"frequencies")[rooms.length] data;


shared static this() {
foreach (i, ref room; rooms) {
data[i].room = 
foreach (letter; room.name) {
data[i].frequencies[letter]++;
}
}
}

Room parse(string line) pure {
immutable name = line;
return Room(name);
}

void main() {
foreach (d; data) {
d.room.name.writeln;
d.frequencies.writeln;
}
}

Ali



DRY version of `static if(__traits(compiles, expr)) fun(expr)`

2016-12-13 Thread Timothee Cour via Digitalmars-d-learn
what's the best (and DRY) way to achieve:

```
static if(__traits(compiles, expr))
  fun(expr);
```

ie, without repeating the expression inside expr?

eg:

```
static if(__traits(compiles, foo.bar[2])){
  counter++;
  writeln(" expr = ", foo.bar[2]);
}

```


Re: arsd.cgi - maximum length of form post

2016-12-13 Thread bachmeier via Digitalmars-d-learn

On Tuesday, 13 December 2016 at 00:54:43 UTC, Adam D. Ruppe wrote:

On Tuesday, 13 December 2016 at 00:48:44 UTC, bachmeier wrote:
a range violation error core.exception.RangeError@test.d(109): 
Range violation


What's that line of your code too?


Here is a minimal program that can replicate the problem. 
Compiled and run with

dmd finderror.d cgi.d -version=embedded_httpd
./finderror


import arsd.cgi;
import std.array, std.conv, std.datetime, std.file, std.process;

string input() {
return `


`;
}

string simpleHtmlEncode(string s) {
  return s.replace("&", "").
  replace("<", "").replace(">", "");
}

void handler(Cgi cgi) {
  cgi.setResponseContentType("text/html; charset=UTF-8");
  string data;
  switch (cgi.pathInfo.simpleHtmlEncode()) {
  case "/":
data = input();
break;
case "/submission":
string foo = cgi.post["note"];
break;
default:
data = "Not a valid page. Try again.";
break;
}
cgi.write(data, true);
}
mixin GenericMain!handler;



Re: Dynamic arrays with static initialization and maybe a bug with sizeof

2016-12-13 Thread Johan Engelen via Digitalmars-d-learn

On Tuesday, 13 December 2016 at 21:27:57 UTC, Xavier Bigand wrote:

Hi,

I have the following code snippet :
voidset()
{
GLfloat[]   data = [
-1.0f, -1.0f, 0.0f,
1.0f, -1.0f, 0.0f,
0.0f,  1.0f, 0.0f,
];

glBindVertexArray(mVAO);
		glBufferData(GL_ARRAY_BUFFER, data.sizeof, cast(void*)data, 
GL_STATIC_DRAW);

}


And I ask my self about the memory management of data, as my 
data array is statically initialized is it allocated on stack?


Note that if you can define the data array as immutable, you save 
on heap memory allocation + copying (LDC, from -O0):

https://godbolt.org/g/CNrZR7

-Johan



Using Nsight with VisualD

2016-12-13 Thread Xavier Bigand via Digitalmars-d-learn

Hi,

I am trying to use Nsight with VisualD by it tell me that it can not 
start the program "". It seems that it does not use the right property 
of the project to retrieve the path of my generated binary.


I do not know if it is an issue of VisualD directly or DUB that don't 
correctly generate the VisualD project and miss to fill a field.


Does someone use Nsight and VisualD?


Re: Dynamic arrays with static initialization and maybe a bug with sizeof

2016-12-13 Thread Xavier Bigand via Digitalmars-d-learn

Le 13/12/2016 22:39, ag0aep6g a écrit :

On 12/13/2016 10:27 PM, Xavier Bigand wrote:

voidset()
{
GLfloat[]data = [
-1.0f, -1.0f, 0.0f,
1.0f, -1.0f, 0.0f,
0.0f,  1.0f, 0.0f,
];

glBindVertexArray(mVAO);
glBufferData(GL_ARRAY_BUFFER, data.sizeof, cast(void*)data,
GL_STATIC_DRAW);
}


And I ask my self about the memory management of data, as my data array
is statically initialized is it allocated on stack?


data is a function-local variable, so there is no static initialization
going on. The array is allocated on the heap at run-time.


On another side I have a strange behavior with the sizeof that returns 8
and not 36 (9 * 4) as I am expecting.


sizeof returns the size of the dynamic array "struct", which is a
pointer and a length. Instead of sizeof, use .length and multiply with
the element type's .sizeof: data.length * GLfloat.sizeof



Seems logic, I just read the wrong table on the documentation because 
there is properties static and dynamic arrays are contiguous.


Thanks you


Re: Dynamic arrays with static initialization and maybe a bug with sizeof

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

On 12/13/2016 10:27 PM, Xavier Bigand wrote:

voidset()
{
GLfloat[]data = [
-1.0f, -1.0f, 0.0f,
1.0f, -1.0f, 0.0f,
0.0f,  1.0f, 0.0f,
];

glBindVertexArray(mVAO);
glBufferData(GL_ARRAY_BUFFER, data.sizeof, cast(void*)data,
GL_STATIC_DRAW);
}


And I ask my self about the memory management of data, as my data array
is statically initialized is it allocated on stack?


data is a function-local variable, so there is no static initialization 
going on. The array is allocated on the heap at run-time.



On another side I have a strange behavior with the sizeof that returns 8
and not 36 (9 * 4) as I am expecting.


sizeof returns the size of the dynamic array "struct", which is a 
pointer and a length. Instead of sizeof, use .length and multiply with 
the element type's .sizeof: data.length * GLfloat.sizeof


Re: Accessing members through pointers to structs (also, CTFE associative arrays)

2016-12-13 Thread Ali via Digitalmars-d-learn

On Tuesday, 13 December 2016 at 21:33:11 UTC, Ali Çehreli wrote:

On 12/13/2016 12:30 PM, Ali wrote:

> foreach (i, room; rooms) {
> data[i].room = 

That is the address of the local variable room. You need to use 
'ref' there:


foreach (i, ref room; rooms) {

> - Ali



Ahh true!! Cheers. Now about that second part of my problem 



Ali
"the real one :o)"


Haha :)


Re: Alias variable from another class

2016-12-13 Thread Ali via Digitalmars-d-learn
I guess it's because you're accessing a member of _1 from inside 
a scope of _2 where nothing has been constructed yet? Someone 
more familiar with D would probably have a better answer on this 
part. I'm quite new :)


But a work around would be

class _2 {
  _1 instance;

  auto twelve() @property {
  return instance.i;
  }

  this() {
  instance = new _1(12);
  }
  }




Re: Accessing members through pointers to structs (also, CTFE associative arrays)

2016-12-13 Thread Ali Çehreli via Digitalmars-d-learn

On 12/13/2016 12:30 PM, Ali wrote:

> foreach (i, room; rooms) {
> data[i].room = 

That is the address of the local variable room. You need to use 'ref' there:

foreach (i, ref room; rooms) {

> - Ali

Ali
"the real one :o)"



Re: Accessing members through pointers to structs (also, CTFE associative arrays)

2016-12-13 Thread bauss (wtf happend to my name took some old cached title LOL??) via Digitalmars-d-learn

On Tuesday, 13 December 2016 at 21:21:34 UTC, Ali wrote:

On Tuesday, 13 December 2016 at 21:08:31 UTC, drug007 wrote:

(*d.room).name


Oh yeah, tried that too. That at least compiles but gives a 
runtime exception (bad address).


Try
(*cast(Room*)(d.room)).name


Dynamic arrays with static initialization and maybe a bug with sizeof

2016-12-13 Thread Xavier Bigand via Digitalmars-d-learn

Hi,

I have the following code snippet :
voidset()
{
GLfloat[]   data = [
-1.0f, -1.0f, 0.0f,
1.0f, -1.0f, 0.0f,
0.0f,  1.0f, 0.0f,
];

glBindVertexArray(mVAO);
		glBufferData(GL_ARRAY_BUFFER, data.sizeof, cast(void*)data, 
GL_STATIC_DRAW);

}


And I ask my self about the memory management of data, as my data array 
is statically initialized is it allocated on stack?


On another side I have a strange behavior with the sizeof that returns 8 
and not 36 (9 * 4) as I am expecting.


Re: Accessing members through pointers to structs (also, CTFE associative arrays)

2016-12-13 Thread Ali via Digitalmars-d-learn

On Tuesday, 13 December 2016 at 21:08:31 UTC, drug007 wrote:

(*d.room).name


Oh yeah, tried that too. That at least compiles but gives a 
runtime exception (bad address).





Re: reading from file

2016-12-13 Thread Ali via Digitalmars-d-learn

On Tuesday, 13 December 2016 at 16:59:17 UTC, Namal wrote:

On Tuesday, 13 December 2016 at 16:57:40 UTC, Namal wrote:
Sorry if I wasn't clear. The array should be two demensional 
and each line in text line should be a row in that 2x2 array.


Also, it should be saved as an integer.


And extending Ali's solution you can actually get the data in to 
a two dimentional array at compile time and have it in static 
memory with a small adjustment:


static immutable matrix = import("data.txt")
.split("\n")
.map!(a => a.split(",").map!(to!int).array)
.array;

void main() {
writeln(matrix);
}


Re: Accessing members through pointers to structs (also, CTFE associative arrays)

2016-12-13 Thread drug007 via Digitalmars-d-learn

On 13.12.2016 23:30, Ali wrote:

Hi, Long time watcher and recently started playing with D a bit more.
Ran in to a couple of snags that I'll combine in one post. It involves a
data set that contains a list of strings. Each string represents a Room
name. What I'm trying to do is pluck out the room names and also
calculate the frequency each letter occurs in a name, per room.

First problem is to do with pointers to structs. Here's the code:

static immutable rooms = import("data.txt").split("\n").map!parse.array;

static Tuple!(const(Room*), "room", int[char],
"frequencies")[rooms.length] data;
static this() {
 foreach (i, room; rooms) {
 data[i].room = 
 // Also calculate frequencies, but that's not important yet.
 }
}

void main() {
 foreach (d; data) {
 d.room.name.writeln; // <-- How do I access name here??
 }
}

I've tried d.(*room).name but that didn't work. There's no arrow

I'm sleepy, sorry for quick and probable wrong answer - try (*d.room).name



Accessing members through pointers to structs (also, CTFE associative arrays)

2016-12-13 Thread Ali via Digitalmars-d-learn
Hi, Long time watcher and recently started playing with D a bit 
more. Ran in to a couple of snags that I'll combine in one post. 
It involves a data set that contains a list of strings. Each 
string represents a Room name. What I'm trying to do is pluck out 
the room names and also calculate the frequency each letter 
occurs in a name, per room.


First problem is to do with pointers to structs. Here's the code:

static immutable rooms = 
import("data.txt").split("\n").map!parse.array;


static Tuple!(const(Room*), "room", int[char], 
"frequencies")[rooms.length] data;

static this() {
foreach (i, room; rooms) {
data[i].room = 
// Also calculate frequencies, but that's not important 
yet.

}
}

void main() {
foreach (d; data) {
d.room.name.writeln; // <-- How do I access name here??
}
}

I've tried d.(*room).name but that didn't work. There's no arrow 
operator. I've tried making my tuple a ref Room instead, but 
that's a no go as well. I can copy the Room object directly in to 
the tuple, but since it's already there in static immutable data 
I'd rather just have a pointer to it.


Is there a way to do that?

Second problem is to do with associative arrays. At first the 
Room object had a frequencies object in it (ie: int[char] <- 
number of times a character appears in the name).


In my parse function, if I add a foreach loop that loops through 
the letters in the room's name, and adds populates an associative 
array like so:


Room parse(string line) {
immutable name = // blah
int[char] frequencies;
foreach (letter; name) {
frequencies[letter] += 1
}
return Room(name, frequencies);
}

pragma(msg, rooms); // <- this works!

In the above case the pragma actually prints out all the Room 
objects, with their respective frequencies calculated correctly. 
But *after* it has printed it out, then a whole list of 
compilation errors that all look like this:


Error: non-constant expression ['d':1, 'r':3, 'x':1, 'e':1, 
'v':2, 'k':2, 'z':1, 't':1, 'u':1, 'p':2, 'c':1, 's':1, 'f':2, 
'i':2]


But it seems that it was calculated correctly, it just can't be 
assigned to the actual variable.


My current workaround includes taking frequencies out of the Room 
struct and calculating them inside a module constructor (hence 
the first question on the Tuple and Room *)


Are there other workarounds?

Cheers, and thanks for any help!
- Ali


Re: reading from file

2016-12-13 Thread Ali Çehreli via Digitalmars-d-learn

On 12/13/2016 08:59 AM, Namal wrote:

On Tuesday, 13 December 2016 at 16:57:40 UTC, Namal wrote:

Sorry if I wasn't clear. The array should be two demensional and each
line in text line should be a row in that 2x2 array.


Also, it should be saved as an integer.


Here is another one:

import std.stdio;
import std.algorithm;
import std.array;
import std.conv;

void main() {
auto a = File("deneme.txt")
 .byLine
 .map!(line => line.splitter(',').map!(to!int).array)
 .array;

writeln(a);
}

Ali



Alias variable from another class

2016-12-13 Thread Begah via Digitalmars-d-learn

I made a little program to illustrate my confusion :

  import std.stdio;

  class _1 {
  int i;

  this(int n) {
  i = n;
  }
  }

  class _2 {
  _1 instance;

  alias twelve = instance.i;

  this() {
  instance = new _1(12);
  }
  }

  void main() {
  _2 two = new _2();
  writeln(two.instance.i);
  writeln(two.twelve);
  }

What i tried to do is create an alias in class _2 of a variable 
in class _1.

What i would like is :
  two.twelve
to extends to:
  two.instance.i
But the compiler complains it cannot find "this" :
  Error: need 'this' for 'i' of type 'int'

Any ideas?


Re: this is not an lvalue

2016-12-13 Thread kinke via Digitalmars-d-learn

On Tuesday, 13 December 2016 at 17:52:21 UTC, Adam D. Ruppe wrote:

On Tuesday, 13 December 2016 at 17:26:28 UTC, kink wrote:

Assuming `Parameter` is a class and not a struct, yes.


Even if it is a struct, ref wouldn't make a difference here 
because the variable is assigned to a member, which means ref 
would get lost.


I should have been more precise, I meant the type of `this`, not 
`Parameter` (I actually assumed both would be the same). ;)


Re: this is not an lvalue

2016-12-13 Thread Adam D. Ruppe via Digitalmars-d-learn

On Tuesday, 13 December 2016 at 17:26:28 UTC, kink wrote:

Assuming `Parameter` is a class and not a struct, yes.


Even if it is a struct, ref wouldn't make a difference here 
because the variable is assigned to a member, which means ref 
would get lost.


Re: this is not an lvalue

2016-12-13 Thread kink via Digitalmars-d-learn

On Tuesday, 13 December 2016 at 16:23:16 UTC, Adam D. Ruppe wrote:

On Tuesday, 13 December 2016 at 15:09:10 UTC, Andrey wrote:

void moveTo(ref Parameter parent) {


You probably don't want the `ref` there, nor likely on any 
other method definitions (especially check removeValue).


Assuming `Parameter` is a class and not a struct, yes. Be sure to 
check out the crucial distinction between the two in D (reference 
vs. value types, similar to C# class and struct), otherwise you 
won't get far.


Re: reading from file

2016-12-13 Thread Namal via Digitalmars-d-learn
Sorry if I wasn't clear. The array should be two demensional and 
each line in text line should be a row in that 2x2 array.


Re: reading from file

2016-12-13 Thread Namal via Digitalmars-d-learn

On Tuesday, 13 December 2016 at 16:57:40 UTC, Namal wrote:
Sorry if I wasn't clear. The array should be two demensional 
and each line in text line should be a row in that 2x2 array.


Also, it should be saved as an integer.


Re: this is not an lvalue

2016-12-13 Thread Adam D. Ruppe via Digitalmars-d-learn

On Tuesday, 13 December 2016 at 15:09:10 UTC, Andrey wrote:

void moveTo(ref Parameter parent) {


You probably don't want the `ref` there, nor likely on any other 
method definitions (especially check removeValue).


Re: reading from file

2016-12-13 Thread bluecat via Digitalmars-d-learn

On Tuesday, 13 December 2016 at 15:42:16 UTC, Namal wrote:
Hello, comming from C++, I find it hard to remember and 
understand how reading from file should be done in D. 
Especially since I am not very good in functional programming. 
So I have a file which looks like this:


1,2,3,4
5,6,7,8
9,11,11,12

and so on

How could I read it row by row and create an array accordingly 
without reading the comma?


import std.stdio;
import std.string:strip;
import std.algorithm: splitter, each;
import std.array: array;

void main() {
  //prepare variables
  File file = File("new.txt", "r");
  string[] arr;

  //read file
  while(!file.eof) {
file
  .readln
  .strip
  .splitter(",")
  .array
  .each!(n => arr ~= n);
  }
  arr.writeln;
}

//here is my attempt. copy, paste, run the program to see if it 
is what you want.
//feel free to ask any questions about my code, it isn't perfect 
but it works.


Re: reading from file

2016-12-13 Thread rikki cattermole via Digitalmars-d-learn

On 14/12/2016 4:42 AM, Namal wrote:

Hello, comming from C++, I find it hard to remember and understand how
reading from file should be done in D. Especially since I am not very
good in functional programming. So I have a file which looks like this:

1,2,3,4
5,6,7,8
9,11,11,12

and so on

How could I read it row by row and create an array accordingly without
reading the comma?


import std.stdio : File;
import std.algorithm : splitter, count;
import std.conv : to;

foreach(line; File("myfile.csv", "r").byLine) {
int[] linetemp;
linetemp.length = line.count(",") + 1;

size_t i;
foreach(num; line.splitter(",")) {
linetemp[i] = to!int(num);
i++;
}

// ... = linetemp.dup;
}

This could be done a lot simpler especially with std.csv help for your 
case. But this should work with only one allocation per line.


reading from file

2016-12-13 Thread Namal via Digitalmars-d-learn
Hello, comming from C++, I find it hard to remember and 
understand how reading from file should be done in D. Especially 
since I am not very good in functional programming. So I have a 
file which looks like this:


1,2,3,4
5,6,7,8
9,11,11,12

and so on

How could I read it row by row and create an array accordingly 
without reading the comma?


this is not an lvalue

2016-12-13 Thread Andrey via Digitalmars-d-learn
Hello, I'm newbie in D. I'm trying to rewrite my project from C++ 
and now I get strange warning:

src/e2ml/node.d(23,22): Deprecation: this is not an lvalue
As I write the following code

void moveTo(ref Parameter parent) {
this.p_parent.removeValue(this);
this.p_parent = parent;
this.updatePath();
}

How to fix it?