Re: Declare reference to variable ?

2013-07-29 Thread Namespace

Alternative:


import std.stdio;

struct S {
uint longnamed;

alias longnamed this;
}

void main() {
S somestruct;

alias v = somestruct;
writeln(v);
v++;
writeln(v);
}



Re: Get template name

2013-07-29 Thread JS

On Tuesday, 30 July 2013 at 06:36:12 UTC, Dicebot wrote:

On Monday, 29 July 2013 at 23:02:57 UTC, JS wrote:

__FUNCTION__ does not return anything when used in templates.

For debugging purposes I sometimes use pragma(msg, 
template_name); (with template_name being the name of the 
template assigned an enum)


It can be relatively trivial or incredibly difficult, depending 
on exact string output for a given template you want get and 
set of template types supported.


I just want the current template name in the current scope. I 
know it seems trivial but it would help me reduce boilerplate 
code. I'm using it to debug and use the template name as a way to 
turn on debugging for specific templates if I need to analyze 
their behavior(lots of code generating templates that may have 
bugs).


A similar feature would end up being needed for functions when I 
log functions debug output.



With such a function I can just copy and paste the line to 
display the output rather than copy paste modify(replace the old 
template name with the new).


I don't need the template name of a template that called it... 
that is different, possibly useful but not required.





void f()
{
pragma(msg, __FUNCTION__);
}

template t()
{
pragma(msg, __FUNCTION__);
}

void main(string[] argv)
{
readln();
}

the function displays main.f. The template displays nothing. I'd 
prefer it to display main.t! or something unique so I can use it 
as a hash.




Re: Get template name

2013-07-29 Thread Dicebot

On Monday, 29 July 2013 at 23:02:57 UTC, JS wrote:

__FUNCTION__ does not return anything when used in templates.

For debugging purposes I sometimes use pragma(msg, 
template_name); (with template_name being the name of the 
template assigned an enum)


It can be relatively trivial or incredibly difficult, depending 
on exact string output for a given template you want get and set 
of template types supported.


Re: Declare reference to variable ?

2013-07-29 Thread yaz
Oh, it doesn't work when accessing member data. Sorry for the 
noise.


Re: Declare reference to variable ?

2013-07-29 Thread Dicebot

On Monday, 29 July 2013 at 21:25:21 UTC, Temtaime wrote:

No, i cannot.

struct S {
uint longnamed;
}

void main() {
S somestruct;

alias v = somestruct.longnamed;
writeln(v);
}

Error: need 'this' for 'longnamed' of type 'uint'

Is it a bug ?


I'd say this is something in between bug and enhancement request. 
Nothing explicitly states that alias should work this way but it 
matches concept of alias much better than current behavior.


Re: Declare reference to variable ?

2013-07-29 Thread yaz

You don't need to use dereference operator. Example:
http://dpaste.dzfl.pl/d34c23e5

import std.stdio;
struct Foo {
  void answer() {
writeln("As you wish!");
  }
}

void main() {
  Foo veryVeryLongNamedStruct;
  auto v = &veryVeryLongNamedStruct;
  v.answer();
}


Re: Declare reference to variable ?

2013-07-29 Thread Namespace

On Monday, 29 July 2013 at 21:25:21 UTC, Temtaime wrote:

No, i cannot.

struct S {
uint longnamed;
}

void main() {
S somestruct;

alias v = somestruct.longnamed;
writeln(v);
}

Error: need 'this' for 'longnamed' of type 'uint'

Is it a bug ?


Oh, that is annoying...
Then, make your own reference type:

import std.stdio;

struct S {
uint longnamed;
}

struct Ref(T) {
public:
T* ptr;

@disable
this();

/*@disable
this(this);*/

@disable
this(typeof(null));

@disable
void opAssign(ref Ref!T);

this(T* ptr) {
this.ptr = ptr;
}

@property
ref inout(T) get() inout {
return *this.ptr;
}

alias get this;
}

void main() {
S somestruct;

Ref!uint v = &somestruct.longnamed;
writeln(v);
}


Re: Get template name

2013-07-29 Thread evilrat

On Monday, 29 July 2013 at 23:02:57 UTC, JS wrote:

__FUNCTION__ does not return anything when used in templates.

For debugging purposes I sometimes use pragma(msg, 
template_name); (with template_name being the name of the 
template assigned an enum)


I would like to make this more general such as

pragma(msg, mixin(__FUNCTION_NAME__));

e.g.,

