Re: Better native D 2D graphics library?

2015-02-09 Thread Namespace via Digitalmars-d-learn

On Monday, 9 February 2015 at 05:50:00 UTC, uri wrote:

On Saturday, 7 February 2015 at 23:29:01 UTC, Namespace wrote:

On Saturday, 7 February 2015 at 22:09:03 UTC, Gan wrote:

Is there a better D graphics library in the works?

I'm using SFML(which is very easy and has lots of features) 
but it seems to use a lot of ram(if you leave it running for 
a while on a graphic intensive scene) and trying to make it 
include the dependencies with the compiled executable is 
complicated.


Is there a D 2D graphics library that's just as easy, cross 
platform, doesn't use X11, allows drawing to off-screen 
buffers and drawing those to screen? (plus supports nice 
drawing of shapes, circles, rectangles, lines)


I'm probably asking too much- I doubt such a thing exists.


I once wrote such a library: https://github.com/Dgame/Dgame
But since I left D I don't maintain it. Maybe that will change 
in the next few weeks. But otherwise you are free to use or 
improve it.


I use Dgame for hobby projects and can definitely recommend it.


That's nice to hear! Thank you.


Re: Static method of inner class needs this

2015-02-09 Thread wobbles via Digitalmars-d-learn

On Monday, 9 February 2015 at 07:32:33 UTC, rumbu wrote:

class Outer
{
class Inner
{
static Inner createInner()
{
return new Inner(); //need 'this' to access member 
this

}
}
}

Is this a bug?

If Inner is not nested, it works as expected:

class Inner
{
static Inner createInner()
{
return new Inner()
}
}

D version: 2.066.1


In the first case, is there an Inner that is visible outside of 
Outer?
If so, the compiler wont know which one your talking about, so 
need to specify with this.


Re: Static method of inner class needs this

2015-02-09 Thread ketmar via Digitalmars-d-learn
On Mon, 09 Feb 2015 07:32:32 +, rumbu wrote:

 class Outer {
  class Inner {
  static Inner createInner()
  {
  return new Inner(); //need 'this' to access member
 this
  }
  }
 }
 
 Is this a bug?

strictly speaking, this is not a bug. compiler doesn't do deep analysis 
on nested structures/classes to determine if they really require context 
pointer. you can use `static class Inner` to tell the compiler that 
`Inner` doesn't require any context.

signature.asc
Description: PGP signature


Re: Classes and @disable this()

2015-02-09 Thread via Digitalmars-d-learn
On Sunday, 8 February 2015 at 19:57:28 UTC, Jonathan M Davis 
wrote:
On Sunday, February 08, 2015 17:51:09 bearophile via 
Digitalmars-d-learn wrote:

fra:

 However making it a compiler error would be far, far better

I think this can be filed in Bugzilla as diagnostic 
enhancement:



class Foo {
 @disable this();
 this(int i) {}
}
void main() {}


The compiler should probably just give you an error telling you 
that
disabling the default constructor on classes is illegal. And 
since no
default constructor is automatically declared if you declare 
another
constructor, there isn't even any point in disabling the 
default constructor
(which is probably why no one has been complaining about this). 
@disable

this() only makes sense on structs.


Alternatively, it could be accepted (and a no-op) if another 
constructor is defined, but an error if not.


Re: Template constructor in a non-template struct.

2015-02-09 Thread via Digitalmars-d-learn

On Sunday, 8 February 2015 at 22:19:35 UTC, ChrisG wrote:
Thanks bearophile. Your first suggestion about making the 
struct a template is something I considered. However, because 
of all the code I've already written that approach would force 
me to use inheritance or convert a ton of functions to 
templates. Ick.


The __ctor syntax looks like the answer to my question. If my 
struct is used by anyone else though, I'm not sure it would be 
OK to assume they would even be aware of that syntax. hmmm... 
Thinking I'll just make it a runtime parameter for now.




You could also hide the ugly __ctor() call behind a nicely named 
factory method.


Worker is not finished while sending message to intermediate worker

2015-02-09 Thread xtreak via Digitalmars-d-learn
I am using programming in D to learn about D language. I wrote 
a simple program that spawns a worker and sends it a number to 
receive its square as a string. The worker 1 gets the number 
squares it and sends to worker 2 (a different function) to get 
casted as string which is returned to the worker 1 and thus it 
returns it to the main function call. I can write the whole thing 
in a single thread. I wrote it to understand about workers 
better. I used receive to get the worker 1 act as per the input. 
The program is as follows


import std.stdio;
import std.concurrency;
import std.conv;
import core.thread;

void main() {

  foreach (int num; 1..100) {
auto square_tid = spawn(square);
square_tid.send(num);
auto square = receiveOnly!string();
writeln(square);
  }
}


void square() {
  static i = 0;
  receive (
   (int num) {
 auto square = num * num;
 writeln(sqaure : Comes in with  , num ,  for  , ++i 
,  time);

 auto stringWorker = spawn(stringConverter);
 stringWorker.send(thisTid, square, ownerTid);
   },
   (Tid tid, string str) {
 writeln(comes in string);
 send(tid, hello);
   });
}

void stringConverter() {
  static i = 0;
  auto params = receiveOnly!(Tid, int, Tid)();
  auto stringified = to!string(params[1]); // Stringify the square
  writeln(string : Comes in with  , params[1],  for  , ++i , 
 time);
  params[0].send(params[2], stringified); // params[0] - square 
function tid, // params[2] - main function tid

}


I got the answer from stackoverflow @ 
https://stackoverflow.com/questions/28128383/worker-is-not-finished-while-sending-message-to-intermediate-worker. 
But the person who answered my question asked me to post back to 
dlang learn to learn more about it. As I spawn the function and a 
send a message to stringConverter as it sends a message to the 
square function why do I need to embed another receive call 
inside the int case as indicated in the answer. How can I avoid 
embedding the receive call and why does the person in the second 
answer used while(1) to receive the message.


