Re: shared Variant[string]

2015-01-28 Thread Tobias Pankrath via Digitalmars-d-learn
On Wednesday, 28 January 2015 at 12:29:09 UTC, Fyodor Ustinov 
wrote:

On Wednesday, 28 January 2015 at 11:27:53 UTC, Kagamin wrote:
Associative array doesn't support thread-safe operations, 
that's why they don't work on shared instance. You should use 
std.concurrency or implement low-level concurrency mechanism.


If associative array does not support share attribute, this 
code should not be compiled without any warning or error, I 
think:


shared string[string] t;
void main() {
t[t] = bebebe;
}

But will.


In your case above, it's std.variant : Variant, which does not 
work when shared. You'll need to cast it and ensure yourself that 
no two threads access the same instance concurrently.


I don't know, if AA should/do work with shared.


Re: How to copy object of class A to another object of class B?

2015-01-28 Thread zhmt via Digitalmars-d-learn

Anybody help?


Re: How to copy object of class A to another object of class B?

2015-01-28 Thread zhmt via Digitalmars-d-learn

void getT(SRC,DEST)(SRC src,DEST dest)
{
foreach (i, type; typeof(SRC.tupleof)) {
string name = SRC.tupleof[i].stringof;
		__traits(getMember, dest, name) =  __traits(getMember, src, 
name);

writeln(name);
}
}

when I write the code above, the compile complains that:

source/app.d(14): Error: variable name cannot be read at compile 
time.


how convert the range to slice ?

2015-01-28 Thread mzfhhhh via Digitalmars-d-learn

is there any simple way to convert?

int [] arr1 = [1,2,3].map!a*2;   //compile error
int [] arr2 = [1,2,3].filter!a3;//compile error


Using std.net.curl to stream data

2015-01-28 Thread Trollgeir via Digitalmars-d-learn

I'm having some trouble trying to stream data to my plot.ly graph:
https://plot.ly/62/~Trollgeir/

The API:  https://plot.ly/streaming/

I am able to post messages that get recorded into the stream 
live, although right after curl uploads it, it just seems to wait 
for a response it's not getting, and eventually timeouts. Does 
anyone have any advice?


auto client = HTTP(stream.plot.ly);
client.addRequestHeader(plotly-streamtoken,e8bg6omat6);
client.verbose = true;

string msg = { \x\: 500, \y\: 500 } \n;
client.postData(msg);
client.perform; 


How to copy object of class A to another object of class B?

2015-01-28 Thread zhmt via Digitalmars-d-learn

I have a struct created by thrift:

struct Card {
  long id;
  string pwd;
  long agentId;
  bool valid;
  long rmb;
  long createDate;
  long soldDate;
  long chargeDate;

  mixin TStructHelpers!([
TFieldMeta(`id`, 1, TReq.OPTIONAL),
TFieldMeta(`pwd`, 2, TReq.OPTIONAL),
TFieldMeta(`agentId`, 3, TReq.OPTIONAL),
TFieldMeta(`valid`, 4, TReq.OPTIONAL),
TFieldMeta(`rmb`, 5, TReq.OPTIONAL),
TFieldMeta(`createDate`, 6, TReq.OPTIONAL),
TFieldMeta(`soldDate`, 7, TReq.OPTIONAL),
TFieldMeta(`chargeDate`, 8, TReq.OPTIONAL)
  ]);
}

and another class created for hibernated:

class Card
{
import hibernated.core;

@Id
@Generated
long id;
@UniqueKey
string pwd;
//index
long agentId;
bool valid;
long rmb;
long createDate;
long soldDate;
long chargeDate;
}


Sometime , I need to copy them:

thrift.Card tc;

db.Card dc;

dc.id = tc.id;
dc.pwd = tc.pwd;
...


It is boring coding, I want a solution to copy them automatically:
void copyObj(SRC,DEST)(SRC src,DEST dest)
{
foreach (i, type; typeof(SRC.tupleof)) {
auto name = SRC.tupleof[i].stringof;
		__traits(getMember, dest, name) =  __traits(getMember, src, 
name);

writeln(name);
}
}

Unfortunitely, it doesnt work,  how to improve it?

Any suggestions is welcome.
Thx ahead!!!


Re: shared Variant[string]

2015-01-28 Thread zhmt via Digitalmars-d-learn
On Wednesday, 28 January 2015 at 10:16:02 UTC, Fyodor Ustinov 
wrote:

Hi!

Simple program:
import std.variant;

shared Variant[string] t;

void main() {
t[t] = bebebe;
}

Without shared this program compiles and works.

With shared I get:

