exercise - find invalid D tokens (Impossible ?)

2016-10-27 Thread Basile B. via Digitalmars-d-learn

Here are the specifications of token strings:

"Token strings open with the characters q{ and close with the 
token }. In between must be valid D tokens. The { and }"


So we can deduce that any invalid D token inside a token string 
will lead to a compilation error. Indeed:


void main()
{
enum s = q{#}; // malformed special token sequence
}

produces an error.

So are you able to find more invalid token ?


Re: How to kill whole application if child thread raises an exception?

2016-10-27 Thread dm via Digitalmars-d-learn

I found http://arsdnet.net/this-week-in-d/2016-aug-07.html
Maybe it's helps me.


Re: How to kill whole application if child thread raises an exception?

2016-10-27 Thread dm via Digitalmars-d-learn

On Friday, 28 October 2016 at 03:38:05 UTC, dm wrote:
On Thursday, 27 October 2016 at 13:37:29 UTC, Steven 
Schveighoffer wrote:

Hm... what about:
...


Main thread still running.


Actually it's depends on compiler.
With ldc2 main thread doesn't stop, but with dmd seems all ok:
root@proxytest:~# ./newthread
object.Exception@newthread.d(30): Everything is bad.

??:? void newthread.func() [0x451f52]
??:? void newthread.mySpawn!(void function()*).mySpawn(void 
function()*).callIt(void function()*) [0x452037]
??:? void std.concurrency._spawn!(void function(void 
function()*)*, void function()*)._spawn(bool, void function(void 
function()*)*, void function()*).exec() [0x4529f4]

??:? void core.thread.Thread.run() [0x46f1f1]
??:? thread_entryPoint [0x46ef1b]
??:? [0x42d00a3]
uncaught exception
dwarfeh(224) fatal error
Aborted



Re: test if the alias of a template is a literal

2016-10-27 Thread Basile B. via Digitalmars-d-learn

On Friday, 28 October 2016 at 03:33:33 UTC, Basile B. wrote:
On Thursday, 27 October 2016 at 14:04:23 UTC, Gianni Pisetta 
wrote:
So i searched for a metod to check if an alias is a literal 
value, but found nothing. Anyone have any clue on how can be 
done?


Thanks,
Gianni Pisetta


Hello, I think the correct isStringLiteral would be:

import
std.meta;

template isStringLiteral(alias V)
{
enum isCompileTime = is(typeof((){enum a = V;}));
enum isString = is(typeof(V) == string);
enum isStringLiteral = isCompileTime && isString;
}

[...]


In addition a fallback for the types must be added:

template isStringLiteral(V){enum isStringLiteral = false;}


Re: How to kill whole application if child thread raises an exception?

2016-10-27 Thread dm via Digitalmars-d-learn
On Thursday, 27 October 2016 at 13:37:29 UTC, Steven 
Schveighoffer wrote:

Hm... what about:
...


Main thread still running.



Re: test if the alias of a template is a literal

2016-10-27 Thread Basile B. via Digitalmars-d-learn
On Thursday, 27 October 2016 at 14:04:23 UTC, Gianni Pisetta 
wrote:

Hi all,
I have an AliasSeq composed of literal strings, variables and 
delegates. I want to build a template Optimize that will return 
an AliasSeq that have all adjacent literals concatenated into 
one. So i writed something like this:


template isStringLiteral(alias V) {
enum bool isStringLiteral = ( is( typeof( V ) == string ) );
}

template Optimize(Os...) {
static if ( Os.length < 2 )
alias Optimize = Os;
else {
alias Optimized = Optimize!(Os[1..$]);

static if ( isStringLiteral!(Os[0]) && 
isStringLiteral!(Optimized[0]) ) {

enum string First = Os[0] ~ Optimized[0];
alias Rest = Optimized[1..$];
}
else {
alias First = AliasSeq!(Os[0]);
alias Rest = Optimized;
}
alias Optimize = AliasSeq!(First, Rest);
}
}

but at the moment isStringLiteral will return true even with 
variables of type string. So i searched for a metod to check if 
an alias is a literal value, but found nothing. Anyone have any 
clue on how can be done?


Thanks,
Gianni Pisetta


Hello, I think the correct isStringLiteral would be:

import
std.meta;

template isStringLiteral(alias V)
{
enum isCompileTime = is(typeof((){enum a = V;}));
enum isString = is(typeof(V) == string);
enum isStringLiteral = isCompileTime && isString;
}

unittest
{
string a;
enum b = "0";
enum c = 0;
static assert(!isStringLiteral!a);
static assert(isStringLiteral!b);
static assert(!isStringLiteral!c);
}

It's decomposed to show the logic:

1. If the delegate that assigns the parameter to a compile-time 
enum is valid we know that the parameter value is defined at 
compile-time.

2. check that the parameter is a string.


Re: A question of function design?

2016-10-27 Thread pineapple via Digitalmars-d-learn

On Thursday, 27 October 2016 at 22:17:35 UTC, WhatMeWorry wrote:


I'm using Derelict GLFW3 and I found the following GLFW3 code 
snippet in a demo.


In a small demo, crap like this usually isn't a big deal.

It's not common practice, though, and for good reason. You should 
definitely avoid imitating it.


A question of function design?

2016-10-27 Thread WhatMeWorry via Digitalmars-d-learn


I'm using Derelict GLFW3 and I found the following GLFW3 code 
snippet in a demo.



float distance = 3.0;

extern(C) void key_callback(GLFWwindow* window, int key, int 
scancode, int action, int modifier) nothrow

{
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
{
glfwSetWindowShouldClose(window, GL_TRUE);
}
if (key == GLFW_KEY_TAB && action == GLFW_PRESS)
{
distance = distance + .25;
}
}


I'm looking at the call back function and all but one of the 
parameters are pass by values and there is no return type.  So 
they use this distance variable (is this called a global 
variable?) which I thought was considered bad programing and the 
classical example of a side-effect.  Isn't this particularly 
dangerous as programs get larger and larger.


So given the way this function was designed, how is a programmer 
supposed to convey information to the outside world?  Do I just 
suck it up?


I looked at D code projects using this function on git hub, but 
stuff there was either either like this or too complex.





Re: New to D

2016-10-27 Thread Era Scarecrow via Digitalmars-d-learn
On Thursday, 27 October 2016 at 13:43:26 UTC, Steven 
Schveighoffer wrote:
It depends on the size of the file and the expectation of 
duplicate words. I'm assuming the number of words is limited, 
so you are going to allocate far less data by duping on demand. 
In addition, you may incur penalties for accessing the string 
directly from the file -- the OS may have swapped out that page 
and have to re-read it from the file itself.


You could also read the entire file into a string and go based 
on that.


 Depends. I recall experimenting early on with Memory mapped 
files (80Mb-300Mb), and it instantly loaded, no time at all. I 
don't think it even read the file or portions until I made 
requests to it (Course in my instance I'd have to create virtual 
records to access everything, still need to re-write and finish 
that project).


 It really depends on the circumstances though, and I suppose 
also understanding when a buffer is shared/reused and when to use 
dup.


Re: Map type to class instance at compile-time

2016-10-27 Thread Ali Çehreli via Digitalmars-d-learn


I admit that I can't come up with a solution right now. I hope others 
can see a way out of what I describe below. :-/


On 10/27/2016 11:33 AM, pontius wrote:
> On Wednesday, 26 October 2016 at 18:19:33 UTC, Ali Çehreli wrote:
>> There are different approaches but I think the solution above achieves
>> what you want:
>>
>> DefaultManager!(A) is managing an object of A
>> DefaultManager!(B) is managing an object of B
>> SomeCustomManager is managing an object of C
>>
>> Ali
>
> Thanks, your solution does work! I am still not familiar with mixins and
> haven't though about using them.
> I refactored it so that it accepts a manager instance:
>
> template ManagerRegistrationFor(T, alias mgr) {
> typeof(mgr) thisComponentMgr;
>
> static this() {
> thisComponentMgr = mgr;
> }
>
> void manage(T obj) {
> thisComponentMgr.manage(obj);
> }
> }
>
> struct GlobalManager {
> void process(T)(T t) {
> manage(t);

The problem is with that line. In the previous design, 
ManagerRegistrationFor would generate a manage() template instance for T 
and mix it in to the scope. As a result manage(t) would be bound to it 
the correct template instance.


Now, because manager.d does not have that instance here, it needs to 
refer to it with the full template name:


ManagerRegistrationFor!(T, ???).manage(t);

would work but we don't have the 'alias mgr' argument to refer to it 
(hence my question marks). I don't think it's ever possible to 
fully-qualify a template mixin that has an alias argument. (Yes, it 
would be possible if the user also had access to the same aliased symbol.)


So, I think the problem is with the new design; we need to get rid of 
that alias parameter and pass the manager object as a runtime parameter.


Ali



Re: MongoDB/DB <-> D

2016-10-27 Thread Daniel Kozak via Digitalmars-d-learn

Dne 27.10.2016 v 20:54 Jot via Digitalmars-d-learn napsal(a):


Using Vibe.D here;

How can one work with the DB's abstractly without incurring duplicity. 
I'd like to be able to create one struct and use that, either in D or 
the DB. I'd prefer to create a struct in D and interact through that 
struct with the database abstracted away.


struct Person
{
string name;
...
}

Person Joes = Person.Query!name("^Joe.*")

Or whatever is an appropriate example.



What Iwant to avoid is having to duplicate the database structures in 
their native language(json for mongodb).


Or, maybe there is a D struct to json convertor?

What are you describe here is called ORM (Object relational mapping) in 
this case it is much more like SRM (Struct relational mapping). AFAIK 
there is no ORM for mongodb, and it does not make much sense because 
mongo is not relational database. But maybe one of these libraries could 
be useful http://code.dlang.org/packages/asdf

http://code.dlang.org/packages/jsonizer


MongoDB/DB <-> D

2016-10-27 Thread Jot via Digitalmars-d-learn

Using Vibe.D here;

How can one work with the DB's abstractly without incurring 
duplicity. I'd like to be able to create one struct and use that, 
either in D or the DB. I'd prefer to create a struct in D and 
interact through that struct with the database abstracted away.


struct Person
{
string name;
...
}

Person Joes = Person.Query!name("^Joe.*")

Or whatever is an appropriate example.



What Iwant to avoid is having to duplicate the database 
structures in their native language(json for mongodb).


Or, maybe there is a D struct to json convertor?



Re: What is the difference between pages under dlang.org/phobos and dlang.org/library?

2016-10-27 Thread A D dev via Digitalmars-d-learn

On Thursday, 27 October 2016 at 18:47:07 UTC, Adam D. Ruppe wrote:

It is the same content, generated at the same time, just laid 
out differently.


You can use whichever you find easier.


Thanks, Adam.



Re: What is the difference between pages under dlang.org/phobos and dlang.org/library?

2016-10-27 Thread Adam D. Ruppe via Digitalmars-d-learn

On Thursday, 27 October 2016 at 18:44:00 UTC, A D dev wrote:
I viewed the above two pages and the content seems roughly the 
same.


It is the same content, generated at the same time, just laid out 
differently.


You can use whichever you find easier.


What is the difference between pages under dlang.org/phobos and dlang.org/library?

2016-10-27 Thread A D dev via Digitalmars-d-learn

Hi list,

What is the difference between pages under dlang.org/phobos and 
dlang.org/library?

And when should we use which, as a reference?

E.g.

https://dlang.org/phobos/std_zip.html

https://dlang.org/library/std/zip.html

I viewed the above two pages and the content seems roughly the 
same. How to know which one is more up to date and should be 
used? Same question for all other such similar pages on the two 
sites. Is there any thumb rule for this?


Thanks.



Re: Map type to class instance at compile-time

2016-10-27 Thread pontius via Digitalmars-d-learn

On Wednesday, 26 October 2016 at 18:19:33 UTC, Ali Çehreli wrote:
There are different approaches but I think the solution above 
achieves what you want:


DefaultManager!(A) is managing an object of A
DefaultManager!(B) is managing an object of B
SomeCustomManager is managing an object of C

Ali


Thanks, your solution does work! I am still not familiar with 
mixins and haven't though about using them.

I refactored it so that it accepts a manager instance:

template ManagerRegistrationFor(T, alias mgr) {
typeof(mgr) thisComponentMgr;

static this() {
thisComponentMgr = mgr;
}

void manage(T obj) {
thisComponentMgr.manage(obj);
}
}

struct GlobalManager {
void process(T)(T t) {
manage(t);
}
}

GlobalManager globalManager;

mixin ManagerRegistrationFor!(A, new ManagerA());
const ManagerB mgrB = new const ManagerB();
mixin ManagerRegistrationFor!(B, mgrB);
mixin ManagerRegistrationFor!(C, new ManagerC());


I have an issue with incapsulating this code in another module, 
though. I should have probably clarified that Managers can be 
defined both inside my library and in separate modules by my 
library users. If I move template ManagerRegistrationFor and 
struct GlobalManager to e.g. manager.d and attempt to define 
ManagerA,B,C and call ManagerRegistrationFor(...) in main.d, it 
doesn't compile:


1) Error: template instance manager.GlobalManager.process!(A) 
error instantiating
This happens 3 times in main.d at globalManager.process(a); and 
the other to .process calls.
2) Error: undefined identifier 'manage', did you mean module 
'manager'?
This error points to the body of GlobalManager.process in 
manager.d (it also occurs 3 times).


What would be the right way to incapsulate this mixin and 
GlobalManager to a separate module?


Re: Address of an array

2016-10-27 Thread David via Digitalmars-d-learn

A dynamic array looks something kind of like this:

struct DynamicArray(T)
{
size_t length;
T* ptr;
}

So, if you take the address of a dynamic array, you're 
basically taking the address of a struct on the stack, whereas 
the address in ptr is the address in memory where the data is 
(be it GC-allocated memory, malloc-ed memory, or a static array 
on the stack somewhere).


Similarly, if you have a class reference, and you take its 
address, you're taking the address of the reference, not the 
class object that it points to. e.g.


class MyClass
{
string foo;
}

MyClass mc;
auto addr = 

addr is the address of mc on the stack, whereas mc itself is 
null.


- Jonathan M Davis


Many thanks for your speedy and clear answer Jonathan! That makes 
even sense to me :-)