Re: parse string as char

2015-02-09 Thread Kagamin via Digitalmars-d-learn

https://github.com/Hackerpilot/libdparse/blob/master/src/std/d/lexer.d#L1491


Re: parse string as char

2015-02-09 Thread FG via Digitalmars-d-learn

Of course consuming it dchar by dchar also works:

string s = `\tabŁŃ\r\nx`;
assert(parseDchar(s) == '\t');
assert(parseDchar(s) == 'a');
assert(parseDchar(s) == 'b');
assert(parseDchar(s) == 'Ł');
assert(parseDchar(s) == 'Ń');
assert(parseDchar(s) == '\r');
assert(parseDchar(s) == '\n');
assert(parseDchar(s) == 'x');
assert(s.empty);


Re: parse string as char

2015-02-09 Thread FG via Digitalmars-d-learn

On 2015-02-09 at 03:40, Timothee Cour via Digitalmars-d-learn wrote:

Is there a simple way to parse a string as a char?
eg:
unittest{
   assert(parseChar(`a`)=='a');
   assert(parseChar(`\n`)=='\n'); //NOTE: I'm looking at `\n` not \n
   // should also work with other forms of characters, see 
http://dlang.org/lex.html
}
Note, std.conv.to http://std.conv.to doesn't work (`\n`.to!char does not work)



parseEscape does something similar to what you need:
https://github.com/D-Programming-Language/phobos/blob/master/std/conv.d#L3415

Unfortunately it is private and handles only escapes, so here is a modified 
version:



import std.range, std.stdio;

dchar parseDchar(Source)(ref Source s)
if (isInputRange!Source  isSomeChar!(ElementType!Source))
{
import std.utf, std.conv;
if (s.front != '\\') {
dchar c = decodeFront(s);
return c;
}
s.popFront;
if (s.empty)
throw new Exception(Unterminated escape sequence);

dchar getHexDigit()(ref Source s_ = s)  // workaround
{
import std.ascii : isAlpha, isHexDigit;
if (s_.empty)
throw new Exception(Unterminated escape sequence);
s_.popFront();
if (s_.empty)
throw new Exception(Unterminated escape sequence);
dchar c = s_.front;
if (!isHexDigit(c))
throw new Exception(Hex digit is missing);
return isAlpha(c) ? ((c  ~0x20) - ('A' - 10)) : c - '0';
}

dchar result;

switch (s.front)
{
case '':   result = '\';  break;
case '\'':  result = '\'';  break;
case '0':   result = '\0';  break;
case '?':   result = '\?';  break;
case '\\':  result = '\\';  break;
case 'a':   result = '\a';  break;
case 'b':   result = '\b';  break;
case 'f':   result = '\f';  break;
case 'n':   result = '\n';  break;
case 'r':   result = '\r';  break;
case 't':   result = '\t';  break;
case 'v':   result = '\v';  break;
case 'x':
result  = getHexDigit()  4;
result |= getHexDigit();
break;
case 'u':
result  = getHexDigit()  12;
result |= getHexDigit()  8;
result |= getHexDigit()  4;
result |= getHexDigit();
break;
case 'U':
result  = getHexDigit()  28;
result |= getHexDigit()  24;
result |= getHexDigit()  20;
result |= getHexDigit()  16;
result |= getHexDigit()  12;
result |= getHexDigit()  8;
result |= getHexDigit()  4;
result |= getHexDigit();
break;
default:
throw new Exception(Unknown escape character  ~ 
to!string(s.front));
}
if (s.empty)
throw new Exception(Unterminated escape sequence);

s.popFront();

return result;
}

dstring parseString(Source)(Source s)
if (isInputRange!Source  isSomeChar!(ElementType!Source))
{
import std.array;
dchar[] result;
auto app = appender(result);
while (!s.empty)
app.put(parseDchar(s));
return app.data;
}

unittest
{
assert(parseString(``) == d);
assert(parseString(`abc`) == abcd);
assert(parseString(`abc\\n\\n\\n`) == abc\\n\\n\\nd);
assert(parseString(`ąćę\\nłńó`) == ąćę\\nłńód);
assert(parseString(`abc\\\nx`) == abc\\\nxd);
assert(parseString(`\tabc\r\nx`) == \tabc\r\nxd);
assert(parseString(` \x20 `) ==d);
}






Re: How to write similar code D?

2015-02-09 Thread FG via Digitalmars-d-learn

On 2015-02-10 at 01:41, bearophile wrote:


 auto query = iota(2, 12)
  .map!(c = Tuple!(int,length, int,height, 
int,hypotenuse)
   (2 * c, c ^^ 2 - 1, c ^^ 2 + 1))
  .map!(x = %3d%4d%4d.format(x.height, x.hypotenuse, 
x.length));



I took advantage of the fact that all elements were of the same type:

auto query = iota(2, 2 + 10)
.map!(c = [Length: 2 * c, Height: c * c - 1, Hypotenuse: c * c + 1])
.map!(x = format(%4d%4d%4d, x[Height], x[Hypotenuse], x[Length]));
...

and was surprised that it worked straight away. :)
But definitely this looks better and less complicated:

auto query = iota(2, 12)
.map!(c = tuple(c ^^ 2 - 1, c ^^ 2 + 1, 2 * c));
foreach (x; query)
writefln(%4d%4d%4d, x[]);

It's the foreach version, since `each` isn't officially out yet.


Re: How to write similar code D?

2015-02-09 Thread Dennis Ritchie via Digitalmars-d-learn

On Tuesday, 10 February 2015 at 01:31:54 UTC, FG wrote:

