getChar() vs. getChar!true()

2013-03-20 Thread Peter Sommerfeld

In std.json is a function getchar() with this signature:

dchar getChar(bool SkipWhitespace = false);

But it is called differently:

getChar(); // obviously SkipWhitespace = true;

getChar!true(); // probably getchar(true)

What is the reason for the different notation ?

Peter


Re: getChar() vs. getChar!true()

2013-03-20 Thread Peter Sommerfeld

cal wrote:


On Wednesday, 20 March 2013 at 20:11:47 UTC, Peter Sommerfeld wrote:

In std.json is a function getchar() with this signature:

dchar getChar(bool SkipWhitespace = false);


It looks like the signature (line 115) is:
dchar getChar(bool SkipWhitespace = false)()

Note the extra set of parens at the end, making the bool a template  
parameter.


Thanks, I must look more carefully...

Peter


Re: getChar() vs. getChar!true()

2013-03-20 Thread Peter Sommerfeld

Ali Çehreli:
Aside: bool parameters (regular or template) hurt readability. It would  
be better to have defined a type similar to std.stdio.KeepTerminator and  
its use with byLine():


   http://dlang.org/phobos/std_stdio.html#.File.byLine


I see. On the other hand: For an internal function like getChar()()
the implementation seem so be a bit to intricate. But for a public
API certainly preferable.

Peter


How to catenate a string multiple times ?

2013-03-16 Thread Peter Sommerfeld

Cannot find a reference: What is the best way
to catenate a string multiple times ?
Unfortunately this this does not work ;-)

string tab = ..;
tab = tab * 4; // - 

Peter


Variant confusion

2013-03-11 Thread Peter Sommerfeld

A confusing example:

import std.stdio, std.variant, std.conv;

alias Value = Variant;
alias List = Value[];   
alias Map = List[Value];

void main(string[] args){
Value value = abc;
Map  map = [value:[]];
Value key = value; // save value

// expect value to become of type Map
value = map;

if(typeid(value) == typeid(Map))
writeln(value == map);
else if(typeid(value) == typeid(Value))
writeln(value == Value); // Is still Value!

// add some values to map, does not work with value.
map[key] ~= to!Value(133);
map[key] ~= to!Value(abc);
map[key] ~= to!Value(1.23456);

// Voila! Both show the same value!!!

writeln(value,  == , map);  
}

What is going on here? If value shows the same value as
map it should be one. But is not a Map, still a Value.
Or do i miss here something importand ?

Peter


Re: Variant confusion

2013-03-11 Thread Peter Sommerfeld

Jesse Phillips wrote:


On Monday, 11 March 2013 at 14:23:41 UTC, Peter Sommerfeld wrote:

A confusing example:

import std.stdio, std.variant, std.conv;

alias Value = Variant;
alias List = Value[];   
alias Map = List[Value];


Oh, the new alias syntax is in?


Hmmm, I'm new to D, why should I use the old one? And I like
the new one more too, it is more descriptive IMHO.

The problem you are seeing is that Variant can hold anything, that  
incudes a map. But since Variant is just a struct it is a different type  
from struct[struct] which is the type for map.


Seems I have to dig into the implementation for a workaround  or use
my own union to integrate the types.

Thanks, Peter


Re: Variant confusion

2013-03-11 Thread Peter Sommerfeld

cal callumena...@gmail.com wrote:


To get the held type of a Variant, use .type():

if(value.type() == typeid(Map))
writeln(value == map);
else if(value.type() == typeid(Value))
writeln(value == Value); // Is still Value!

prints map.


Thanks cal, that will help! Seems I'm currently (?)
a bit blind...

Peter


Workaround for foreward declaration ?

2013-03-03 Thread Peter Sommerfeld

According to  http://dlang.org/ctod.html foreward declarations
are not needed because functions can be defined in any order.
But that seems not to be true for inner functions. A somewhat
artificial example:
-
import std.stdio;

