Re: Distinguish float and integer types from string

2019-03-11 Thread Johann Lermer via Digitalmars-d-learn

On Saturday, 9 March 2019 at 18:11:09 UTC, Jacob Shtokolov wrote:
I tried to use std.conv.to and std.conv.parse, but found that 
they can't really do this. When I call `data.to!int`, the value 
of "123.45" will be converted to int!


Are you sure? This here works for me:

import std.stdio;
import std.string;
import std.conv;

void main ()
{
auto s = readln.strip;

try
{
int i = to!int (s);
writefln ("string is an int: %s", i);
}
catch(Exception e)
{
try
{
float f = to!float(s);
writefln ("string is a float: %s", f);
}
catch(Exception e)
{
writefln ("string is an neither an int nor a float: 
%s", s);

}
}
}


Re: const pointers C vs. D

2020-02-06 Thread Johann Lermer via Digitalmars-d-learn

On Tuesday, 4 February 2020 at 10:17:39 UTC, Dennis wrote:
C++ has a const system that is closer to D's than any other 
language, but it still has huge differences:


Thanks, that clears it up a bit!


const pointers C vs. D

2020-02-04 Thread Johann Lermer via Digitalmars-d-learn

Hi,

I'm just wondering about defining const pointers and if there's a 
difference in C and D.


in C, this works:

const char* text = "Hello";
text = "world";

but in D it doesn't, because the char* is const. Ff I would like 
tho have the same behaviour in D as in C, I need to write:


const (char)* text = "Hello";
text = "world";

In C, this would not be valid. So the question for me now is: is 
const char* in D different from C?




Re: sending the address of a struct

2020-09-06 Thread Johann Lermer via Digitalmars-d-learn

On Sunday, 6 September 2020 at 11:10:14 UTC, Simen Kjærås wrote:

auto e = receiveOnly!(shared(Env)*);


Oh, thanks. Seems, that I just missed that bit with the 
pranetheses.


sending the address of a struct

2020-09-06 Thread Johann Lermer via Digitalmars-d-learn

Hi,

I have a struct in a separate thread and want to pass it's 
address back to the main thread. This is how I think it should 
work:


import std.concurrency;

struct Env {}

void run ()
{
shared Env env;
ownerTid.send ();
for (;;) {}
}

void main ()
{
spawn ();
auto e = receiveOnly!(shared Env*);
}

but I'm getting an error when main tries to receive the pointer. 
The error message says:


std.concurrency.MessageMismatch@std/concurrency.d(237): 
Unexpected message type: expected 'shared(Env*)', got 
'shared(test.Env)*'


Now, how can I pass that pointer back to main?



Re: How Stop Worker Thread if Owner Thread is Finished?

2020-10-27 Thread Johann Lermer via Digitalmars-d-learn
You could tell your thread via a shared variable that main has 
ended:


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

shared bool end = false;

void thread ()
{
for (;;)
{
Thread.sleep (500.msecs);
synchronized
{
if (end) break;
}
}
writeln ("thread ends");
}

void main ()
{
spawn ();
Thread.sleep (3.seconds);
writeln ("main ends");
synchronized
{
end = true;
}
}

or you could use the fact, that receiveTimeout throws an 
exception, when main ends (although I don't know if this is the 
intended behaviour; the manual just says that it throws an 
exception when the sending thread was terminated):


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

void thread ()
{
for (;;)
{
try
{
receiveTimeout (500.msecs);
}
catch (Throwable)
{
break;
}

}
writeln ("thread ends");
}

void main ()
{
auto tid = spawn ();
Thread.sleep (3.seconds);
writeln ("main ends");
}




Re: How to implement Canceleable spawn() from parent

2020-06-29 Thread Johann Lermer via Digitalmars-d-learn
I'm doing this in an X11 application in order to send a timer 
event every 100 milliseconds to the main event queue.


class Application
{
  shared private bool s_tick;

  void clock_task (shared X11.Display* disp, X11.Atom atom, 
X11.Window win)

  {
for (;;)
{
  try
  {
receiveTimeout (100.msecs);

if (disp && atomicLoad(s_tick))
{
  // disable ticking until it is allowed again at the end 
of the event loop

  atomicStore(s_tick, false);

  X11.XClientMessageEvent event;
  event.type = X11.ClientMessage;
  event.window   = win;
  event.message_type = atom;
  event.format   = 32;
  event.data.l   = [0, 0, 0, 0, 0];

  X11.XSendEvent (cast (X11.Display*) disp, win, 0, 0,  
cast(X11.XEvent*));

  X11.XFlush (cast (X11.Display*) disp);
}
  }
  catch (Throwable)
  {
return;
  }
}
  }

  this ()
  {
...
spawn (_task, cast(shared)x11Display, x11SigClockAtom, 
_x11_proxyWindow);

  }

  run ()
  {
while (true)
{
...
  // event processing starts here: read in X11 event and 
convert it to a wit Event

  X11.XEvent x11_event;
  X11.XNextEvent (_x11.display, _event);
...
  atomicStore(s_tick, true);
}
  }
}


Re: Visual D showing weird errors

2021-04-26 Thread Johann Lermer via Digitalmars-d-learn

On Monday, 26 April 2021 at 08:58:25 UTC, Raimondo Mancino wrote:

According to -v: DMD64 D Compiler v2.096.0-dirty


Well, that says it all, doesn't it? I'm not familiar with the 
windows versions, but that doesn't seem to be an offical release 
- at least I never had a dmd2 that claimed to be dirty ;-)