On 2015-02-10 at 01:41, bearophile wrote:


auto query = iota(2, 12)
 .map!(c = Tuple!(int,length, int,height, 
int,hypotenuse)
  (2 * c, c ^^ 2 - 1, c ^^ 2 + 
1))
 .map!(x = %3d%4d%4d.format(x.height, 
x.hypotenuse, x.length));




I took advantage of the fact that all elements were of the same 
type:


auto query = iota(2, 2 + 10)
.map!(c = [Length: 2 * c, Height: c * c - 1, 
Hypotenuse: c * c + 1])
.map!(x = format(%4d%4d%4d, x[Height], 
x[Hypotenuse], x[Length]));

...

and was surprised that it worked straight away. :)
But definitely this looks better and less complicated:

auto query = iota(2, 12)
.map!(c = tuple(c ^^ 2 - 1, c ^^ 2 + 1, 2 * c));
foreach (x; query)
writefln(%4d%4d%4d, x[]);

It's the foreach version, since `each` isn't officially out yet.


Thank you.


Re: To write such an expressive code D

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

On 02/09/2015 08:17 PM, Dennis Ritchie wrote:

 Ali, and you can write it without using the function iota() and map?

No because the a..b syntax is not a D language construct that we can use 
anywhere that it makes sense. It only works as number ranges inside 
foreach loops, when indexing slices, and case value ranges.


Also no, because there is no equivalent of F#'s - syntax, we have to 
use map.


 %(%.15g\n%).writefln(iota(0, PI/2, PI/2/9).map!sin);

 I just need that code was only used features of the language without
 using library functions. You may only use the function sin().

I am waiting to see a language solution that does not use even sin(). :o)

Ali



Re: function and variable

2015-02-09 Thread Vlad Levenfeld via Digitalmars-d-learn
On Tuesday, 10 February 2015 at 03:59:22 UTC, Rikki Cattermole 
wrote:

That's a bug. It should be using the function pointer.

UFCS call should abide by the same scoping rules as anything 
else.

https://issues.dlang.org/show_bug.cgi?id=14161


I thought that's how UFCS was explicitly designed? Global symbols 
only, yes?


Re: To write such an expressive code D

2015-02-09 Thread Dennis Ritchie via Digitalmars-d-learn

On Monday, 9 February 2015 at 20:16:45 UTC, Ali Çehreli wrote:
Yes, but apparently D's default precision for output is less 
than F#'s so how about the following? :p


%(%.15g\n%).writefln(iota(0, PI/2, PI/2/9).map!sin);

Just for demonstration, I would not write anything like that 
but the following is fine because now the format becomes the 
second parameter: :)


iota(0, PI/2, PI/2/9).map!sin.writeF(%(%.15g\n%));

void writeF(R)(R range, string format)
{
return writefln(format, range);
}


Ali, and you can write it without using the function iota() and 
map?

%(%.15g\n%).writefln(iota(0, PI/2, PI/2/9).map!sin);

I just need that code was only used features of the language 
without using library functions. You may only use the function 
sin().


function and variable

2015-02-09 Thread Fyodor Ustinov via Digitalmars-d-learn

Hi!

I think this code should not be compiled without any warning:

import std.stdio;

void f(int a) {
writeln(it's a function! : , a);
}

void main() {
auto f = function (int a) {writeln(It's a variable! : , 
a);};

5.f();
f(5);
}

Output:

it's a function! : 5
It's a variable! : 5

WBR,
Fyodor.


Re: function and variable

2015-02-09 Thread Fyodor Ustinov via Digitalmars-d-learn
On Tuesday, 10 February 2015 at 03:59:22 UTC, Rikki Cattermole 
wrote:

That's a bug. It should be using the function pointer.

UFCS call should abide by the same scoping rules as anything 
else.

https://issues.dlang.org/show_bug.cgi?id=14161


Moreover. If the function is not defined (only pointer), I get 
this error:


aa.d(5): Error: no property 'f' for type 'int'

when try use 5.f();


Re: Intended to be able to RefCount an interface?

2015-02-09 Thread weaselcat via Digitalmars-d-learn

On Tuesday, 10 February 2015 at 05:09:00 UTC, Jakob Ovrum wrote:

On Tuesday, 10 February 2015 at 04:44:55 UTC, weaselcat wrote:

Thread title.


interface Itest{

}

class testclass : Itest{
}

void main()
{
   import std.typecons;
   auto test = RefCounted!Itest(new testclass());
}

If you change the refcounted to type testclass it doesn't 
compile because refcounted doesn't support classes.


Is this a bug, or intended?


I think this is a bug, and that the current RefCounted needs to 
reject interfaces.


I assumed as much, thanks.

AFAIK, it is intended that RefCounted!SomeClass is supposed to 
reference-count the actual class instance, not its reference, 
which is what RefCounted!SomeInterface currently does.


Is there currently an enhancement request open for this on the 
bug tracker? I cannot find anything.


Re: To write such an expressive code D

2015-02-09 Thread Dennis Ritchie via Digitalmars-d-learn

On Tuesday, 10 February 2015 at 06:17:17 UTC, Ali Çehreli wrote:

On 02/09/2015 08:17 PM, Dennis Ritchie wrote:

 Ali, and you can write it without using the function iota()
and map?

No because the a..b syntax is not a D language construct that 
we can use anywhere that it makes sense. It only works as 
number ranges inside foreach loops, when indexing slices, and 
case value ranges.


Also no, because there is no equivalent of F#'s - syntax, we 
have to use map.


 %(%.15g\n%).writefln(iota(0, PI/2, PI/2/9).map!sin);

 I just need that code was only used features of the language
without
 using library functions. You may only use the function sin().

I am waiting to see a language solution that does not use even 
sin(). :o)