aa.d(6): Error: template 
std.variant.VariantN!32LU.VariantN.opAssign cannot deduce 
function from argument types !()(string) shared, candidates are:
/usr/include/dmd/phobos/std/variant.d(577):
std.variant.VariantN!32LU.VariantN.opAssign(T)(T rhs)
aa.d(6): Error: cannot implicitly convert expression 
(VariantN( handler, cast(ubyte)0u, ).this(bebebe)) of type 
VariantN!32LU to shared(VariantN!32LU)


Help me, please.


try __gshared


shared Variant[string]

2015-01-28 Thread Fyodor Ustinov via Digitalmars-d-learn

Hi!

Simple program:
import std.variant;

shared Variant[string] t;

void main() {
t[t] = bebebe;
}

Without shared this program compiles and works.

With shared I get:

aa.d(6): Error: template 
std.variant.VariantN!32LU.VariantN.opAssign cannot deduce 
function from argument types !()(string) shared, candidates are:
/usr/include/dmd/phobos/std/variant.d(577):
std.variant.VariantN!32LU.VariantN.opAssign(T)(T rhs)
aa.d(6): Error: cannot implicitly convert expression (VariantN( 
handler, cast(ubyte)0u, ).this(bebebe)) of type VariantN!32LU 
to shared(VariantN!32LU)


Help me, please.


Re: shared Variant[string]

2015-01-28 Thread Fyodor Ustinov via Digitalmars-d-learn

On Wednesday, 28 January 2015 at 10:20:42 UTC, zhmt wrote:


try __gshared


It seems to me - is not the solution, it's a hack. I want to 
understand what the happening. :)





Re: shared Variant[string]

2015-01-28 Thread Kagamin via Digitalmars-d-learn

Some reading:
http://ddili.org/ders/d.en/concurrency.html
http://ddili.org/ders/d.en/concurrency_shared.html
http://www.informit.com/articles/article.aspx?p=1609144


Re: Does anybody work on std.net.curl?

2015-01-28 Thread Rikki Cattermole via Digitalmars-d-learn

On 29/01/2015 12:54 a.m., Suliman wrote:

Just interesting is there any plans to replace net.curl on native
lib? I mean not plans for long future, but some stuff on which
somebody active work?


The best I can suggest is Vibe.d.


Re: shared Variant[string]

2015-01-28 Thread Fyodor Ustinov via Digitalmars-d-learn

On Wednesday, 28 January 2015 at 11:27:53 UTC, Kagamin wrote:
Associative array doesn't support thread-safe operations, 
that's why they don't work on shared instance. You should use 
std.concurrency or implement low-level concurrency mechanism.


If associative array does not support share attribute, this 
code should not be compiled without any warning or error, I think:


shared string[string] t;
void main() {
t[t] = bebebe;
}

But will.



Re: shared Variant[string]

2015-01-28 Thread zhmt via Digitalmars-d-learn
On Wednesday, 28 January 2015 at 10:32:56 UTC, Fyodor Ustinov 
wrote:

On Wednesday, 28 January 2015 at 10:20:42 UTC, zhmt wrote:


try __gshared


It seems to me - is not the solution, it's a hack. I want to 
understand what the happening. :)


I am new to D. But I that error yesterday, and it's solved in 
that way, I dont understand the reason clearly.


Re: shared Variant[string]

2015-01-28 Thread Kagamin via Digitalmars-d-learn
Associative array doesn't support thread-safe operations, that's 
why they don't work on shared instance. You should use 
std.concurrency or implement low-level concurrency mechanism.


Re: Using dub

2015-01-28 Thread Rikki Cattermole via Digitalmars-d-learn

On 28/01/2015 2:14 p.m., Joel wrote:

On Wednesday, 28 January 2015 at 00:34:13 UTC, Joel wrote:

On Tuesday, 27 January 2015 at 08:08:19 UTC, Joel wrote:

Oope, yeah, and it ran.


Thanks Rikki, I wiped off the dub installation. Now, no errors. The
small program worked too.


Actually I got this with dlangui, (I followed the instructions on the
announce post):

Joels-MacBook-Pro:dlangui joelcnz$ ../../dub run dlangui:example1
Failed to parse package description in
/Users/joelcnz/jpro/dpro2/OtherPeoples/dlangui
Failed to parse package description in
/Users/joelcnz/jpro/dpro2/OtherPeoples/dlangui
Error executing command run: Expected version number in version spec: *


Ehhh check dub version. * should work fine.


Re: Using dub

2015-01-28 Thread Rikki Cattermole via Digitalmars-d-learn

On 28/01/2015 1:34 p.m., Joel wrote:

On Tuesday, 27 January 2015 at 08:08:19 UTC, Joel wrote:

Oope, yeah, and it ran.