Re: Address of an array

2016-10-27 Thread Jonathan M Davis via Digitalmars-d-learn
On Thursday, October 27, 2016 16:13:34 David via Digitalmars-d-learn wrote:
> Hi
>
> The pointer (.ptr) of an array is the address of the first array
> element. But what exactly is the address of the array? And how is
> it used?
>
>auto myArray = [5, 10, 15, 20, 25, 30, 35];
>writeln("myArray.ptr: ", myArray.ptr); // myArray.ptr:
> 7FFA95F29000
>writeln("[0]: ", [0]); // [0]:
> 7FFA95F29000
>writeln(": ", ); // : 7FFE4B576B10
>writeln("*: ", *); // *: [5, 10, 15,
> 20, 25, 30, 35]

A dynamic array looks something kind of like this:

struct DynamicArray(T)
{
size_t length;
T* ptr;
}

So, if you take the address of a dynamic array, you're basically taking the
address of a struct on the stack, whereas the address in ptr is the address
in memory where the data is (be it GC-allocated memory, malloc-ed memory, or
a static array on the stack somewhere).

Similarly, if you have a class reference, and you take its address, you're
taking the address of the reference, not the class object that it points to.
e.g.

class MyClass
{
string foo;
}

MyClass mc;
auto addr = 

addr is the address of mc on the stack, whereas mc itself is null.