Ali

Thank you.


Re: Classes and @disable this()

2015-02-09 Thread Steven Schveighoffer via Digitalmars-d-learn

On 2/8/15 2:57 PM, Jonathan M Davis via Digitalmars-d-learn wrote:

On Sunday, February 08, 2015 17:51:09 bearophile via Digitalmars-d-learn wrote:

fra:


However making it a compiler error would be far, far better


I think this can be filed in Bugzilla as diagnostic enhancement:


class Foo {
  @disable this();
  this(int i) {}
}
void main() {}


The compiler should probably just give you an error telling you that
disabling the default constructor on classes is illegal. And since no
default constructor is automatically declared if you declare another
constructor, there isn't even any point in disabling the default constructor
(which is probably why no one has been complaining about this). @disable
this() only makes sense on structs.


Why? I think it's perfectly acceptable.

What should be illegal is if you extend Foo and don't @disable this on 
the derivative.


-Steve



Re: Static method of inner class needs this

2015-02-09 Thread Steven Schveighoffer via Digitalmars-d-learn

On 2/9/15 4:30 AM, ketmar wrote:

On Mon, 09 Feb 2015 07:32:32 +, rumbu wrote:


class Outer {
  class Inner {
  static Inner createInner()
  {
  return new Inner(); //need 'this' to access member
this
  }
  }
}

Is this a bug?


strictly speaking, this is not a bug. compiler doesn't do deep analysis
on nested structures/classes to determine if they really require context
pointer. you can use `static class Inner` to tell the compiler that
`Inner` doesn't require any context.



To expand on this, nested classes (that is, a class nested inside 
another class) REQUIRE a pointer to the outer class instance (accessed 
via hidden member outer). The reason for this is Java portability. 
Seriously :)


-Steve


Re: To write such an expressive code D

2015-02-09 Thread Dennis Ritchie via Digitalmars-d-learn
On Monday, 9 February 2015 at 20:03:00 UTC, Vladimir Panteleev 
wrote:

On Monday, 9 February 2015 at 19:57:23 UTC, Ali Çehreli wrote:

   writefln(%(%.15g\n%), sins);


In 2.067, you can write:

iota(0, PI/2, PI/2/9).map!sin.each!writeln;


March 1!


Re: Worker is not finished while sending message to intermediate worker

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

On 02/09/2015 11:46 AM, Ali Çehreli wrote:

 threads normally start one [or] more worker threads and
 send tasks to those threads

Accordingly, the following program uses just three threads:

import std.stdio;
import std.concurrency;
import std.conv;
import core.thread;

struct Terminate
{}

void main() {

auto square_tid = spawn(square);

foreach (int num; 1..100) {
square_tid.send(num);
auto square = receiveOnly!string();
writeln(square);
}

square_tid.send(Terminate());
}

void square() {
auto i = 0;
bool done = false;
auto stringWorker = spawn(stringConverter, ownerTid);

while (!done) {
receive (
(Terminate message) {
stringWorker.send(message);
done = true;
},

(int num) {
auto square = num * num;
writeln(sqaure : Comes in with  ,
num ,  for  , ++i ,  time);
stringWorker.send(square);
});
}
}

void stringConverter(Tid destination) {
auto i = 0;
bool done = false;

while (!done) {
receive (
(Terminate message) {
done = true;
},

(int num) {
auto stringified = num.to!string;

writeln(string : Comes in with ,
num,  for  , ++i ,  time);
destination.send(stringified);
});
}
}

Ali



Binding C++ value types

2015-02-09 Thread Benjamin Thaut via Digitalmars-d-learn
When binding C++ value types you might want to use them by placing them 
on the D-Stack. This however seems to be not supported as the mangling 
for the constructor is completely wrong. Is this supposed to work?


Kind Regards
Benjamin Thaut


Re: To write such an expressive code D

2015-02-09 Thread Tobias Pankrath via Digitalmars-d-learn

On Monday, 9 February 2015 at 19:40:42 UTC, Dennis Ritchie wrote:

Good evening.
Is it possible to D something to replace the container on the 
F#, which displays the values of the sine from 0 to 90 degrees 
with an interval of 10 degrees:

let pi = Math.PI
let sins = [for x in 0.0..pi / 2.0 / 9.0..pi / 2.0 - sin x]
sins.Dump()

Output:
0
0,17364817766693
0,342020143325699
0,5
0,642787609686539
0,76603118978
0,866025403784439
0,939692620785908
0,984807753012208
1

P.S. Interested in code that will be as impressive as this. In 
General, I would like to see something akin to D.


iota(0, 91, 10).map!sin.writeln

or something like that.


Re: To write such an expressive code D

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

On 02/09/2015 11:45 AM, Tobias Pankrath wrote:


iota(0, 91, 10).map!sin.writeln

or something like that.


Yes: :)

import std.math;
import std.stdio;
import std.range;
import std.algorithm;

void main()
{
const beg = 0.0L;
const interval = PI_2 / 9;
const end = PI_2 + interval;

auto sins = iota(beg, end, interval).map!sin;
writefln(%(%.15g\n%), sins);
}

0
0.17364817766693
0.342020143325669
0.5
0.642787609686539
0.76603118978
0.866025403784439
0.939692620785908
0.984807753012208
1

Ali



Re: Classes and @disable this()