Thanks Rikki, I wiped off the dub installation. Now, no errors. The
small program worked too.

I don't now how to set up the dub executable to work with out doing
stuff like this - '../dub' (Mac OS 10.10.1)

Joels-MacBook-Pro:window joelcnz$ ../../dub build de_window:test
Building package de_window:test in
/Users/joelcnz/jpro/dpro2/OtherPeoples/window/
Fetching de_util 0.0.4 (getting selected version)...
Placing de_util 0.0.4 to /Users/joelcnz/.dub/packages/...
Fetching x11 1.0.5 (getting selected version)...
Placing x11 1.0.5 to /Users/joelcnz/.dub/packages/...
Fetching de_image 0.3.4 (getting selected version)...
Placing de_image 0.3.4 to /Users/joelcnz/.dub/packages/...
Fetching derelict-util 1.9.0 (getting selected version)...
Placing derelict-util 1.9.0 to /Users/joelcnz/.dub/packages/...
Fetching derelict-gl3 1.0.12 (getting selected version)...
Placing derelict-gl3 1.0.12 to /Users/joelcnz/.dub/packages/...
Building de_util:core 0.0.4 configuration library, build type debug.
Running dmd...
Building de_image:interfaces 0.3.4 configuration library, build type
debug.
Running dmd...
Building de_image:mutable 0.3.4 configuration library, build type debug.
Running dmd...
Building x11 1.0.5 configuration library, build type debug.
Running dmd...
Building de_window:interfaces 0.0.8 configuration library, build type
debug.
Running dmd...
Building derelict-util 1.9.0 configuration library, build type debug.
Running dmd...
Building derelict-gl3 1.0.12 configuration library, build type debug.
Running dmd...
Building de_window:test 0.0.8 configuration application, build type
debug.
Compiling using dmd...
Linking...
Joels-MacBook-Pro:window joelcnz$ ls
LICENSEdub.json
README.mddub.selections.json
WindowsAPIinterfaces
cocoa_librarylibde_window_interfaces.a
de_window_testplatforms
dubtest


When I setup dub/dmd on my OSX install, I used the OSX packages and it 
should already be on the PATH variable.


Re: Threads and stdio and HANDLE

2015-01-28 Thread Kagamin via Digitalmars-d-learn

On Wednesday, 28 January 2015 at 11:50:46 UTC, Danny wrote:
For Windows, if I use GetStdHandle, is the resulting HANDLE 
valid for threads other than the one that called GetStdHandle ? 
Because the HANDLE is a pointer but doesn't have shared. Does 
one know for Windows handles in general which are per-thread 
and which are per-process ?


There are no per-thread handles in windows. HANDLE was a pointer 
during DOS days, but its implementation changed to use indexes 
like on unix.


Re: Cached Incremental Updates of DUB Builds

2015-01-28 Thread data man via Digitalmars-d-learn

How you thought about merging Cook2 with DUB?


I'm not the author the Cook2. :)



Threads and stdio and HANDLE

2015-01-28 Thread Danny via Digitalmars-d-learn

Hello,

I'm trying to write some toy examples using threads in D.

Is the std.stdio.File thread-local or shared? Is flockfile used 
when I synchronize on it?


I tried checking phobos myself and found some things I don't get 
(in stdio.d):


alias FLOCK = flockfile;

this(this) { @trusted
  if(fps_)
FLOCK(fps_);
}

What is this(this)?

If I want to write to stdout from a thread, do I use 
LockingTextWriter? File? shared File? Does each thread have the 
same stdout? (Ok I checked, they have the same address, so 
probably. Phobos has it as __gshared stdout, aha)


Also, in order to avoid all that (also I want to be able to set 
Console text attributes on Windows), I tried to use the lowlevel 
I/O next:


For UNIX, the fds are per-process and just integers. So I know 
there that I can just pass around the int fd to any threads.


For Windows, if I use GetStdHandle, is the resulting HANDLE valid 
for threads other than the one that called GetStdHandle ? Because 
the HANDLE is a pointer but doesn't have shared. Does one know 
for Windows handles in general which are per-thread and which are 
per-process ?


Finally, I'm trying to come to grips with shared:

The first use of shared is to signal to the compiler that it 
should not store the variable in thread-local storage. But when I 
acquire a lock (using synchronized, say), I'm supposed to cast 
away the shared, right? Does it then still know that it's not 
thread-local (but that I ensured that nobody else accesses it for 
the time being)?


What does specifying shared class or shared struct do?


Does anybody work on std.net.curl?

2015-01-28 Thread Suliman via Digitalmars-d-learn

Just interesting is there any plans to replace net.curl on native
lib? I mean not plans for long future, but some stuff on which
somebody active work?


Re: shared Variant[string]

