Storing arbitrary type info in XML

2012-05-07 Thread F i L
I'm trying to store arbitrary D object information in an XML 
file, then load and create the objects it represents 
programmatically. For example, I have the file test.xml, which 
looks like:




  
  
  


and corresponds to the D file:

// main.d
import std.conv, std.stdio;
import std.file, std.xml;

class Person {
  string name;
  uint age;
}

void main() {
  // 1. read XML file, create Document
  // 2. create objects named in Document
  // 3. set objects data based on attributes
}

currently I'm accomplishing this by using Object.factory() and 
casting it to Person to see if it's a person; then run each XML 
attribute through a switch statement which sets the Person fields 
accordingly.


I can generate all that, which makes it a bit more dynamic, but 
is that really the best way to go about this? It seems too rigid 
for my tastes (can't create structs) and, when lots of objects 
are potentially represented, slow as well.



I was thinking of using generated D files instead of XML which 
was an interesting concept except a bit limiting in other ways.


Does anyone have any thoughts on any of this. I'd love to know 
them if you do :)


Re: undefined reference - Derelict2

2012-05-07 Thread Mike Parker

On 5/7/2012 8:53 PM, sergeiV wrote:

rdmd -Iinclude/Derelict2/import -L-Linclude/Derelict2/lib
-L-lDerelictAL -L-lDerelictUtil -L-ldl test.d

Thnx, this works.

Though I was under the impression that the new Derelict2 hierarchy / d
interface files in Derelict2/import reduced the neccesity for command
line arguments, that is a Derelict related question.


Well, it does. With all of the imports in a single directory, you don't 
need to specify multiple import directories. But you will always need to 
link with the libraries. The import directory doesn't affect that.


Re: simple "find" question

2012-05-07 Thread WhatMeWorry

On Monday, 7 May 2012 at 19:45:36 UTC, WhatMeWorry wrote:

I get a comiler error for some older code which uses regexp.find

if (std.regexp.find(pText, lMacro.Pattern, "m") != -1)

Ok. So I look at the phobos docs and find:

deprecated sizediff_t find(string s, string pattern, string
attributes = null); Returns:

Same as find(s, RegExp(pattern, attributes)).

WARNING:
   This function is scheduled for deprecation due to unnecessary
ambiguity with the homonym function in std.string. Instead of
std.regexp.find(s, p, a), you may want to use find(s, RegExp(p,
a)).

But I can't find "find(s, RegExp(p, a))".  There is an entry in
std.algorithm but does a haystack and needle coorespond to a
string and regular expression.

And std.regex talks about "matches" and stuff but is this
equivalent to "find".





Think I found it. CountUntil gives me a index.

assert(countUntil("hello world", "world") == 6);

Will study more, and post less.


Re: ptrace (process trace system call) on Linux from D