- Jonathan M Davis



Re: Address of an array

2016-10-27 Thread David via Digitalmars-d-learn

On Thursday, 27 October 2016 at 16:13:34 UTC, David wrote:

Hi

The pointer (.ptr) of an array is the address of the first 
array element. But what exactly is the address of the array? 
And how is it used?


  auto myArray = [5, 10, 15, 20, 25, 30, 35];
  writeln("myArray.ptr: ", myArray.ptr); // myArray.ptr: 
7FFA95F29000
  writeln("[0]: ", [0]); // [0]: 
7FFA95F29000

  writeln(": ", ); // : 7FFE4B576B10
  writeln("*: ", *); // *: [5, 10, 15, 
20, 25, 30, 35]


Sorry that went to quickly ;-)

I observe for example that the even if the pointer is moved to 
another address the address of the (dynamic) array stays constant:


  auto shrink = myArray[0 .. $-1];
  writeln("shrink.ptr: ", shrink.ptr);
  writeln(": ", );

Note: myArray and shrink have the same ptr but a different address

  shrink ~= 100;
  writeln("shrink.ptr: ", shrink.ptr);
  writeln(": ", );

Note: After appending, shrink changes its ptr but its address 
stays the same.


Thanks for your help in advance.



Re: test if the alias of a template is a literal