void main(string[] args){
int count;

// void foo() -- unlike in C in D not possible

void bar(){
++count;
writeln(bar);

foo(); // ERROR: undefined identifier foo
}

void foo(){
writeln(foo);   
if(count){
 writeln(foo again);
 return;
}

bar();
}

foo();
}
---

Is there a workaround for such a situation or do I have
to put everything outside the enclosing function.

Peter


Re: Workaround for foreward declaration ?

2013-03-03 Thread Peter Sommerfeld

Jonathan M Davis wrote:
AFAIK, you have to put them outside for this. The order of both function  
calls and import declarations matters inside of a function even though  
it doesn't matter outside. In general, nested functions are more limited 
(e.g. if   they're templated, you can't instantiate them with different

arguments - you  only get one instantation).


Thanks David! Should be mentioned somewhere though. Well, probably is,
somewhere ...

Peter


eating whitespace

2013-02-09 Thread Peter Sommerfeld

I wonder what the best, that means the fastest  way is
to remove all whitespace  \t\n\r from as string.
Whitespace are distributed erratically within the string.

Peter


Re: eating whitespace

2013-02-09 Thread Peter Sommerfeld
Am 09.02.2013, 22:30 Uhr, schrieb Andrej Mitrovic  
andrej.mitrov...@gmail.com:



 f\too\t\n\rb\nar.removechars(\t\n\r)


thanks, works...

Peter


Re: std.process.system and friends

2013-02-07 Thread Peter Sommerfeld

notna wrote:

This works for me on Win7 with DMD2.061

http://dpaste.dzfl.pl/64529e76


Yes that works, but that was not quite the question.
The point is you cannot *set* the path variable.
But meanwhile I think it isn't a good idea anyway.
The PATH belongs to the user/system, not to programs.
Otherwise that may introduce some harm...


environment(PATH] = pathSeparator ~ path ~ pathToMyDir ;
// failed, unidentified identifier


Yep, typo!

Peter


Re: std.process.system and friends

2013-02-06 Thread Peter Sommerfeld

HeiHon heiko.honr...@web.de schrieb:


On Sunday, 3 February 2013 at 19:05:01 UTC, Peter Sommerfeld wrote:
Do you know a way to set it via system/shell calls, notably under  
windows?


Peter

You might want to use Tango:

module env;
// 2013-01-08
// dmd2.061 + Siegelord Tango-D2-d2port



import tango.sys.Environment;


Hmmm, AFAIK it is outdated, isn't it ? I also hesitate to introduce
major dependencies for this small point.

Can you point me to the sources of this Tango version please.
May be I can reuse a small part of it.

Thanks, Peter


Re: std.process.system and friends

2013-02-06 Thread Peter Sommerfeld

Am 06.02.2013, 14:51 Uhr, schrieb HeiHon heiko.honr...@web.de:

https://github.com/SiegeLord/Tango-D2

There are still some very useful things in Tango that you don't find in  
Phobos (e.g. logging) and it plays nicely together with Phobos.


Thanks, interesting read. Will have a deeper look in it later.

Peter


Re: Good tutorials

2013-01-14 Thread Peter Sommerfeld

SaltySugar wrote:


Where i can find some good tutorials and books? :)


http://ddili.org/ders/d.en/

http://www.amazon.com/exec/obidos/ASIN/0321635361/classicempire

Peter


Unexpected behavior of std.json.toJSON

2013-01-07 Thread Peter Sommerfeld