Re: Visual D showing weird errors

2021-04-26 Thread Johann Lermer via Digitalmars-d-learn

On Monday, 26 April 2021 at 10:42:54 UTC, Mike Parker wrote:
(According to -v: DMD64 D Compiler v2.096.0-dirty)
That's actually normal for the Windows versions. I'm not sure 
where it comes from, but it's always there.


Ouuu, that's bad advertising, isn't it? Who wants to use dirty 
software, it could infect your computer... :-O




alias this - am I using it wrong?

2021-08-25 Thread Johann Lermer via Digitalmars-d-learn

Hi all,

I have a little problem understanding alias this. I always 
thought, that alias this only makes implicit conversions from the 
aliased object to this. Then, why do lines 18 and 22 compile in 
the code below? And, btw, line 22 crashes with a segmentation 
fault.


```d
01 struct Test_Struct {long t;}
02
03 class Alias_Class
04 {
05 Test_Struct ts;
06 alias ts this;
07 }
08
09 class Test_Class
10 {
11 Alias_Class ac;
12 }
13
14 void main ()
15 {
16 auto ac = new Alias_Class;
17 Test_Struct ts = ac;  // compiles
18 ac = ts;  // compiles as well - why?
19
20 auto tc = new Test_Class;
21 ts = tc.ac;   // compiles
22 tc.ac = ts;   // again this compiles, but seg 
faults

23 }
```

Johann



Re: alias this - am I using it wrong?

2021-08-25 Thread Johann Lermer via Digitalmars-d-learn

On Wednesday, 25 August 2021 at 12:23:06 UTC, Adam D Ruppe wrote:
...

Thanks - that explains in all.

On Wednesday, 25 August 2021 at 12:23:32 UTC, FeepingCreature 
wrote:



class Alias_Class
{
Test_Struct ts;
Test_Struct getter() { return ts; }
alias getter this;
}


Good idea, that should solve my current problem. The reason for 
having an alias this was that I had a C data type 
(cairo_surface_t) that I transferred into a class and I'm using 
the alias so that I don't need to rewrite the whole application 
at once. Unfortunately I overlooked some code that resulted in a 
seg fault - and I was lost at understanding why.


problem with alias this and Tuple

2021-10-12 Thread Johann Lermer via Digitalmars-d-learn

Hi all,

I have a problem with Tuples and struct templates that contain an 
alias this:

```d
import std;

struct Element(T)
{
T payload;
alias payload this;

this (T p)
{
payload = p;
}
}

class Item {}

void main ()
{
auto e = Element!Item (new Item());
auto t = tuple(e);
}
```
dmd generates a warning: **Warning: struct Element has method 
toHash, however it cannot be called with const(Element!(Item)) 
this.
Element.toHash defined here: 
/usr/local/include/d/druntime/import/object.d(83)**


The culprit seems to be the 'alias payload this'. When I remove 
that line, the code works. However, in my code, I need that 
alias. Any ideas, what's wrong or what I could do? Thanks.


Re: problem with alias this and Tuple

2021-10-12 Thread Johann Lermer via Digitalmars-d-learn
Thanks, understood. But isn't this a deficiency in the library 
that should be fixed?


C structs

2014-06-20 Thread Johann Lermer via Digitalmars-d-learn

Hi,

I'm fiddling around with cairo (downloaded 
fromhttps://github.com/D-Programming-Deimos/cairo) and I stumbled 
over this problem:


(file rectandmatrix.d)

import deimos.cairo.cairo;

void main ()
{
cairo_rectangle_int_t rect;
cairo_matrix_t matrix;
}


Compiling that with 'dmd rectandmatrix' fails with this error:

rectandmatrix.o: In function `_Dmain':
rectandmatrix.d:(.text._Dmain+0x18): undefined reference to 
`_D6deimos5cairo5cairo14cairo_matrix_t6__initZ'

collect2: error: ld returned 1 exit status
--- errorlevel 1

The rectangle is OK - no error there. The matrix however, 
produces the error. The difference between both is that 
cairo_rectangle_int_t is a struct containing integers, and 
cairo_matrix_t contains doubles.


I can, however, compile like that:

'dmd rectandmatrix deimos/cairo/cairo.d' (deimos being in a 
subdir of the working directory)


then cairo.d is compiled as well and linked to the code.

So, my questions are:

1) Why do I have to compile and link cairo.d - it's only an 
definition file for C code and IMHO there shouldn't be any need 
to compile cairo.d at all.


2) Why ist a struct containing only integers handled so 
differently from a struct holding doubles?


Thanks


Re: C structs

2014-06-20 Thread Johann Lermer via Digitalmars-d-learn
Agreed, but I assumed, that since all definitions in cairo.d are 
defined as
extern (System), (same happens with extern (C), btw.), I would 
have expected,

that D does not implicitly generate initialisation functions.

So, why is there no init routine for the rectangle? There's only 
one for the matrix.