2015-01-28 Thread Fyodor Ustinov via Digitalmars-d-learn
On Wednesday, 28 January 2015 at 12:32:29 UTC, Tobias Pankrath 
wrote:
In your case above, it's std.variant : Variant, which does not 
work when shared.


I look into variant.d and after seeing so many lines @@@ BUG - 
strange that it somehow works. :)


Re: Cached Incremental Updates of DUB Builds

2015-01-28 Thread Nordlöw

On Wednesday, 28 January 2015 at 01:53:43 UTC, data man wrote:

Try it: https://github.com/gecko0307/Cook2



Looks nice...I'll try it...but I still want integration with 
DUB's other feature to automatically download external packages 
from github.


How you thought about merging Cook2 with DUB? I think the DUB 
developers would be more than happy to get these cool features in 
DUB.


Re: How to copy object of class A to another object of class B?

2015-01-28 Thread via Digitalmars-d-learn

On Wednesday, 28 January 2015 at 09:44:29 UTC, zhmt wrote:
It is boring coding, I want a solution to copy them 
automatically:

void copyObj(SRC,DEST)(SRC src,DEST dest)
{
foreach (i, type; typeof(SRC.tupleof)) {
auto name = SRC.tupleof[i].stringof;
		__traits(getMember, dest, name) =  __traits(getMember, src, 
name);

writeln(name);
}
}

Unfortunitely, it doesnt work,  how to improve it?


Haven't tested it, but the `auto name = ...` part is likely to be 
the problem. By using `auto`, your declaring a runtime variable, 
which you then later try to use with `__traits(getMember, ...)`, 
which expects a value known at compile time. Try using `alias 
name = ...`, or if that fails, just repeat the expression 
`SRC.tupleof[i].stringof` wherever `name` occurs (though I'm sure 
there is a nicer way).


Re: How to copy object of class A to another object of class B?

2015-01-28 Thread zhmt via Digitalmars-d-learn

The final version works well:

void copyObj(SRC,DEST)(ref SRC src,ref DEST dest)
{
foreach (i, type; typeof(SRC.tupleof)) {
		__traits(getMember, dest, SRC.tupleof[i].stringof) =  
__traits(getMember, src, SRC.tupleof[i].stringof);

}
}

thank u , @Marc Schütz .


Re: How to copy object of class A to another object of class B?

2015-01-28 Thread Baz via Digitalmars-d-learn

On Wednesday, 28 January 2015 at 14:35:58 UTC, zhmt wrote:

Anybody help?


__Traits functions are evaluated at compile time so it's not as 
simple.
The best you can do is to generate a string to mixin based on the 
aggragate members. here i have an example of how you can iterate 
through the members: 
https://github.com/BBasile/Iz/blob/master/import/iz/traits.d#L18


Writing an opAssign() operator will be as fast, you just need to 
remember to keep it in sync with the classe members.


You could also generate the struct programmatically, based on the 
classes declaration.


Re: How to copy object of class A to another object of class B?

2015-01-28 Thread Chris Williams via Digitalmars-d-learn

On Wednesday, 28 January 2015 at 09:44:29 UTC, zhmt wrote:

Sometime , I need to copy them:

thrift.Card tc;

db.Card dc;

dc.id = tc.id;
dc.pwd = tc.pwd;
...


It is boring coding, I want a solution to copy them 
automatically:

void copyObj(SRC,DEST)(SRC src,DEST dest)
{
foreach (i, type; typeof(SRC.tupleof)) {
auto name = SRC.tupleof[i].stringof;
		__traits(getMember, dest, name) =  __traits(getMember, src, 
name);

writeln(name);
}
}

Unfortunitely, it doesnt work,  how to improve it?


Assuming that the hibernated class isn't auto-generated and you 
can redefine its contents freely, the following style may be an 
alternative that works for you:


struct Foo {
public:
string a;
int b;
}

class FooClass {
public:
union {
struct {
string a;
int b;
};
Foo foo;
}

}

void main() {
Foo f = Foo(a, 10);
FooClass c = new FooClass();
c.foo = f;

writefln(%s %s, c.a, c.b);
}

Probably the anonymous struct will break the UDAs, but it should 
be worth testing.


Re: Using std.net.curl to stream data

2015-01-28 Thread Chris Williams via Digitalmars-d-learn

On Wednesday, 28 January 2015 at 14:18:38 UTC, Trollgeir wrote:
I'm having some trouble trying to stream data to my plot.ly 
graph:

https://plot.ly/62/~Trollgeir/

The API:  https://plot.ly/streaming/

I am able to post messages that get recorded into the stream 
live, although right after curl uploads it, it just seems to 
wait for a response it's not getting, and eventually timeouts. 
Does anyone have any advice?