2015-02-09 Thread Jonathan M Davis via Digitalmars-d-learn
On Monday, February 09, 2015 13:29:22 Steven Schveighoffer via 
Digitalmars-d-learn wrote:
 On 2/8/15 2:57 PM, Jonathan M Davis via Digitalmars-d-learn wrote:
  On Sunday, February 08, 2015 17:51:09 bearophile via Digitalmars-d-learn 
  wrote:
  fra:
 
  However making it a compiler error would be far, far better
 
  I think this can be filed in Bugzilla as diagnostic enhancement:
 
 
  class Foo {
@disable this();
this(int i) {}
  }
  void main() {}
 
  The compiler should probably just give you an error telling you that
  disabling the default constructor on classes is illegal. And since no
  default constructor is automatically declared if you declare another
  constructor, there isn't even any point in disabling the default constructor
  (which is probably why no one has been complaining about this). @disable
  this() only makes sense on structs.

 Why? I think it's perfectly acceptable.

 What should be illegal is if you extend Foo and don't @disable this on
 the derivative.

Why would it we even allow it? What benefit is there? It's meaningless.
@disable this(); is for disabling the init property on structs. Classes
themselves have no init values - and their references have null as their
init value.

The default constructor already follows sensible rules where it's not
generated if another constructor is declared, and derived classes have to
call a base class constructor if the base class doesn't have a default
constructor.

- Jonathan M Davis



Re: How to write similar code D?

2015-02-09 Thread bearophile via Digitalmars-d-learn

Dennis Ritchie:


Tell me, please, how to write similar С# code D:


This is more or less exactly the same:

void main() {
import std.stdio, std.range, std.algorithm, std.typecons, 
std.format;


auto query = iota(2, 12)
 .map!(c = Tuple!(int,length, int,height, 
int,hypotenuse)

  (2 * c, c ^^ 2 - 1, c ^^ 2 + 1))
 .map!(x = %3d%4d%4d.format(x.height, 
x.hypotenuse, x.length));


foreach (immutable x; query)
x.writeln;
}


But often you write something more like this in D using the 
latest version of the compiler:



void main() {
import std.stdio, std.range, std.algorithm, std.typecons;

iota(2, 12)
.map!(c = tuple(c ^^ 2 - 1, c ^^ 2 + 1, 2 * c))
.each!(x = writefln(%3d%4d%4d, x[]));
}


Bye,
bearophile


Re: To write such an expressive code D

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

On 02/09/2015 12:05 PM, Dennis Ritchie wrote:

On Monday, 9 February 2015 at 20:03:00 UTC, Vladimir Panteleev wrote:

On Monday, 9 February 2015 at 19:57:23 UTC, Ali Çehreli wrote:

   writefln(%(%.15g\n%), sins);


In 2.067, you can write:

iota(0, PI/2, PI/2/9).map!sin.each!writeln;


March 1!


Yes, but apparently D's default precision for output is less than F#'s 
so how about the following? :p


%(%.15g\n%).writefln(iota(0, PI/2, PI/2/9).map!sin);

Just for demonstration, I would not write anything like that but the 
following is fine because now the format becomes the second parameter: :)


iota(0, PI/2, PI/2/9).map!sin.writeF(%(%.15g\n%));

void writeF(R)(R range, string format)
{
return writefln(format, range);
}

Ali



Re: Classes and @disable this()

2015-02-09 Thread Steven Schveighoffer via Digitalmars-d-learn

On 2/9/15 3:15 PM, Jonathan M Davis via Digitalmars-d-learn wrote:

On Monday, February 09, 2015 13:29:22 Steven Schveighoffer via 
Digitalmars-d-learn wrote:

On 2/8/15 2:57 PM, Jonathan M Davis via Digitalmars-d-learn wrote:

On Sunday, February 08, 2015 17:51:09 bearophile via Digitalmars-d-learn wrote:

fra:


However making it a compiler error would be far, far better


I think this can be filed in Bugzilla as diagnostic enhancement:


class Foo {
   @disable this();
   this(int i) {}
}
void main() {}


The compiler should probably just give you an error telling you that
disabling the default constructor on classes is illegal. And since no
default constructor is automatically declared if you declare another
constructor, there isn't even any point in disabling the default constructor
(which is probably why no one has been complaining about this). @disable
this() only makes sense on structs.


Why? I think it's perfectly acceptable.

What should be illegal is if you extend Foo and don't @disable this on
the derivative.


Why would it we even allow it? What benefit is there? It's meaningless.
@disable this(); is for disabling the init property on structs. Classes
themselves have no init values - and their references have null as their
init value.

The default constructor already follows sensible rules where it's not
generated if another constructor is declared, and derived classes have to
call a base class constructor if the base class doesn't have a default
constructor.


Well, if I do this:

class C {}

I can do this:

new C();

Mechanisms to disable this are kind of awkward. I can define this() as 
private, but that doesn't help for intra-module calls.


static class C doesn't work.

It really is only useful in the case where you don't want to define a 
constructor. Which probably means -- you don't want to use a class anyway ;)


But for completeness, it seems like I should be able to have the option 
of disabling something the compiler does by default. Even if it's next 
to useless.


-Steve


Re: Worker is not finished while sending message to intermediate worker

2015-02-09 Thread xtreak via Digitalmars-d-learn

On Monday, 9 February 2015 at 20:11:09 UTC, Ali Çehreli wrote:

On 02/09/2015 11:46 AM, Ali Çehreli wrote:

 threads normally start one [or] more worker threads and
 send tasks to those threads

Accordingly, the following program uses just three threads:

import std.stdio;
import std.concurrency;
import std.conv;
import core.thread;

struct Terminate
{}

void main() {

auto square_tid = spawn(square);

foreach (int num; 1..100) {
square_tid.send(num);
auto square = receiveOnly!string();
writeln(square);
}

square_tid.send(Terminate());
}

void square() {
auto i = 0;
bool done = false;
auto stringWorker = spawn(stringConverter, ownerTid);

while (!done) {
receive (
(Terminate message) {
stringWorker.send(message);
done = true;
},

(int num) {
auto square = num * num;
writeln(sqaure : Comes in with  ,
num ,  for  , ++i ,  time);
stringWorker.send(square);
});
}
}