Sample code:
---
import std.stdio;
import std.json;
void main(string[] args){

  string text = { \url\:\http://www.boost.org\; };
  JSONValue json = parseJSON(text);

  writeln(toJSON(json));
  // prints: { url:http\/\/www.boost.org\ }
  // The same happens when writing to a file.
}


The double slash // in the url is replaced by \/\/.
Is that a feature or a bug? If the former, what is the
reason for this behavior and how to avoid it?

Peter


Customizing ddoc from cmd-line

2013-01-05 Thread Peter Sommerfeld


I seem to be unable to customize DDoc from the
command line.

Suppose the sources are in src, docs in doc.
Here is my command line (Win7 if that matters):

dmd -D -Dddoc doc/my.ddoc src/main.d

Nothing happens...

What I would like to do is to add a style sheet
as described in http://dlang.org/ddoc.html.
Isn't it not possible from the cmdline or do
I something wrong here ? How to make it working ?

Peter


Re: Customizing ddoc from cmd-line

2013-01-05 Thread Peter Sommerfeld

Am 05.01.2013, 20:23 Uhr, schrieb Peter Sommerfeld nore...@rubrica.at:

Nothing happens...


Sorry, I was imprecise. Of course, the executable as
well as the html was generated but my.ddoc had no
effect.

Peter


Re: Compiling shared example.

2012-10-28 Thread Peter Sommerfeld

Am 28.10.2012, 08:06 Uhr, schrieb Ali Çehreli acehr...@yahoo.com:


On 10/26/2012 02:22 PM, Peter Sommerfeld wrote:

To learn about shared attribute I've copied nearly verbatim an
example from Andreis book. The code:

import core.atomic;

struct Data{
int value;
}

shared struct SharedStack(T) {

private shared struct Node{
T data;
Node* next;
this(T value){data = value;};
}
private Node* root;

// push

void push(T value){
auto n = new Node(value);
shared(Node)* oldRoot;
do {
oldRoot = root;
n.next = oldRoot;
} while(!cas(root,oldRoot,n)); // line 30
}
// ...
}

SharedStack!(Data) q;

void main(string[] args){

Data m;
q.push(m); // line 40
}

I got the following error (dmd 2.060 win):

(40) Error function main.SharedStack!(Data).SharedStack.push(Data value)
shared is not callable using argument types (Data)

(30) template core.atomic.cas does not match any function template
declaration

What is wrong here ?

Peter


The following two changes are the workaround at least for compilation:

 auto n = cast(shared)new Node(value);
// ...

shared SharedStack!(Data) q;

Ali


Thanks Ali, that keeps me going for now. But I wonder why I
have do declare the variables shared if the data are declared
to be shared. Is that a shortcoming of the current compiler ?
Anyway, I can continue ...

BTW: Thanks for your book! It is of great help for beginners
like me!

Peter


Compiling shared example.

2012-10-26 Thread Peter Sommerfeld

To learn about shared attribute I've copied nearly verbatim an
example from Andreis book. The code:

import core.atomic;

struct Data{
  int value;
}

shared struct SharedStack(T) {

  private shared struct Node{
T data;
Node* next;
this(T value){data = value;};
  }
  private Node* root;

  // push

  void push(T value){
auto n = new Node(value);
shared(Node)* oldRoot;
do {
  oldRoot = root;
  n.next = oldRoot;
} while(!cas(root,oldRoot,n)); // line 30
  }
  // ...
}

SharedStack!(Data) q;

void main(string[] args){

  Data m;
  q.push(m); // line 40
}

I got the following error (dmd 2.060 win):

(40) Error function main.SharedStack!(Data).SharedStack.push(Data value)
shared is not callable using argument types (Data)

(30) template core.atomic.cas does not match any function template  
declaration


What is wrong here ?

Peter


Re: Threading Question

2012-10-25 Thread Peter Sommerfeld

Sean Kelly s...@invisibleduck.org wrote:
[snip]
In D, the main thread will join all non-daemon threads before calling  
static dtors or performing any other cleanup.  Daemon threads will  
continue to run until the process exits.  So daemon threads are  
basically just C-style kernel threads.

[snip]
The GC in D uses SIGUSR1 and SIGUSR2 to coordinate collections on Posix  
OSes (except for OSX).  You're free to use any other signal just as you  
would in C.


Thanks for clarification Sean!

Peter


Threading Question

2012-10-23 Thread Peter Sommerfeld

Hi!

I'm new to D an not a native speaker so I may misunderstand
the following sentence of the thread documentation.

final @property void isDaemon(bool val);

Sets the daemon status for this thread. While the runtime
will wait for all normal threads to complete before tearing
down the process, daemon threads are effectively ignored and
thus will not prevent the process from terminating. In effect,
daemon threads will be terminated automatically by the OS when
the process exits.

That sounds to me as if the daemon will be finished when the
main-thread has finished. But in my understanding daemons will
survive the termination of the main-thread and be killed by
a signal (KILL, SIGTERM etc) or finish itself.

I think that is the case here too. Is that true ?

Another question: I cannot find a reference how D deals with
OS SIGNALS, especially about differences between the platform
(*nix; Windows, Mac). Can you explain or point me to the
documentation?

Peter


system vs. execvp ?

2012-09-22 Thread Peter Sommerfeld

 Hi!

This works as expected:

  string cmd = dmd src/xyz.d;
  int i = system(cmd);

But this not:

  string[] cmd;
  cmd ~= src/xyz.d;
  int i = execvp(dmd,cmd);

Of course, dmd is in PATH (Win7).

What is wrong here?

tia Peter


Re: system vs. execvp ?

2012-09-22 Thread Peter Sommerfeld

Jonathan M Davis wrote:

Peter Sommerfeld wrote:


This works as expected:

   string cmd = dmd src/xyz.d;
   int i = system(cmd);

But this not:

   string[] cmd;
   cmd ~= src/xyz.d;
   int i = execvp(dmd,cmd);

Of course, dmd is in PATH (Win7).

What is wrong here?


Please elaborate on what doesn't work as expected. We can'thelp you if   
you don't tell us what's wrong.


Well, system(cmd) compiles xyz.d and creates an executable.

execvp(cmd) does call dmd but that behaves as if no arguments
where given (Usage msg etc). No executables are created.

system should run your command in a new process and return,whereas   
execvp will run it and never return, because the

new process replaces your  current one.


I did not know that it does not return. Anyway, it should
compile the args IMHO but it does not.

Peter


Re: std.json: SkipWhitespace = false ?

2012-09-20 Thread Peter Sommerfeld

Sean Kelly wrote:

It sounds like what you really want is pretty printing from toJSON.   
It's really not too hard to add, and I'd like it too.  I think this will  
have to wait for the new std.json, whenever we get that though.


Agreed! Where can the version be seen which are on review ?

Peter


std.json: SkipWhitespace = false ?

2012-09-19 Thread Peter Sommerfeld

Hi Everyone!

I'm new to :D and have a small problem with std.json.

If I use parseJSON() or toJSON() all whitespaces are
removed. I would like it to have them preserved for
better readability for users.

In std.json.d various functions contain SkipWhitespace
as parameter but that is not propagated to the toplevel
functions.

Are there any strong reasons for this behavior or can
I rebuild it with SkipWhitespace = true? Or is there
another way to work around it.

Peter


Re: std.json: SkipWhitespace = false ?

2012-09-19 Thread Peter Sommerfeld

Jesse Phillips wrote:

Peter Sommerfeld wrote:

Hi Everyone!

I'm new to :D and have a small problem with std.json.

If I use parseJSON() or toJSON() all whitespaces are
removed. I would like it to have them preserved for
better readability for users.

In std.json.d various functions contain SkipWhitespace
as parameter but that is not propagated to the toplevel
functions.

Are there any strong reasons for this behavior or can
I rebuild it with SkipWhitespace = true? Or is there
another way to work around it.

Peter


Save a copy of it locally and make the changes needed. There is a new  
version of std.json which is waiting on changes to std.variant and  
review... Anyway not much will happen with the current version so this  
will be good anyway.


Thanks Jesse, will do so!

Peter