auto client = HTTP(stream.plot.ly);
client.addRequestHeader(plotly-streamtoken,e8bg6omat6);
client.verbose = true;

string msg = { \x\: 500, \y\: 500 } \n;
client.postData(msg);
client.perform; 


You have to define a handler for HTTP.onReceive before calling 
HTTP.perform. It will receive ubyte arrays for each packet that 
comes in. For most purposes, you just copy that onto the end of a 
string in an external scope. But if you're streaming content, 
you'll need to do something more fancy.


Re: Print to Win Printer

2015-01-28 Thread Paul via Digitalmars-d-learn

Thanks Vladimir.



Re: Using dub

2015-01-28 Thread Joel via Digitalmars-d-learn
On Wednesday, 28 January 2015 at 23:48:52 UTC, Rikki Cattermole 
wrote:

On 29/01/2015 11:27 a.m., Joel wrote:


When I setup dub/dmd on my OSX install, I used the OSX 
packages and it

should already be on the PATH variable.


What packages? I'm new to Mac OS.

Ohh, I was meaning a dmg.
But ugh looks like I lied, my bad, only had to do it once.
Looks like I used brew[0].
Just install dub, dmd will go along right with it as a 
dependency.


[0] http://brew.sh/


Do I just put 'brew dub'?


Re: how convert the range to slice ?

2015-01-28 Thread Nordlöw

On Wednesday, 28 January 2015 at 23:15:32 UTC, bearophile wrote:

I think you can try to open a diagnostic enhancement request.


Nice, that's what I'd hoped for you'd say :)

I'll dig into it later on...


Re: Using dub

2015-01-28 Thread Rikki Cattermole via Digitalmars-d-learn

On 29/01/2015 11:27 a.m., Joel wrote:


When I setup dub/dmd on my OSX install, I used the OSX packages and it
should already be on the PATH variable.


What packages? I'm new to Mac OS.

Ohh, I was meaning a dmg.
But ugh looks like I lied, my bad, only had to do it once.
Looks like I used brew[0].
Just install dub, dmd will go along right with it as a dependency.

[0] http://brew.sh/


Re: how convert the range to slice ?

2015-01-28 Thread bearophile via Digitalmars-d-learn

Chris Williams:

Range is not castable to array. See std.array.array to generate 
an array from a Range.



Currently this program:

void main() {
import std.range;
int[] a = iota(10);
}


Gives an error like:

test.d(3,19): Error: cannot implicitly convert expression 
(iota(10)) of type Result to int[]



For the error message to be like yours, the compiler has to 
recognize a Range, this could be possible because foreach() 
already does that.


I think you can try to open a diagnostic enhancement request.

In D there are already examples of hardcoded error messages 
targeting newbies:


void main() {
int x;
writeln(a);
printf(%d\n, x);
}


Gives:

test.d(3,5): Error: 'writeln' is not defined, perhaps you need to 
import std.stdio; ?
test.d(4,5): Error: 'printf' is not defined, perhaps you need to 
import core.stdc.stdio; ?


Bye,
bearophile


Re: how convert the range to slice ?

2015-01-28 Thread Nordlöw

On Wednesday, 28 January 2015 at 22:43:36 UTC, bearophile wrote:
It's such a fundamental part of D+Phobos that newbies are 
forced to learn this quickly. On the other hand an informative 
error message could be useful...


What error message do you suggest?


Something like:

..., expression of type (SomeRange!T) must be 
converted/transformed to T[] through .array.


I have no idea if DMD could figure this out by intercepting array 
assignment somehow. DMD must at least be aware of the Range 
concept throught its duck type members when generating code for 
foreach, right?


Re: Using dub

2015-01-28 Thread Rikki Cattermole via Digitalmars-d-learn

On 29/01/2015 1:24 p.m., Joel wrote:

On Wednesday, 28 January 2015 at 23:48:52 UTC, Rikki Cattermole wrote:

On 29/01/2015 11:27 a.m., Joel wrote:


When I setup dub/dmd on my OSX install, I used the OSX packages and it
should already be on the PATH variable.


What packages? I'm new to Mac OS.

Ohh, I was meaning a dmg.
But ugh looks like I lied, my bad, only had to do it once.
Looks like I used brew[0].
Just install dub, dmd will go along right with it as a dependency.

[0] http://brew.sh/


Do I just put 'brew dub'?


First install brew then
$ brew install dub

From that it should just be dub to run.
Don't forget to restart terminal afterwards or just rerun bash.
If dub isn't found, PATH variable will need to be changed via .bashrc. 
But I'm doubting its needed to be done.


Re: how convert the range to slice ?

2015-01-28 Thread data man via Digitalmars-d-learn