2012-05-07 Thread mta`chrono
> (2) Is it more D-ish to use something like "enum PTRequest { traceMe =
> 0, ... }" instead of the fully capitalised originals?  It doesn't seem
> to affect the working of things.

Yes, that's more D-ish. In new D code I prefer the camelcased ones, but
there are cases where you'd like to use the original ones. it's up to! ;-)

> (3) wait() is declared as returning a pid_t in the manpage, and is
> declared as such in core.sys.posix.sys.wait.  In std.c.process however,
> it returns an int.  I can't even find a process.h in my Linux install. 
> What am I supposed to use?  I know it essentially doesn't matter, as
> pid_t is aliased as int.
> Are there D alternatives to wait() and fork() that I could use with
> ptrace() instead of the C posix system functions?
> 
> (4) Some things are declared only in core.*, not in std.*.  Will they be
> added at some point or is this how it's supposed to be?


I thing core.* (druntime) is the right place for _all_ bindings to libc.
But yeha, there are still some relicts in std.c.* of the good old days.

What do you meant by "D alternatives to wait() and fork() that could be
used with ptrace()". Maybe I don't understand your intention.


Re: Ref / NotNull struct

2012-05-07 Thread Chris Cain

On Monday, 7 May 2012 at 19:36:52 UTC, Namespace wrote:

But apart from that, nor anything else that does not work? :)


I put together a few toy examples with it and it seems to work in 
general otherwise. It might be better if someone has a larger 
project that they wanted a non-null reference for to get a lot of 
code tested all at once. Overall, I'm pretty happy with it as 
long as I don't use UFCS with it.


Again, thanks for your efforts developing it :)


simple "find" question

2012-05-07 Thread WhatMeWorry

I get a comiler error for some older code which uses regexp.find

if (std.regexp.find(pText, lMacro.Pattern, "m") != -1)

Ok. So I look at the phobos docs and find:

deprecated sizediff_t find(string s, string pattern, string
attributes = null); Returns:

Same as find(s, RegExp(pattern, attributes)).

WARNING:
   This function is scheduled for deprecation due to unnecessary
ambiguity with the homonym function in std.string. Instead of
std.regexp.find(s, p, a), you may want to use find(s, RegExp(p,
a)).

But I can't find "find(s, RegExp(p, a))".  There is an entry in
std.algorithm but does a haystack and needle coorespond to a
string and regular expression.

And std.regex talks about "matches" and stuff but is this
equivalent to "find".


Re: Ref / NotNull struct

2012-05-07 Thread Namespace

On Monday, 7 May 2012 at 18:46:55 UTC, Chris Cain wrote:
OK, I tried a few things, but something that I think might need 
some attention: UFCS is very problematic with this.


https://gist.github.com/6198938b9c6d40a53ca1


But apart from that, nor anything else that does not work? :)


Re: Ref / NotNull struct

2012-05-07 Thread Namespace

On Monday, 7 May 2012 at 18:46:55 UTC, Chris Cain wrote:
OK, I tried a few things, but something that I think might need 
some attention: UFCS is very problematic with this.


https://gist.github.com/6198938b9c6d40a53ca1


I see. I think that is the same problem as "alias this". The 
compiler doesn't know what he has to do. I hope that 
functionality like this and the conversion from Foo to Ref!Foo 
and in reverse works in the next version. But I don't know how i 
can solve the problem. :/


Re: Ref / NotNull struct

2012-05-07 Thread Chris Cain
OK, I tried a few things, but something that I think might need 
some attention: UFCS is very problematic with this.


https://gist.github.com/6198938b9c6d40a53ca1


Re: Clunky syntax

2012-05-07 Thread Chris Cain

On Monday, 7 May 2012 at 17:52:01 UTC, ixid wrote:
Thank you, could you explain what is happening in your example? 
Bar is inheriting from Foo, what are you getting when you 
create a parent of type sub-class compared  to Bar b = new Bar; 
and Foo b = new Foo; ? Foo b = new Bar won't compile if you add 
members to Bar and access them.


Hello,

Foo is the "interface" you'll have to bar.

So, a bit of a bigger example:


-=-=-=-
import std.stdio;
class Foo {
void doStuff() {}
}
class Bar : Foo {
void doStuff() {
writeln("Hi");
}
void doThings() { }
}

void main() {
Foo f = new Bar;
f.doStuff(); // prints "Hi" to the screen
f.doThings(); // doesn't compile
}
-=-=-=-

So, as you can see, if you have a Foo, you can't call "doThings" 
using it. However, if your Foo is actually a Bar underneath, then 
it'll use Bar's version of "doStuff".


OOP isn't terribly hard, but I suggest reading up on it some to 
grasp the concepts (and especially so you can see the benefits).


Here's a link that might help you get started on some of the 
fundamentals:

http://en.wikipedia.org/wiki/Object-oriented_programming#Fundamental_features_and_concepts


Re: Clunky syntax

2012-05-07 Thread ixid

class Foo {}
class Bar : Foo {}
void main() {
   Foo b = new Bar;
}


Thank you, could you explain what is happening in your example? 
Bar is inheriting from Foo, what are you getting when you create 
a parent of type sub-class compared  to Bar b = new Bar; and Foo 
b = new Foo; ? Foo b = new Bar won't compile if you add members 
to Bar and access them.






Re: Clunky syntax

2012-05-07 Thread bearophile

ixid:


Why is the new syntax so clunky?

thing myThing = new thing;

When it could be:

new thing myThing;


Sometimes you want to write:

class Foo {}
class Bar : Foo {}
void main() {
   Foo b = new Bar;
}

Otherwise there is 'auto' too:

auto myThing = new thing;

Bye,
bearophile


Clunky syntax

2012-05-07 Thread ixid

Why is the new syntax so clunky?

thing myThing = new thing;

When it could be:

new thing myThing;


Re: Ref / NotNull struct

2012-05-07 Thread Namespace
I think it's great! I'm planning on trying it out on a few 
things and I'll let you know if I have any suggestions. I 
_almost_ suggested Proxy earlier, but I had never used it and I 
wasn't sure whether it was appropriate for the problem you were 
having.


I would be glad if you will do this. :)

Overall, I think a lot of people will get a lot of use out of 
it. It's something I know people have been looking for, but I 
think you'll have to save the link and when someone asks/talks 
about NonNull references/pointers you can refer them here.


That's a good idea.


Re: Ref / NotNull struct

2012-05-07 Thread Chris Cain

On Monday, 7 May 2012 at 10:37:57 UTC, Namespace wrote:

I hoped for a little more interest/suggestions/criticism.
Particularly for the missed implementation of "Proxy".


I think it's great! I'm planning on trying it out on a few things 
and I'll let you know if I have any suggestions. I _almost_ 
suggested Proxy earlier, but I had never used it and I wasn't 
sure whether it was appropriate for the problem you were having.


Overall, I think a lot of people will get a lot of use out of it. 
It's something I know people have been looking for, but I think 
you'll have to save the link and when someone asks/talks about 
NonNull references/pointers you can refer them here.


Re: ref semantics with hashes

2012-05-07 Thread Steven Schveighoffer

On Sat, 05 May 2012 00:11:04 -0400, Kagamin  wrote:


Well, if one needs strictly ref dictionary
class Dictionary(TKey, TValue)
{
   TValue[TKey] dictionary;
   alias dictionary this;
}


This is a wasted memory block.  dictionary already has reference semantics  
*as long as* it has been initialized.


The problem is, the only way to initialize it is to add something.  There  
is no current way to create an initialized empty AA.


-Steve


Re: undefined reference - Derelict2

2012-05-07 Thread Trass3r
Though I was under the impression that the new Derelict2 hierarchy / d  
interface files in Derelict2/import reduced the neccesity for command  
line arguments, that is a Derelict related question.


You may try to use --chatty and compare the output.


Re: ptrace (process trace system call) on Linux from D

2012-05-07 Thread Stanislav Blinov

On Monday, 7 May 2012 at 11:30:33 UTC, Artur Skawina wrote:
struct method names are mangled, including static and extern(C) 
ones.
(they have to be, or you'd get collisions; a way to turn it off 
for

cases like this one would be useful, yes.)


Hmm, that's right. Silly me. Sorry for the confusion.



Re: undefined reference - Derelict2

2012-05-07 Thread sergeiV
rdmd -Iinclude/Derelict2/import -L-Linclude/Derelict2/lib 
-L-lDerelictAL -L-lDerelictUtil -L-ldl test.d

Thnx, this works.

Though I was under the impression that the new Derelict2 
hierarchy / d interface files in Derelict2/import reduced the 
neccesity for command line arguments, that is a Derelict related 
question.


Condsider this solved.


Re: ptrace (process trace system call) on Linux from D

2012-05-07 Thread Artur Skawina
On 05/07/12 12:45, Stanislav Blinov wrote:
> On Friday, 4 May 2012 at 14:47:05 UTC, Matej Nanut wrote:
>> (1) I've managed this by putting the extern ptrace declaration in a seperate 
>> file and call it via filename.ptrace in my program.
>>
> 
> You can achieve the same without additional files:
> 
> // This struct acts as a namespace to hide C declarations,
> // just like separate module
> struct PtraceApi {
> static:
> 
> extern(C) long ptrace(__ptrace_request request, pid_t pid, void *addr,
> void *data);
> 
> }
> 
> long ptrace(__ptrace_request request, pid_t pid, void *addr,
> void *data) {
> auto result = PtraceApi.ptrace(request, pid, addr, data);
> // Check result and errno for errors...
> }
> 

struct method names are mangled, including static and extern(C) ones.
(they have to be, or you'd get collisions; a way to turn it off for
cases like this one would be useful, yes.)

artur


Re: ptrace (process trace system call) on Linux from D

2012-05-07 Thread Stanislav Blinov

On Friday, 4 May 2012 at 14:47:05 UTC, Matej Nanut wrote:
(1) I've managed this by putting the extern ptrace declaration 
in a seperate file and call it via filename.ptrace in my 
program.




You can achieve the same without additional files:

// This struct acts as a namespace to hide C declarations,
// just like separate module
struct PtraceApi {
static:

extern(C) long ptrace(__ptrace_request request, pid_t pid, 
void *addr,

void *data);

}

long ptrace(__ptrace_request request, pid_t pid, void *addr,
void *data) {
auto result = PtraceApi.ptrace(request, pid, addr, data);
// Check result and errno for errors...
}


Re: Ref / NotNull struct

2012-05-07 Thread Namespace

On Sunday, 6 May 2012 at 17:58:22 UTC, Namespace wrote:

It seems that "inout" isn't enough to fix the problem.

So i extend the template to this:
http://codepad.org/SwBPoVM2

I wrote a copy of the function and now there is one with and one
without const. I thought inout would do the job alone but it
seems that it doesn't.

Now all of my code works as i expected. But because of my bad
english i would prefer that one of you report the bug and the 
fix.


I hoped for a little more interest/suggestions/criticism.
Particularly for the missed implementation of "Proxy".


Re: problem parsing xml (std.xml)

2012-05-07 Thread Stanislav Blinov
Just a guess, but wouldn't  be the root node of the 
document, thus not qualifying for the onStartTag?


Re: undefined reference - Derelict2

2012-05-07 Thread Stanislav Blinov

On Sunday, 6 May 2012 at 01:23:48 UTC, sergeiV wrote:


Solved, missed -of:
rdmd -Iinclude/Derelict2/import/
-Linclude/Derelict2/lib/libDerelictAL.a
-Linclude/Derelict2/lib/libDerelictUtil.a -ofderelict_test 
test.d


This is on Linux? Shouldn't it be something like

rdmd -Iinclude/Derelict2/import -L-Linclude/Derelict2/lib 
-L-lDerelictAL -L-lDerelictUtil -L-ldl test.d


?