void stringConverter(Tid destination) {
auto i = 0;
bool done = false;

while (!done) {
receive (
(Terminate message) {
done = true;
},

(int num) {
auto stringified = num.to!string;

writeln(string : Comes in with ,
num,  for  , ++i ,  time);
destination.send(stringified);
});
}
}

Ali


Hi Ali,

Thanks for your book :) I am using it for learning D. I want the 
stringConverter to send a message to square which in turn 
messages the main function with string hello. So are you saying 
that message is not delivered to square from stringWorker. How 
does embedding the receive call inside the int in stackoverflow 
answer receives the message? It seems silly but how can I keep my 
threads alive to receive messages? Thanks a lot again. Please 
correct me if I am wrong anywhere.


hasmap with tuple as key

2015-02-09 Thread Ondra via Digitalmars-d-learn

Is there any drawback of doing this: string[Tuple!(int, int)] a;

Especially performance one.

Thanks.


Re: Static method of inner class needs this

2015-02-09 Thread rumbu via Digitalmars-d-learn

On Monday, 9 February 2015 at 09:30:55 UTC, ketmar wrote:

... you can use `static class Inner` to tell the compiler
that
`Inner` doesn't require any context.


Thank you, static qualifier works. I thought in C# terms where a 
static class means anything else.


Re: Learning to XML with D

2015-02-09 Thread Derix via Digitalmars-d-learn
What I don't quite grab is the construct (in Element e) , 
especially the *in* part.


Function parameters in D can be qualified as in or out, 
optionally:



But of course. Actually I kinda found out just a little while 
after posting the question. Asking questions is a great way to 
figure out the answer, so thank you for reading mines ;-) Thank 
you for your answer too, which consolidates my guess and makes me 
think I still have some thinking to do about the life of a 
function parameter.



I was a bit puzzled too as to where the Element e comes from, 
how is it that it's already instanciated and all. Well, I've just 
found the relevant part of the documentation. To be honest, said 
documentation is not always easy to navigate or to decrypt. I 
sense some potential for progress here.


Re: Learning to XML with D

2015-02-09 Thread Derix via Digitalmars-d-learn

my dom.d works in a familiar way

OK, will check it



useful for scraping html sites.
Not exactly what I'm doing, but close. I'm in the midst of a 
self-training spree, and what I use as test-tubes fodder is the 
following : a collection of 300+ html files constituting an 
electronic version of a technical book. My intent is to generate 
a clickable table of contents, by parsing the files for css 
styles specific to section headers. The first leg of the journey 
was to normalize styles accross the bunch. That is done, more or 
less. I already have a proto-toc, but not entirely satisfying : 
lacks handles for propper styling, and the way I arrived there is 
kinda brutish.


One hurdle I haven't overcame yet is that the text content, and 
the section headers themsleves, contain some html tags (well, the 
book /is/ about html, among other things). For example, some 
section headers are rendered as two bold lines, with a fat br/ 
in the middle, and b/b around. So when I parse the payload of 
the p element, I end up with some lt;br/gt; in the middle of 
a sentence. Survivable, but unclean.


So yeah, I'll give it another try with your dom.d


Re: Better native D 2D graphics library?

2015-02-09 Thread ponce via Digitalmars-d-learn

On Saturday, 7 February 2015 at 22:09:03 UTC, Gan wrote:

Is there a better D graphics library in the works?

I'm using SFML(which is very easy and has lots of features) but 
it seems to use a lot of ram(if you leave it running for a 
while on a graphic intensive scene) and trying to make it 
include the dependencies with the compiled executable is 
complicated.


Is there a D 2D graphics library that's just as easy, cross 
platform, doesn't use X11, allows drawing to off-screen buffers 
and drawing those to screen? (plus supports nice drawing of 
shapes, circles, rectangles, lines)


I'm probably asking too much- I doubt such a thing exists.


SDL 2.x can do that with renderers.



Re: Worker is not finished while sending message to intermediate worker

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

On 02/09/2015 12:34 PM, xtreak wrote:

 I want the stringConverter to send a message to square which in
 turn messages the main function with string hello.

Your code did almost that but there was a bug (which I've pointed out).

 So are you saying that message is not delivered to square from
 stringWorker.

Not in your code due to that bug but the program that I've shown does 
exactly what you want.


 How does embedding the receive call inside the int in
 stackoverflow answer receives the message?

Once one receive() is executed, the program flow continues with further 
expressions. The suggestion that you received was making square() wait a 
second message. It need not be embedded at all. All it is needed was a 
second receive() call. That's all... However, that is not what you 
intended anyway.


You wanted stringConverter() to send a message to main(). So, just 
forget about that embeded receive and either do what my program did or 
fix the bug. :) (See my earlier message.)


 It seems silly but how can I keep my threads alive to receive
 messages?

By having an infinite loop like 'while (!done)'. (Please see my program.)

Ali



To write such an expressive code D

2015-02-09 Thread Dennis Ritchie via Digitalmars-d-learn

Good evening.
Is it possible to D something to replace the container on the F#, 
which displays the values of the sine from 0 to 90 degrees with 
an interval of 10 degrees:

let pi = Math.PI
let sins = [for x in 0.0..pi / 2.0 / 9.0..pi / 2.0 - sin x]
sins.Dump()

Output:
0
0,17364817766693
0,342020143325699
0,5
0,642787609686539
0,76603118978
0,866025403784439
0,939692620785908
0,984807753012208
1

P.S. Interested in code that will be as impressive as this. In 
General, I would like to see something akin to D.


Re: To write such an expressive code D

2015-02-09 Thread Dennis Ritchie via Digitalmars-d-learn