On Wednesday, 28 January 2015 at 14:32:38 UTC, mzf wrote:

is there any simple way to convert?

int [] arr1 = [1,2,3].map!a*2;   //compile error
int [] arr2 = [1,2,3].filter!a3;//compile error


auto arr1 = [1,2,3].map!a*2.array;
auto arr2 = [1,2,3].filter!a3.array;


Re: How to copy object of class A to another object of class B?

2015-01-28 Thread Ali Çehreli via Digitalmars-d-learn

name must be 'enum':

On 01/28/2015 06:34 AM, zhmt wrote:

void getT(SRC,DEST)(SRC src,DEST dest)
{
 foreach (i, type; typeof(SRC.tupleof)) {
 string name = SRC.tupleof[i].stringof;


enum name = SRC.tupleof[i].stringof;


 __traits(getMember, dest, name) =  __traits(getMember, src, name);
  writeln(name);
 }
}

when I write the code above, the compile complains that:

source/app.d(14): Error: variable name cannot be read at compile time.


Ali



Re: How to copy object of class A to another object of class B?

2015-01-28 Thread aldanor via Digitalmars-d-learn

On Wednesday, 28 January 2015 at 11:30:13 UTC, Marc Schütz wrote:

On Wednesday, 28 January 2015 at 09:44:29 UTC, zhmt wrote:
It is boring coding, I want a solution to copy them 
automatically:

void copyObj(SRC,DEST)(SRC src,DEST dest)
{
foreach (i, type; typeof(SRC.tupleof)) {
auto name = SRC.tupleof[i].stringof;
		__traits(getMember, dest, name) =  __traits(getMember, src, 
name);

writeln(name);
}
}

Unfortunitely, it doesnt work,  how to improve it?


Haven't tested it, but the `auto name = ...` part is likely to 
be the problem. By using `auto`, your declaring a runtime 
variable, which you then later try to use with 
`__traits(getMember, ...)`, which expects a value known at 
compile time. Try using `alias name = ...`, or if that fails, 
just repeat the expression `SRC.tupleof[i].stringof` wherever 
`name` occurs (though I'm sure there is a nicer way).


And if the alias doesn't work directly, you can always use a 
well-known hack:


alias Alias(T) = T;
alias Alias(alias T) = T;

so then this works:

alias member = Alias!(__traits(getMember, Parent, child));

Idk if it's a feature or a bug of how getMember works but I had 
to use this numerous times.


Re: How to copy object of class A to another object of class B?

2015-01-28 Thread zhmt via Digitalmars-d-learn

Thx very much for all the help, I will try.


Re: How to copy object of class A to another object of class B?

2015-01-28 Thread Artur Skawina via Digitalmars-d-learn
On 01/28/15 10:44, zhmt via Digitalmars-d-learn wrote:
 I have a struct created by thrift:
 
 struct Card {
   long id;
   string pwd;
   long agentId;
   bool valid;
   long rmb;
   long createDate;
   long soldDate;
   long chargeDate;
 
   mixin TStructHelpers!([
 TFieldMeta(`id`, 1, TReq.OPTIONAL),
 TFieldMeta(`pwd`, 2, TReq.OPTIONAL),
 TFieldMeta(`agentId`, 3, TReq.OPTIONAL),
 TFieldMeta(`valid`, 4, TReq.OPTIONAL),
 TFieldMeta(`rmb`, 5, TReq.OPTIONAL),
 TFieldMeta(`createDate`, 6, TReq.OPTIONAL),
 TFieldMeta(`soldDate`, 7, TReq.OPTIONAL),
 TFieldMeta(`chargeDate`, 8, TReq.OPTIONAL)
   ]);
 }
 
 and another class created for hibernated:
 
 class Card
 {
 import hibernated.core;
 
 @Id
 @Generated
 long id;
 @UniqueKey
 string pwd;
 //index
 long agentId;
 bool valid;
 long rmb;
 long createDate;
 long soldDate;
 long chargeDate;
 }
 
 
 Sometime , I need to copy them:
 
 thrift.Card tc;
 
 db.Card dc;
 
 dc.id = tc.id;
 dc.pwd = tc.pwd;
 ...
 
 
 It is boring coding, I want a solution to copy them automatically:

You could just add a method to the db class:

void fields(B)(auto ref B b) @property {
  foreach (I, _; typeof(this.tupleof))
 this.tupleof[I] = mixin(`b.`~__traits(identifier, this.tupleof[I]));
}

then

   dc.fields = tc;

will work.

artur


Re: how convert the range to slice ?

2015-01-28 Thread bearophile via Digitalmars-d-learn

Nordlöw:

Is there any chance we could add logic to dmd+phobos that hints 
user about this?


It's such a fundamental part of D+Phobos that newbies are forced 
to learn this quickly. On the other hand an informative error 
message could be useful...


What error message do you suggest?

Bye,
bearophile


Re: Using dub

2015-01-28 Thread Joel via Digitalmars-d-learn


When I setup dub/dmd on my OSX install, I used the OSX packages 
and it should already be on the PATH variable.


What packages? I'm new to Mac OS.


Re: how convert the range to slice ?

2015-01-28 Thread Nordlöw

On Wednesday, 28 January 2015 at 14:54:27 UTC, data man wrote:

auto arr1 = [1,2,3].map!a*2.array;
auto arr2 = [1,2,3].filter!a3.array;


Is there any chance we could add logic to dmd+phobos that hints 
user about this?


Re: Cached Incremental Updates of DUB Builds

2015-01-28 Thread Nordlöw

On Wednesday, 28 January 2015 at 01:39:21 UTC, Nordlöw wrote:
Ping. Why no answers? Should i post this as a DUB issue on 
github instead?


I made it an issue here:

https://github.com/D-Programming-Language/dub/issues/498


Re: how convert the range to slice ?

2015-01-28 Thread Chris Williams via Digitalmars-d-learn

On Wednesday, 28 January 2015 at 22:43:36 UTC, bearophile wrote:

Nordlöw:

Is there any chance we could add logic to dmd+phobos that 
hints user about this?


It's such a fundamental part of D+Phobos that newbies are 
forced to learn this quickly. On the other hand an informative 
error message could be useful...


What error message do you suggest?

Bye,
bearophile


Range is not castable to array. See std.array.array to generate 
an array from a Range.


Re: Using dub

2015-01-28 Thread Joel via Digitalmars-d-learn


[0] http://brew.sh/


Do I just put 'brew dub'?


First install brew then
$ brew install dub

From that it should just be dub to run.
Don't forget to restart terminal afterwards or just rerun bash.
If dub isn't found, PATH variable will need to be changed via 
.bashrc. But I'm doubting its needed to be done.


Yay! Seems to be working well! Though, I didn't rerun the 
terminal or any thing.


Thanks Rikki!


Re: shared Variant[string]

2015-01-28 Thread Meta via Digitalmars-d-learn
On Wednesday, 28 January 2015 at 12:46:20 UTC, Fyodor Ustinov 
wrote:
On Wednesday, 28 January 2015 at 12:32:29 UTC, Tobias Pankrath 
wrote:
In your case above, it's std.variant : Variant, which does not 
work when shared.


I look into variant.d and after seeing so many lines @@@ BUG 
- strange that it somehow works. :)