template t()
{
enum t = "asdf";
pragma(msg, t);
}

same but more general

template t()
{
   enum t = "asdf";
   pragma(msg, __FUNCTION_NAME__);
}

(please don't ask why I would do this... this is a simple 
example, my case is more complex, trust me that I have a reason)


is this an option?
writeln("template name: ", t.stringof);


Re: Declare reference to variable ?

2013-07-29 Thread Maxim Fomin

On Monday, 29 July 2013 at 21:37:30 UTC, bearophile wrote:

Temtaime:


Why i cannot declare reference in D ?


I don't know the reasons. But maybe you can create a little 
struct with just a pointer inside and an alias this to a member 
function that returns a ref.


Bye,
bearophile


It doesn't work because alias this does not capture context 
pointer like delegate.


Re: use template function without assignment

2013-07-29 Thread Meta

On Tuesday, 30 July 2013 at 01:06:39 UTC, Meta wrote:
Does this code do what you want, or are there other 
requirements as well?


void Pragma(alias amsg)(string file = __FILE__)
{
pragma(msg, amsg);
}


Actually, sorry, that's the exact same code, just with some 
syntactic sugar. To avoid the "expression has no effect" problem, 
you could use an alias instead of an enum. You're getting the 
error when you try to use pragma in the "global" scope, right?


void Pragma(alias amsg)(string file = __FILE__)
{
pragma(msg, amsg);
}

alias temp = Pragma!("test");

In general, D doesn't have top-level expressions like what I 
think you want to do here. Maybe somebody else knows a way to 
work around that.


Re: use template function without assignment

2013-07-29 Thread Meta

On Monday, 29 July 2013 at 23:09:20 UTC, JS wrote:
I have created a template Pragma that emulates pragma but 
better, the problem is that I have to assign it to something 
which is very redundant in my code:


enum temp = Pragma!(msg)

e.g.,

template Pragma(alias amsg)
{
string Pragma(string file = __FILE__)
{
pragma(msg, amsg);
return "";
}
}

When I try to use void instead of string and do something like

Pragma!(msg)

I get an error that the template has no effect. It does have an 
effect but what it is complaining about is exactly what I want.


I've tried all kinds of combinations(mixins work but I then 
can't ise __FILE__) and nothing works. Maybe someone has an 
idea.


Does this code do what you want, or are there other requirements 
as well?


void Pragma(alias amsg)(string file = __FILE__)
{
pragma(msg, amsg);
}


use template function without assignment

2013-07-29 Thread JS
I have created a template Pragma that emulates pragma but better, 
the problem is that I have to assign it to something which is 
very redundant in my code:


enum temp = Pragma!(msg)

e.g.,

template Pragma(alias amsg)
{
string Pragma(string file = __FILE__)
{
pragma(msg, amsg);
return "";
}
}

When I try to use void instead of string and do something like

Pragma!(msg)

I get an error that the template has no effect. It does have an 
effect but what it is complaining about is exactly what I want.


I've tried all kinds of combinations(mixins work but I then can't 
ise __FILE__) and nothing works. Maybe someone has an idea.








Get template name

2013-07-29 Thread JS

__FUNCTION__ does not return anything when used in templates.

For debugging purposes I sometimes use pragma(msg, 
template_name); (with template_name being the name of the 
template assigned an enum)


I would like to make this more general such as

pragma(msg, mixin(__FUNCTION_NAME__));

e.g.,

template t()
{
enum t = "asdf";
pragma(msg, t);
}

same but more general

template t()
{
   enum t = "asdf";
   pragma(msg, __FUNCTION_NAME__);
}

(please don't ask why I would do this... this is a simple 
example, my case is more complex, trust me that I have a reason)




Re: Declare reference to variable ?

2013-07-29 Thread bearophile

Temtaime:


I have a long named variable in a struct.

For example let's name that longnamedstruct.longnamedmember

I need to use that variable in many places of the code and i 
cannot create the copy of it.


You can shorten the outer name with an alias or "remove" it with 
a with() statement.




Why i cannot declare reference in D ?


I don't know the reasons. But maybe you can create a little 
struct with just a pointer inside and an alias this to a member 
function that returns a ref.


Bye,
bearophile


Re: Declare reference to variable ?

2013-07-29 Thread Temtaime

No, i cannot.

struct S {
uint longnamed;
}

void main() {
S somestruct;

alias v = somestruct.longnamed;
writeln(v);
}