2016-10-27 Thread TheFlyingFiddle via Digitalmars-d-learn
On Thursday, 27 October 2016 at 14:45:22 UTC, Gianni Pisetta 
wrote:
On Thursday, 27 October 2016 at 14:34:38 UTC, TheFlyingFiddle 
wrote:
On Thursday, 27 October 2016 at 14:04:23 UTC, Gianni Pisetta 
wrote:

Hi all,
but at the moment isStringLiteral will return true even with 
variables of type string. So i searched for a metod to check 
if an alias is a literal value, but found nothing. Anyone 
have any clue on how can be done?


Thanks,
Gianni Pisetta


Not really understanding your problem. Could you include an 
example use that is problematic?


Yea, sorry I missed that.
A really stupid example would be

string var;

alias Sequence = Optimize!( "The", " ", "value", " ", "of", " 
", "var is ", var );


static assert( is( Sequence == AliasSeq!( "The value of var is 
", var ) ) );


writeln( Sequence );

given that you include the code snippet in the first post.

Thanks, Gianni


I think this fixes the problem:

template isStringLiteral(T...) if (T.length == 1) {
static if(is( typeof(T[0]) == string ))
{
enum bool isStringLiteral = !__traits(compiles, [0]);
}
else
{
enum bool isStringLiteral = false;
}
}

Literals do not have an address but variables do.

However note that:
string var;
static assert(!is(AliasSeq!(var) == AliasSeq!(var)));

It still works at run-time though:
string var = " hello ";
alias Seq = Optimize!("This", " is", " a", " variable! ", var);
//pragma(msg, Seq) //Fails to compile at var
//static assert(is(Seq == AliasSeq!("This is a variable!", 
var))); //Also fails

writeln(Seq); //Still works



Re: test if the alias of a template is a literal

2016-10-27 Thread Gianni Pisetta via Digitalmars-d-learn
On Thursday, 27 October 2016 at 14:34:38 UTC, TheFlyingFiddle 
wrote:
On Thursday, 27 October 2016 at 14:04:23 UTC, Gianni Pisetta 
wrote:

Hi all,
but at the moment isStringLiteral will return true even with 
variables of type string. So i searched for a metod to check 
if an alias is a literal value, but found nothing. Anyone have 
any clue on how can be done?


Thanks,
Gianni Pisetta


Not really understanding your problem. Could you include an 
example use that is problematic?


Yea, sorry I missed that.
A really stupid example would be

string var;

alias Sequence = Optimize!( "The", " ", "value", " ", "of", " ", 
"var is ", var );


static assert( is( Sequence == AliasSeq!( "The value of var is ", 
var ) ) );


writeln( Sequence );

given that you include the code snippet in the first post.

Thanks, Gianni


test if the alias of a template is a literal

2016-10-27 Thread Gianni Pisetta via Digitalmars-d-learn

Hi all,
I have an AliasSeq composed of literal strings, variables and 
delegates. I want to build a template Optimize that will return 
an AliasSeq that have all adjacent literals concatenated into 
one. So i writed something like this:


template isStringLiteral(alias V) {
enum bool isStringLiteral = ( is( typeof( V ) == string ) );
}

template Optimize(Os...) {
static if ( Os.length < 2 )
alias Optimize = Os;
else {
alias Optimized = Optimize!(Os[1..$]);

static if ( isStringLiteral!(Os[0]) && 
isStringLiteral!(Optimized[0]) ) {

enum string First = Os[0] ~ Optimized[0];
alias Rest = Optimized[1..$];
}
else {
alias First = AliasSeq!(Os[0]);
alias Rest = Optimized;
}
alias Optimize = AliasSeq!(First, Rest);
}
}

but at the moment isStringLiteral will return true even with 
variables of type string. So i searched for a metod to check if 
an alias is a literal value, but found nothing. Anyone have any 
clue on how can be done?


Thanks,
Gianni Pisetta


Re: New to D

2016-10-27 Thread Steven Schveighoffer via Digitalmars-d-learn

On 10/27/16 2:40 AM, Era Scarecrow wrote:

On Tuesday, 25 October 2016 at 14:40:17 UTC, Steven Schveighoffer wrote:

I will note, that in addition to the other comments, this is going to
result in corruption. Simply put, the buffer that 'line' uses is
reused for each line. So the string data used inside the associative
array is going to change. This will result in not finding words
already added when using the 'word in dictionary' check.

You need to use dictionary[word.idup] = newId; This will duplicate the
line into a GC string that will live as long as the AA uses it.


 If there's a case where you have immutable data AND can reference it
(say... mmap files?) then referencing the string would work rather than
having to duplicate it.


It depends on the size of the file and the expectation of duplicate 
words. I'm assuming the number of words is limited, so you are going to 
allocate far less data by duping on demand. In addition, you may incur 
penalties for accessing the string directly from the file -- the OS may 
have swapped out that page and have to re-read it from the file itself.


You could also read the entire file into a string and go based on that.

-Steve


Re: No trace of cumulativeFold except in the source.

2016-10-27 Thread Jonathan M Davis via Digitalmars-d-learn
On Sunday, October 23, 2016 19:20:43 e-y-e via Digitalmars-d-learn wrote:
> On this topic, do you think a 'cumulativeSum' specialisation
> based on the 'sum' specialisation be welcome in phobos? Here's a
> quick prototype, obviously not library standard but the basic
> logic is there:
> https://gist.github.com/anonymous/4fb79b4aba79b59348273288993740cb

If you think that it would be generally useful and worth having in Phobos,
by all means, get it up to snuff for inclusion in Phobos and create a PR for
it.

- Jonathan M Davis



Re: Avoiding GC

2016-10-27 Thread Guillaume Piolat via Digitalmars-d-learn

On Wednesday, 26 October 2016 at 08:18:07 UTC, hardreset wrote:
Is there a page somewhere on how to program D without using the 
GC?


The information is scattered.



How do I allocate / free structs / classes on the heap manually?


Classes => 
https://github.com/AuburnSounds/dplug/blob/master/core/dplug/core/nogc.d#L122





New would be GCed memeory wouldnt it?


Yes.




Delete is being depreciated?


I don't think you ever want delete when there is .destroy