AAs are a messy part of the language right now due to how they're 
implemented. A new implementation is being worked on but it's a 
tricky thing to fix.


What is @return?

2015-01-28 Thread Jesse Phillips via Digitalmars-d-learn

A recent discussion over in digitalmars.D
http://forum.dlang.org/post/rtwbtxigfeupvykpb...@forum.dlang.org

talks about accepting @return... Here is an example from the PR:
https://github.com/D-Programming-Language/dmd/compare/366422338ece...6b86b12f79e8

struct At
{
 @property auto info() @safe @nothrow @pure @return const { 
return this; }


 @pure @nothrow @return ref int info2(ref int x) { return x; }
}

Which I assume before the change would be:

struct At
{
 @property auto info() @safe nothrow pure return const { return 
this; }


 pure nothrow return ref int info2(ref int x) { return x; }
}

I don't know about anyone else, but this doesn't compile in 2.066.


Re: What is @return?

2015-01-28 Thread ketmar via Digitalmars-d-learn
On Thu, 29 Jan 2015 04:54:38 +, Jesse Phillips wrote:

 A recent discussion over in digitalmars.D
 http://forum.dlang.org/post/rtwbtxigfeupvykpb...@forum.dlang.org
 
 talks about accepting @return... Here is an example from the PR:
 https://github.com/D-Programming-Language/dmd/
compare/366422338ece...6b86b12f79e8
 
 struct At {
   @property auto info() @safe @nothrow @pure @return const {
 return this; }
 
   @pure @nothrow @return ref int info2(ref int x) { return x; }
 }
 
 Which I assume before the change would be:
 
 struct At {
   @property auto info() @safe nothrow pure return const { return
 this; }
 
   pure nothrow return ref int info2(ref int x) { return x; }
 }
 
 I don't know about anyone else, but this doesn't compile in 2.066.

http://wiki.dlang.org/DIP25
this is a very recent thing, it wasn't coded when 2.066 was released.

signature.asc
Description: PGP signature


Import paths do not work