Thank you, Tobias Pankrath and Ali Çehreli.


Re: signal handling

2015-02-09 Thread rlonstein via Digitalmars-d-learn

On Saturday, 7 February 2015 at 18:30:00 UTC, Danny wrote:
[snip]
Seems to work fine so far. Not sure whether it's safe to 
raise() inside a signal handler. Calling raise() without 
reinstalling the old signal handler is a very bad idea, I

[snip]

I'm not sure that it's really safe to call raise in the handler.
Even C printf() is potentially unsafe leading to recommendations
to use only re-entrant functions. I would not expect raise() to
be safe (ex. never allocates memory or calls gc somewhere down
the line).

 [snip]
Yeah,I don't think that EINTR and flag-checking is even safe. 
What if you check the flag (and see nothing happened) and then 
go back to the loop and block (in read() or whatever), and 
right after the flag checking, unbeknowst to you the signal 
handler sets the flag, returns, and only then you block in 
read()? You'd block forever.


Yes, pretty much.


Do you know signalfd() ?


Yes, and I'm looking at using it.

I know how it is with external libaries, they always block at 
the silliest of times. But I've had one (OKAPI) which gave me 
the FD back, so I could select() on a bunch FDs in my mainloop. 
In that case, signalfd() was nice since it showed up as a 
normal read ready in select(), i.e. as a normal event 
source. Might be worth a try in your case?

 [snip]
so I'm thinking now of leveraging std.signals but I'm not sure 
that will be reliable.


Hmm, I don't know that one yet.


Again, probably not safe in sigaction, but using D's 
slots/signals to

communicate that a posix signal has been received and must be
handled. Along with signalfd(), I expect it will let me trigger 
object destruction.


Re: To write such an expressive code D

2015-02-09 Thread Vladimir Panteleev via Digitalmars-d-learn

On Monday, 9 February 2015 at 19:57:23 UTC, Ali Çehreli wrote:

writefln(%(%.15g\n%), sins);


In 2.067, you can write:

iota(0, PI/2, PI/2/9).map!sin.each!writeln;


How to write similar code D?

2015-02-09 Thread Dennis Ritchie via Digitalmars-d-learn

Tell me, please, how to write similar С# code D:

using System;
using System.Linq;

public class Test
{
public static void Main()
{
var query = Enumerable.Range(2, 10)
.Select(c = new { Length = 2 * c, Height = c * c - 1, 
Hypotenuse = c * c + 1 })
.Select(x = string.Format({0,4}{1,4}{2,4}, x.Height, 
x.Hypotenuse, x.Length));


foreach (var x in query)
Console.WriteLine(x);
}
}

Output:

  3   5   4
  8  10   6
 15  17   8
 24  26  10
 35  37  12
 48  50  14
 63  65  16
 80  82  18
 99 101  20
120 122  22


Re: internal compiler error with immutable

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

On 02/08/2015 05:21 AM, Danny wrote:


The obvious
 bool opEquals(immutable(C) b) immutable {
 return value == b.value;
 }
doesn't work. Probably have to override the one from Object ? Even
though I don't really use polymorphism here.

 override bool opEquals(Object b) immutable {
 return value == (cast(immutable(C)) b).value;
 }

Nope. Doesn't work (or compile) either.


Yes, opEquals for classes has a particular syntax:

  http://ddili.org/ders/d.en/object.html#ix_object.opEquals

Remember that 'const' binds to immutable as well. The following works 
with at least git head:


override bool opEquals(Object b) const {
auto rhs = cast(C)b;
return rhs  (value == rhs.value);
}

Ali



Re: signal handling

2015-02-09 Thread Danny via Digitalmars-d-learn

Hmmm...

Just found 
https://www.securecoding.cert.org/confluence/display/seccode/void+SIG33-C.+Do+not+recursively+invoke+the+raise%28%29+function, 
the bottom part Compliant Solution (POSIX) does raise() in the 
signal handler.


However, I can't find it in the POSIX standard at 
http://pubs.opengroup.org/onlinepubs/009695399/functions/sigaction.html.


But 
https://www.securecoding.cert.org/confluence/display/seccode/SIG30-C.+Call+only+asynchronous-safe+functions+within+signal+handlers 
lists the async-safe functions. raise() and sigaction() are in 
it. But _Exit() is, too.


Re: function and variable

2015-02-09 Thread Rikki Cattermole via Digitalmars-d-learn

On 10/02/2015 4:28 p.m., Fyodor Ustinov wrote:

Hi!

I think this code should not be compiled without any warning:

import std.stdio;