Error: need 'this' for 'longnamed' of type 'uint'

Is it a bug ?


Re: Declare reference to variable ?

2013-07-29 Thread Namespace

On Monday, 29 July 2013 at 21:13:54 UTC, Temtaime wrote:

I have a long named variable in a struct.

For example let's name that longnamedstruct.longnamedmember

I need to use that variable in many places of the code and i 
cannot create the copy of it.


In c++ i can
auto &v = longnamedstruct.longnamedmember;

Now i can use v anywhere.
Why i cannot declare reference in D ? Or can i ?

I can make pointer to that, but it is workaround and it's need 
to use variable dereferencing by * operator.


You can use alias:
alias v = longnamedstruct.longnamedmember;


Declare reference to variable ?

2013-07-29 Thread Temtaime

I have a long named variable in a struct.

For example let's name that longnamedstruct.longnamedmember

I need to use that variable in many places of the code and i 
cannot create the copy of it.


In c++ i can
auto &v = longnamedstruct.longnamedmember;

Now i can use v anywhere.
Why i cannot declare reference in D ? Or can i ?

I can make pointer to that, but it is workaround and it's need to 
use variable dereferencing by * operator.


Re: Async messages to a thread.

2013-07-29 Thread John Colvin

On Monday, 29 July 2013 at 16:19:03 UTC, lindenk wrote:

void foo(Tid parent) {
   bool run = true;
   while(run)
   {
//do_some_blocking_function();
receiveTimeout(dur!"msecs"(0),
(string s){if(s == "STAHP!") run = false;}
);
   }

   // clean up
   send(parent, "done");
}


void main()
{
   auto tid = spawn(&foo, thisTid);

   // do stuff / wait for some condition

   send(tid, "STAHP!");
   auto msg = receiveOnly!(string)();
   assert(msg == "done");
}


Ah, no I mean, what if do_some_blocking_function blocks for 
some indeterminate amount of time. I would like it to exit even 
when it is currently blocking (as it could be unpredictable 
when it will stop blocking).