2015-01-28 Thread tcak via Digitalmars-d-learn
I have a library that has many folders and D files in them. I do 
not want to list name of all module files one by one while 
compiling projects. So, I thought I could use -I flag while 
compiling. It says:


-Ipath
where to look for imports

So, I made a test as follows:


./test.d
==
import inc.blah;

void main(){
inc.blah.a = 5;
}


./inc/blah.d
==
module inc.blah;

public int a;


./makefile
==
all:
dmd -I./inc/ test.d


When I do make, result is as follows:

dmd -I./inc/ test.d
test.o: In function `_Dmain':
test.d:(.text._Dmain+0x10): undefined reference to 
`_D3inc4blah1ai'



Do I understand wrong how that -I flag works?


Re: How to copy object of class A to another object of class B?

2015-01-28 Thread zhmt via Digitalmars-d-learn

On Wednesday, 28 January 2015 at 14:59:52 UTC, Ali Çehreli wrote:

name must be 'enum':

On 01/28/2015 06:34 AM, zhmt wrote:

void getT(SRC,DEST)(SRC src,DEST dest)
{
foreach (i, type; typeof(SRC.tupleof)) {
string name = SRC.tupleof[i].stringof;


enum name = SRC.tupleof[i].stringof;

__traits(getMember, dest, name) =  __traits(getMember, 
src, name);

 writeln(name);
}
}

when I write the code above, the compile complains that:

source/app.d(14): Error: variable name cannot be read at 
compile time.


Ali


@Ali, I have test the enum name declaration, it works well, Thx 
for your help.


Re: What is @return?

2015-01-28 Thread Jonathan M Davis via Digitalmars-d-learn
On Thursday, January 29, 2015 05:02:58 ketmar via Digitalmars-d-learn wrote:
 http://wiki.dlang.org/DIP25
 this is a very recent thing, it wasn't coded when 2.066 was released.

I don't know if it's even fully coded up in git master, though clearly it's
at least partially there, because @ was put on return as part of the PR that
triggered that discussion. Regardless, it's _very_ new (IIRC, it's been less
than a month since it was approved).

- Jonathan M Davis



Re: How to copy object of class A to another object of class B?

2015-01-28 Thread zhmt via Digitalmars-d-learn
On Wednesday, 28 January 2015 at 23:34:10 UTC, Chris Williams 
wrote:

On Wednesday, 28 January 2015 at 09:44:29 UTC, zhmt wrote:

Sometime , I need to copy them:

thrift.Card tc;

db.Card dc;

dc.id = tc.id;
dc.pwd = tc.pwd;
...


It is boring coding, I want a solution to copy them 
automatically:

void copyObj(SRC,DEST)(SRC src,DEST dest)
{
foreach (i, type; typeof(SRC.tupleof)) {
auto name = SRC.tupleof[i].stringof;
		__traits(getMember, dest, name) =  __traits(getMember, src, 
name);

writeln(name);
}
}

Unfortunitely, it doesnt work,  how to improve it?


Assuming that the hibernated class isn't auto-generated and you 
can redefine its contents freely, the following style may be an 
alternative that works for you:


struct Foo {
public:
string a;
int b;
}

class FooClass {
public:
union {
struct {
string a;
int b;
};
Foo foo;
}

}

void main() {
Foo f = Foo(a, 10);
FooClass c = new FooClass();
c.foo = f;

writefln(%s %s, c.a, c.b);
}

Probably the anonymous struct will break the UDAs, but it 
should be worth testing.


The hibernated class is not auto-generated yet. I think this is a 
good idea too.




Re: Import paths do not work

2015-01-28 Thread Rikki Cattermole via Digitalmars-d-learn

On 29/01/2015 8:08 p.m., tcak wrote:

I have a library that has many folders and D files in them. I do not
want to list name of all module files one by one while compiling
projects. So, I thought I could use -I flag while compiling. It says:

-Ipath
 where to look for imports

So, I made a test as follows:


./test.d
==
import inc.blah;

void main(){
 inc.blah.a = 5;
}


./inc/blah.d
==
module inc.blah;

public int a;


./makefile
==
all:
 dmd -I./inc/ test.d


When I do make, result is as follows:

dmd -I./inc/ test.d
test.o: In function `_Dmain':
test.d:(.text._Dmain+0x10): undefined reference to `_D3inc4blah1ai'


Do I understand wrong how that -I flag works?


Basically during linking not all symbols used is passed in.
You are doing a single compile + link.
So what -I does is tell the compiler to look for definitions in files on 
the paths specified. But does not compile them into the binary output.


I would suggest instead of using make, use dub[0] build manager instead.
It'll handle grabbing all the files and compiling them correctly.

[0] http://code.dlang.org/package-format