void f(int a) {
 writeln(it's a function! : , a);
}

void main() {
 auto f = function (int a) {writeln(It's a variable! : , a);};
 5.f();
 f(5);
}

Output:

it's a function! : 5
It's a variable! : 5

WBR,
 Fyodor.


That's a bug. It should be using the function pointer.

UFCS call should abide by the same scoping rules as anything else.
https://issues.dlang.org/show_bug.cgi?id=14161


Re: Worker is not finished while sending message to intermediate worker

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

On 02/09/2015 08:00 AM, xtreak wrote:

I am using programming in D to learn about D language. I wrote a
simple program that spawns a worker and sends it a number to receive its
square as a string. The worker 1 gets the number squares it and sends to
worker 2 (a different function) to get casted as string which is
returned to the worker 1 and thus it returns it to the main function
call. I can write the whole thing in a single thread. I wrote it to
understand about workers better. I used receive to get the worker 1 act
as per the input. The program is as follows

import std.stdio;
import std.concurrency;
import std.conv;
import core.thread;

void main() {

   foreach (int num; 1..100) {
 auto square_tid = spawn(square);
 square_tid.send(num);
 auto square = receiveOnly!string();
 writeln(square);
   }
}


void square() {
   static i = 0;
   receive (
(int num) {
  auto square = num * num;
  writeln(sqaure : Comes in with  , num ,  for  , ++i , 
time);
  auto stringWorker = spawn(stringConverter);
  stringWorker.send(thisTid, square, ownerTid);
},
(Tid tid, string str) {
  writeln(comes in string);
  send(tid, hello);
});
}

void stringConverter() {
   static i = 0;
   auto params = receiveOnly!(Tid, int, Tid)();
   auto stringified = to!string(params[1]); // Stringify the square
   writeln(string : Comes in with  , params[1],  for  , ++i ,  time);
   params[0].send(params[2], stringified); // params[0] - square
function tid, // params[2] - main function tid
}


I got the answer from stackoverflow @
https://stackoverflow.com/questions/28128383/worker-is-not-finished-while-sending-message-to-intermediate-worker.
But the person who answered my question asked me to post back to dlang
learn to learn more about it. As I spawn the function and a send a
message to stringConverter as it sends a message to the square function
why do I need to embed another receive call inside the int case as
indicated in the answer. How can I avoid embedding the receive call and
why does the person in the second answer used while(1) to receive the
message.


Let me answer your question about 'value' first. You said on StackOverflow:

 Why does it have the value = 0 as its obvious in the code.

I decided to use the value received to keep the example as simple as 
possible. Note that later examples use a special type Terminate to 
request termination:


  int value = 0;
  while (value = 0) { // -- Yes, obvious at first
value = receiveOnly!int(); // -- But may change here
// ...
  }

In message passing, threads normally start one more worker threads and 
send tasks to those threads. In your example, your thread threads that 
live for a very short time. In the case of square(), it creates a thread 
for every request that it receives.


There is technically nothing wrong with that. However, it will be a good 
idea to ensure that threads are alive when messages are sent to them. 
This is not the case in your code.


For example, square() starts a thread, sends a message to it, and leaves 
the receive() call. When stringConverter() sends the message, that 
message will go to the main() thread. square() has executed its 
receive() call and about to exit. In other words, the message comes in 
string will never be printed.


Again, nothing wrong with it; just stating the obvious... :)

If you want main() to receive stringConverter()'s message, then there is 
a bug in stringConverter() because it sends the message to square() 
which will never receive it:


params[0].send(/* ... */);

should be

params[2].send(/* ... */);

And then main() complains because it should receive just a string:

  Unexpected message type: expected 'string', got 
'std.typecons.Tuple!(Tid, string).Tuple'


so the call should actually be

  params[2].send(stringified);

Now it works. :)

Ali



Re: function and variable

2015-02-09 Thread Fyodor Ustinov via Digitalmars-d-learn
On Tuesday, 10 February 2015 at 04:11:43 UTC, Vlad Levenfeld 
wrote:
On Tuesday, 10 February 2015 at 03:59:22 UTC, Rikki Cattermole 
wrote:

That's a bug. It should be using the function pointer.

UFCS call should abide by the same scoping rules as anything 
else.

https://issues.dlang.org/show_bug.cgi?id=14161


I thought that's how UFCS was explicitly designed? Global 
symbols only, yes?


IMHO even if it is not a bug, it is a feature - it should not 
be compiled without notice.


WBR,
Fyodor.


Re: function and variable

2015-02-09 Thread Vlad Levenfeld via Digitalmars-d-learn
On Tuesday, 10 February 2015 at 04:20:27 UTC, Fyodor Ustinov 
wrote:
IMHO even if it is not a bug, it is a feature - it should not 
be compiled without notice.


WBR,
Fyodor.


Agreed, should be a warning at least.


Intended to be able to RefCount an interface?

2015-02-09 Thread weaselcat via Digitalmars-d-learn

Thread title.


interface Itest{

}

class testclass : Itest{
}

void main()
{
import std.typecons;
auto test = RefCounted!Itest(new testclass());
}

If you change the refcounted to type testclass it doesn't compile 
because refcounted doesn't support classes.


Is this a bug, or intended?


Re: Binding C++ value types

2015-02-09 Thread Nicholas Wilson via Digitalmars-d-learn

On Monday, 9 February 2015 at 19:30:32 UTC, Benjamin Thaut wrote:
When binding C++ value types you might want to use them by 
placing them on the D-Stack. This however seems to be not 
supported as the mangling for the constructor is completely 
wrong. Is this supposed to work?


Kind Regards
Benjamin Thaut


See https://issues.dlang.org/show_bug.cgi?id=14086
I can't see any reason why it should not to work. It is a very 
valid

use case.

Nic


Re: hasmap with tuple as key

2015-02-09 Thread Meta via Digitalmars-d-learn

On Monday, 9 February 2015 at 21:22:29 UTC, Ondra wrote:

Is there any drawback of doing this: string[Tuple!(int, int)] a;

Especially performance one.

Thanks.


Tuples are really just structs generated on the fly, so they 
are very fast. Hashmaps use the GC, though, so keep that in mind.


Re: Intended to be able to RefCount an interface?

2015-02-09 Thread Jakob Ovrum via Digitalmars-d-learn

On Tuesday, 10 February 2015 at 04:44:55 UTC, weaselcat wrote:

Thread title.


interface Itest{

}

class testclass : Itest{
}

void main()
{
import std.typecons;
auto test = RefCounted!Itest(new testclass());
}

If you change the refcounted to type testclass it doesn't 
compile because refcounted doesn't support classes.


Is this a bug, or intended?


I think this is a bug, and that the current RefCounted needs to 
reject interfaces. AFAIK, it is intended that 
RefCounted!SomeClass is supposed to reference-count the actual 
class instance, not its reference, which is what 
RefCounted!SomeInterface currently does.