The blocking function could start in a new thread and do a 
receiveTimeout periodically, or you could make it stateful (if 
necessary) and return periodically and be restarted if it hasn't 
been asked to stop (which is just a very ugly and rudimentary 
version of what you'd want to do with fibers).


Or, if you don't care about making a mess, just kill the thread I 
guess.


Re: Async messages to a thread.

2013-07-29 Thread lindenk

On Monday, 29 July 2013 at 17:26:55 UTC, Sean Kelly wrote:
On Jul 29, 2013, at 10:07 AM, lindenk  
wrote:



After a bit more research it looks like everyone else uses -

while(checkIfRunning())
{
   // block with timeout
}

which leads me to believe this might not be possible or 
standard. Although, should something along the lines of this 
be possible?


It sounds like you're maybe doing socket IO?  In those cases, 
you can open a pipe for signaling.  select() on whatever other 
socket plus the read end of the pipe, and if you want to break 
out of the blocking select() call, write a byte to the pipe.  
Ultimately, std.concurrency should get some socket integration 
so data can arrive as messages, but that will never be as 
efficient as the approach I've described.



Process p = new Process();
p.doTask(p.func()); //create new thread and start function

// do stuff until this needs to exit

p.stop(); // halt running function,
p.cleanup(); // call a clean up function for func's resources
p.kill(); // forcibly kill the thread


Forcibly killing threads tends to be a pretty bad idea if you 
intend to keep running after the thread is killed.  It can even 
screw up attempts at a clean shutdown.



The blocking function will sometimes be socket IO, it's set up to 
get data from whatever it is set to. That sounds promising 
though, I could set it up to do that when it is socket IO then 
have it poll when it isn't. Thanks!


Re: Async messages to a thread.

2013-07-29 Thread Sean Kelly
On Jul 29, 2013, at 10:07 AM, lindenk  wrote:

> After a bit more research it looks like everyone else uses -
> 
> while(checkIfRunning())
> {
>// block with timeout
> }
> 
> which leads me to believe this might not be possible or standard. Although, 
> should something along the lines of this be possible?

It sounds like you're maybe doing socket IO?  In those cases, you can open a 
pipe for signaling.  select() on whatever other socket plus the read end of the 
pipe, and if you want to break out of the blocking select() call, write a byte 
to the pipe.  Ultimately, std.concurrency should get some socket integration so 
data can arrive as messages, but that will never be as efficient as the 
approach I've described.

> Process p = new Process();
> p.doTask(p.func()); //create new thread and start function
> 
> // do stuff until this needs to exit
> 
> p.stop(); // halt running function,
> p.cleanup(); // call a clean up function for func's resources
> p.kill(); // forcibly kill the thread

Forcibly killing threads tends to be a pretty bad idea if you intend to keep 
running after the thread is killed.  It can even screw up attempts at a clean 
shutdown.

Re: untuple a tuple

2013-07-29 Thread Meta

On Monday, 29 July 2013 at 16:57:49 UTC, JS wrote:

if I have something like

template t(args...)
{
pragma(msg, args);
}

it prints out args in a tuple... e.g.,

tuple!(...)

I do not want it to print out the tuple!().

I can write my own pragma and pass each arg to it (e.g., 
pragma(msg, arg[0], arg[1], ...)) but this is not very general 
and requires a lot of static if's (one for each possible n).



Is it possible to "untuple"?


template t(args...)
{
pragma(msg, args);
}

void main()
{
alias f = t!(int, double, string);
}

Prints `(int, double, string)` for me, with no `tuple!` part. 
However, putting it in a template means you need an alias, so 
it's probably better to make it a function instead.


void t(args...)()
{
pragma(msg, args);
}

void main()
{
t!(int, double, string);
}


Re: Async messages to a thread.

2013-07-29 Thread Sean Kelly
On Jul 29, 2013, at 8:28 AM, lindenk  wrote:
> 
> Ah, no I mean, what if do_some_blocking_function blocks for some 
> indeterminate amount of time. I would like it to exit even when it is 
> currently blocking (as it could be unpredictable when it will stop blocking).

Execute the blocking functions in a separate thread?  Really, this sounds like 
the kind of thing futures are for.  Alternately, maybe the sequence of blocking 
functions could call receive periodically, or maybe be executed in a fiber and 
yield periodically, etc.

Re: Async messages to a thread.

2013-07-29 Thread lindenk

After a bit more research it looks like everyone else uses -

while(checkIfRunning())
{
// block with timeout
}

which leads me to believe this might not be possible or standard. 
Although, should something along the lines of this be possible?


Process p = new Process();
p.doTask(p.func()); //create new thread and start function

// do stuff until this needs to exit

p.stop(); // halt running function,
p.cleanup(); // call a clean up function for func's resources
p.kill(); // forcibly kill the thread


If this is possible and/or not a bad idea, how can I accomplish 
this in D? (And if there isn't really a way, I feel like D's 
scope(exit) and such should work nicely with it).


untuple a tuple

2013-07-29 Thread JS

if I have something like

template t(args...)
{
pragma(msg, args);
}

it prints out args in a tuple... e.g.,

tuple!(...)

I do not want it to print out the tuple!().

I can write my own pragma and pass each arg to it (e.g., 
pragma(msg, arg[0], arg[1], ...)) but this is not very general 
and requires a lot of static if's (one for each possible n).



Is it possible to "untuple"?


Re: Async messages to a thread.

2013-07-29 Thread lindenk

void foo(Tid parent) {
bool run = true;
while(run)
{
 //do_some_blocking_function();
 receiveTimeout(dur!"msecs"(0),
 (string s){if(s == "STAHP!") run = false;}
 );
}

// clean up
send(parent, "done");
}


void main()
{
auto tid = spawn(&foo, thisTid);

// do stuff / wait for some condition

send(tid, "STAHP!");
auto msg = receiveOnly!(string)();
assert(msg == "done");
}


Ah, no I mean, what if do_some_blocking_function blocks for some 
indeterminate amount of time. I would like it to exit even when 
it is currently blocking (as it could be unpredictable when it 
will stop blocking).


Re: Async messages to a thread.

2013-07-29 Thread John Colvin

On Monday, 29 July 2013 at 15:28:45 UTC, lindenk wrote:

Hello!
This is partially a general question as I don't know what this 
is called or if it exists. Say for example I want to do 
something like the following -


import std.concurrency;

void main()
{
void foo() {
try
{
while(true)
{
 do_some_blocking_function();
}
} catch(GotAMessage msg){}

// clean up
};

tid = spawn(&foo, thisTid);

// do stuff / wait for some condition

send(tid, "STAHP!");
}

One solution I've used in the past is setting a timeout for the 
blocking function then polling to see if the thread should 
exit. The disadvantage is it causes extra, unnecessary checking 
and has a delay before it gets the message and exits.


This is a fairly simple example but the idea should be the 
same, is there a way to pass a message or signal to a thread to 
cause it to branch to some other location?


import std.concurrency;
import core.time;

void foo(Tid parent) {
bool run = true;
while(run)
{
 //do_some_blocking_function();
 receiveTimeout(dur!"msecs"(0),
 (string s){if(s == "STAHP!") run = false;}
 );
}

// clean up
send(parent, "done");
}


void main()
{
auto tid = spawn(&foo, thisTid);

// do stuff / wait for some condition

send(tid, "STAHP!");
auto msg = receiveOnly!(string)();
assert(msg == "done");
}


Async messages to a thread.

2013-07-29 Thread lindenk

Hello!
This is partially a general question as I don't know what this is 
called or if it exists. Say for example I want to do something 
like the following -


import std.concurrency;

void main()
{
void foo() {
try
{
while(true)
{
 do_some_blocking_function();
}
} catch(GotAMessage msg){}

// clean up
};

tid = spawn(&foo, thisTid);

// do stuff / wait for some condition

send(tid, "STAHP!");
}

One solution I've used in the past is setting a timeout for the 
blocking function then polling to see if the thread should exit. 
The disadvantage is it causes extra, unnecessary checking and has 
a delay before it gets the message and exits.


This is a fairly simple example but the idea should be the same, 
is there a way to pass a message or signal to a thread to cause 
it to branch to some other location?


Re: Generic string join

2013-07-29 Thread JS

On Monday, 29 July 2013 at 09:01:36 UTC, JS wrote:
Here is a optimal generic string joiner that maximizes compiler 
optimization, allows for a variable number of arguments that 
can be strings or arrays of strings.


http://dpaste.dzfl.pl/0a021e1f

Drawbacks:
1. Static if explosion
2. Not elegant
3. Only works with RT strings or RT string arrays, would be 
nice to work with chars and possibly others.
4. Does not have the ability to use book ends 
delimitation... not difficult to do but just more bloat-code.
5. Would be nice to allow apply a lambda to each "element". 
Again, not hard but requires all those special cases to deal 
with proper delimitation due to the recursion required.
6. Requires a mixin statement... this is not pretty at all. 
One can't wrap the mixin in a normal function call because it 
defeats the purpose of optimization(CT strings will be passed 
as variables and not optimized by the CT).

etc...

While I have some code to flatten such arrays before 
delimitation, which would probably make it much easier, which 
is the way I started, the code was going to turn out to be the 
same in the end as the flattener, so I just modified the 
code(basically use string concatenation instead of tuple 
concatenation).



Anyways, maybe someone has a good idea how to improve the code 
to achieve the same results. Note in in the test, every 
possible 3-combination is tried, which I think is good enough 
to prove the validity of the code.


BTW, one can wrap the template to remove the mixin in #6 but this 
is not very satisfactory as it has a much higher performance hit 
for CT joining.


template t(T...)
{
string t(string d, T args)
{
return mixin(tJoin!(",", args));
}
}

mixin(tJoin!(",", "a", "b")) is just 4 instructions max. Using t 
is about 10-15 times that(although I'm not sure if inlining will 
solve that).




Generic string join

2013-07-29 Thread JS
Here is a optimal generic string joiner that maximizes compiler 
optimization, allows for a variable number of arguments that can 
be strings or arrays of strings.


http://dpaste.dzfl.pl/0a021e1f

Drawbacks:
1. Static if explosion
2. Not elegant
3. Only works with RT strings or RT string arrays, would be 
nice to work with chars and possibly others.
4. Does not have the ability to use book ends delimitation... 
not difficult to do but just more bloat-code.
5. Would be nice to allow apply a lambda to each "element". 
Again, not hard but requires all those special cases to deal with 
proper delimitation due to the recursion required.
6. Requires a mixin statement... this is not pretty at all. 
One can't wrap the mixin in a normal function call because it 
defeats the purpose of optimization(CT strings will be passed as 
variables and not optimized by the CT).

etc...

While I have some code to flatten such arrays before 
delimitation, which would probably make it much easier, which is 
the way I started, the code was going to turn out to be the same 
in the end as the flattener, so I just modified the 
code(basically use string concatenation instead of tuple 
concatenation).



Anyways, maybe someone has a good idea how to improve the code to 
achieve the same results. Note in in the test, every possible 
3-combination is tried, which I think is good enough to prove the 
validity of the code.