Re: No implicitly convert derived ptr to base ptr?

2012-04-25 Thread Jonathan M Davis
On Wednesday, April 25, 2012 01:50:51 Nick Sabalausky wrote:
 The compiler rejects this:
 
 class Base {}
 class Derived : Base {}
 
 void main()
 {
 Base*basePtr;
 Derived* derivedPtr;
 
 basePtr = derivedPtr; // ERROR
 }
 
 Is that really correct that it shouldn't be allowed?

Well, given that pointers aren't polymorphic at all, it's not all that great 
an idea in general to assign a derived class object to a base class pointer, 
so it's arguably a good thing that it doesn't work, but I am a bit surprised 
that it doesn't work.

- Jonathan M Davis


Re: No implicitly convert derived ptr to base ptr?

2012-04-25 Thread Timon Gehr

On 04/25/2012 07:50 AM, Nick Sabalausky wrote:

The compiler rejects this:

 class Base {}
 class Derived : Base {}

 void main()
 {
 Base*basePtr;
 Derived* derivedPtr;

 basePtr = derivedPtr; // ERROR
 }

Is that really correct that it shouldn't be allowed?




Yes it is. It was allowed a number of releases ago, but that bug has 
since been fixed.


Example that shows unsoundness:

class Base {}
class Derived1 : Base {}
class Derived2 : Base {}

void main() {
Derived1 d1;
Base* basePtr = d1;
*basePtr = new Derived2;
assert(typeid(d1)==typeid(Derived2)  !is(Derived2: typeof(d1));
}


Note that the conversion succeeds if the tail of the pointer is not 
mutable, eg Derived* implicitly converts to const(Base)*.


Re: Object Serialization?

2012-04-25 Thread Jacob Carlborg

On 2012-04-24 23:30, sclytrack wrote:


Does it automatically pick everything to serialize?


Yes.


How would you make it more selective?


It depends on what you want to do.


struct Me
{
int x;
int y;
}

serialize x but not y.


In this simple case you can do like this:

struct Me
{
int x;
int y;

mixin NonSerialized!(y);
}


Would you have to create a custom serializer?


No, not in this case. But it depends on what you want to do.


If so I would like to see an example with the custom serializer and
deserializer that only does the x.



Basic Example
Serialize Through Base Class
Register Serializer?



There is no register serializer example.


Yeah, I know.


277 private void serializeStruct (T) (T value, string key, Id id)
278 {
279 string type = T.stringof;
280
281 triggerEvents(serializing, value, {
282 archive.archiveStruct(type, key, id, {
283 if (type in serializers)
284 {
285 auto wrapper = getSerializerWrapper!(T)(type);
286 wrapper(value, this, key);
287 }
288
289 else
290 {
291 static if (isSerializable!(T))
292 value.toData(this, key);
293
294 else
295 objectStructSerializeHelper(value);
296 }
297 });
298 });
299 }



I assume that the wrapper is the custom serializer that can be registered.


Yes.


Then there is the toData that a struct can have, basically
member functions that do the serialization. Priority is
given to the registered serializer over the member functions.


Yes.


And the last one. ObjectStructSerializerHelper is more a default
serializer if none is registered or doesn't have the correct
isSerializable member functions.
ObjectStructSerializerHelper(T) (T .)


Just browsing, I haven't downloaded anything.


You can have a look at registerSerializer and registerDeserializer here:

http://dl.dropbox.com/u/18386187/orange_docs/orange.serialization.Serializer.html

Most of the documented classes and methods contains examples.

There are basically three ways to customize the (de)serialization process:

* Using NonSerialized - 
http://dl.dropbox.com/u/18386187/orange_docs/orange.serialization.Serializable.html


* Non-intrusive serialization (registerSerializer) - 
http://dl.dropbox.com/u/18386187/orange_docs/orange.serialization.Serializer.html


* Intrusive serialization (toData) - 
http://dl.dropbox.com/u/18386187/orange_docs/orange.serialization.Serializable.html


So in your example it would look something like this:

struct Me
{
int x;
int y;
}

auto archive = new XmlArchive!();
auto serializer = new Serializer(archive);

auto dg = (Me value, Serializer serializer, Data key) {
serializer.serialize(x, x);
};

auto dg2 = (ref Me value, Serializer serializer, Data key) {
value.x = serializer.deserialize!(int)(x);
};

Serializer.registerSerializer!(Me)(dg);
Serializer.registerDeserializer!(Me)(dg2);

The above would be the non-intrusive example.

--
/Jacob Carlborg


Help with C struct by value on OSX 64bits

2012-04-25 Thread Johan Hernandez
Hi. I'm having trouble calling a C function passing a structure 
by value, printing a member and getting it back in D but I must 
be doing something wrong.


I have a C file called native.c that defines a function called 
'filter' taking a buf_t structure, I compile it using GCC to 
native.o. From D, I use extern(C) to access the 'filter' function 
defined in native.o.


##Makefile

app: native.o
dmd -ofbin/app app.d native.o
chmod +x bin/app
bin/./app

native.o: native.c
gcc -c native.c -o native.o

##app.d

import std.stdio;

extern(C) struct buf_t {
void* base;
size_t len;
  }

extern(C) buf_t filter(const buf_t value);

void main() {
  buf_t buf;
  buf.len = 10;
  auto r = filter(buf);
  writefln(D: len %d, r.len);
}

***native.c***

#include sys/types.h
#include stdio.h

typedef struct {
  void* base;
  size_t len;
} buf_t;

buf_t filter(buf_t value) {
  printf(C: len: %zu\n, value.len);
  return value;
}


***The output***

C: len: 140734905718328
D: len 0


len should be 10 in both cases. What am I doing wrong?

I'm using:
OS: Mac OSX Lion, 64bits.
D compiler: DMD64 D Compiler v2.058
C compiler: gcc version 4.2.1 (Based on Apple Inc. build 5658) 
(LLVM build 2336.1.00)


Thanks,
Johan


Re: Help with C struct by value on OSX 64bits

2012-04-25 Thread simendsjo
On Wed, 25 Apr 2012 09:57:39 +0200, Johan Hernandez  
thepumpkin1...@gmail.com wrote:


Hi. I'm having trouble calling a C function passing a structure by  
value, printing a member and getting it back in D but I must be doing  
something wrong.


Could be this one: http://d.puremagic.com/issues/show_bug.cgi?id=5570


newb question re. reading lines from stdin

2012-04-25 Thread aylwyn

Hi, I've just written a smallish program reading
lines from stdin and doing some parsing and other text
processing. I find it's about 3-4 times faster than a python
script I had doing the same thing, which is nice but not as good
as I hoped. After profiling, it seems almost half the total time
is being spent in

std.stdio.File.ByLine!(char, char).ByLine.popFront()

called once per line on stdin, and within that mostly in

std.stdio.File.readln!(char).readln(ref char[], dchar)


Before I do some benchmark comparisons with C etc, can I just
check that

foreach (line; stdin.byLine())

is the correct way to do this. Are there any lower-level methods
I could use for buffered access to a stream of chars?

(using DMD64 D Compiler v2.058.)


Re: newb question re. reading lines from stdin

2012-04-25 Thread Dmitry Olshansky

On 25.04.2012 15:10, aylwyn wrote:

Hi, I've just written a smallish program reading
lines from stdin and doing some parsing and other text
processing. I find it's about 3-4 times faster than a python
script I had doing the same thing, which is nice but not as good
as I hoped. After profiling, it seems almost half the total time
is being spent in

std.stdio.File.ByLine!(char, char).ByLine.popFront()

called once per line on stdin, and within that mostly in

std.stdio.File.readln!(char).readln(ref char[], dchar)


Before I do some benchmark comparisons with C etc, can I just
check that

foreach (line; stdin.byLine())

is the correct way to do this. Are there any lower-level methods
I could use for buffered access to a stream of chars?


It is. It needs more optimizations though. There have been talks of 
byLineAsync() that will opportunistically fill next line in background 
and so on and so forth.




(using DMD64 D Compiler v2.058.)


I bet straight fgets will beat stdin.byLine.

--
Dmitry Olshansky


Re: Docs: Section on local variables

2012-04-25 Thread Stewart Gordon

On 21/04/2012 19:24, H. S. Teoh wrote:
snip

In finished code, it's obviously a bad thing to have unused variables
(unless the compiler optimizes them away,


Whether the compiler optimises it away or not, an unused variable is a code smell. 
Complaining about unused variables serves as a warning to the programmer that there's 
probably a bug in the program.  Even if it's left over from debugging, it looks silly, and 
might lead other people reading the code to believe something's wrong.



but that's not happening 'cos
it depends on flow analysis, which would have let us spit out warnings
about it in the first place.)


How does seeing that there are no references to a variable anywhere in its scope depend on 
flow analysis?



So do you prefer just an unused variable warning that comes out only
when you use -wi/-w? A problem I've seen in D.learn is that lot of
people here doesn't seem to use -wi/-w.


So you think compiler warnings should be compulsory - with perhaps a CLO just to control 
whether they cause the compilation to fail?



Or maybe, on the contrary,
this unused variable error should be suppressed only if the D code is
compiled with -debug?


I don't know if conflating unused variable warnings with -debug is a
good thing. Just like the conflation of -release with the opposite of
-debug or -unittest.

snip

I don't really like this idea either.  The point of -debug is to add code to the program 
for debugging.  You might need to switch this debugging code on/off independently of 
whether you have unused variables.


Two possibilities I can see:
- Keep the statement in the spec, and fix DMD to implement it properly.  Maybe add a CLO 
to suppress errors such as this one that are only there to catch bugs.

- Remove the statement from the spec, and implement a warning in DMD.

Stewart.


Re: Docs: Section on local variables

2012-04-25 Thread Stewart Gordon

On 21/04/2012 17:26, Andrej Mitrovic wrote:
snip

Next thing you know the compiler will start warning me when I indent
my code with uneven number of spaces!


Or more usefully, warn if you have a mishmash of tab and space indentation.

How do indent-sensitive languages (Haskell, Python, whatever else) deal with mixed 
tab/space indentation, for that matter?


Stewart.


Re: Strange measurements when reproducing issue 5650

2012-04-25 Thread Martin Drašar

Dne 25.4.2012 13:27, SomeDude napsal(a):

On Wednesday, 25 April 2012 at 08:34:40 UTC, SomeDude wrote:

Noone reproduces this ?


Linux uriel 2.6.32-5-amd64 #1 SMP Wed Jan 12 03:40:32 UTC 2011 x86_64 
GNU/Linux

DMD64 D Compiler v2.058

Commented writeln on average (10 runs) 40 ms slower.

So yes, it happens to me as well.

Martin


Internal error: ..\ztc\cod3.c 485

2012-04-25 Thread Era Scarecrow

This is an interesting one that has cropped up. I'm curious if
this is because the differences between a fixed/dynamic arrays.
Working around it is easy enough but that it even does it is an
annoyance.

I'm not seeing this exact problem in the archives, and I am 
not

sure if it's in the tracker already under a different
name/identifier. :(


---
module internal_error_485;
/*Internal error: ..\ztc\cod3.c 485
 happens when class (or inherited) have one return a string 
(to

convert) and the other with the workaround.

 Oddly it isn't doing auto conversion/shortening on a string 
of

a known size.
*/

alias immutable(char[4]) string4;
class x {
 string4 f() {
//  return cast(string4) HEDR;//Internal error:
..\ztc\cod3.c 485
//  return cast(string4) HEDR.ptr;  //same

//  return HEDR;  //implicit conversion fails
//  return string4(HEDR);  //temporary object doesn't
create properly for returning.

 string4 x = HEDR;
 return x;
 }
}

class z : x {
 override string4 f() {
   return brk_;  //Internal error: ..\ztc\cod3.c 485
//return cast(string4) brk_;  //Same error

//following works... but...
//string4 x = brk_;
//return x;
 }
}

---

it also breaks the same way if the base class returns the
string
and the inherited class returns the converted/long steps.


Re: Internal error: ..\ztc\cod3.c 485

2012-04-25 Thread Steven Schveighoffer

http://d.puremagic.com/issues/show_bug.cgi?id=7254 maybe?

-Steve


Re: A general tag

2012-04-25 Thread Marco Leise
Am Mon, 16 Apr 2012 20:52:16 +0200
schrieb Xan xancor...@gmail.com:

 Uf!, it's more than I can process
 It's really a **complicated** thing to do that in D.

Too much information, yes. Too complicated no. :)
D is statically typed (Fantom allows both static and dynamic typing). That 
means that for every variable in your program you know beforehand what type it 
is. You say you have String, Type and Obj. Here we cannot say what type Obj is, 
because it depends on what Type says. Now you want something from a statically 
typed language, that it wasn't designed for.

You may want to adapt to the style of a statically typed language and use a 
struct:

import std.datetime;

struct Person {
string name;  // a person has a field 'name' of type string
Date   date;
}

This means that you cannot change the type of 'date' or 'name', and you cannot 
have a person without a date.

If you really, *really*, REALLY need the full flexibility, you can use 
Variants. You can store any data in a Variant, so it would replace Type and Obj 
in your code.

struct Tag {
string description; // String: store name, date, etc. in this
Variant data;   // Type  Obj: store John or Date(2012, 4, 25) 
in this
}

Many more solutions are possible, but we'd need to take a look at your code. 
Also one solution may be more flexible while another is faster. It depends a 
bit on what you expect. Every language has its pros and cons. Dynamic typing is 
not one of D's strengths, but execution speed is, for example.

-- 
Marco



D static lib called from C on Mac OS X

2012-04-25 Thread Nicolas Sicard

Hi,

I am trying to use a D static library from C on Mac OSX Lion, but 
it always fails.


--- file mylib.d ---
module mylib;
import core.runtime;

extern(C) {
bool mylib_init() {
return Runtime.initialize();
}

bool mylib_free() {
return Runtime.terminate();
}
}
---

--- file main.c ---
extern void mylib_init();
extern void mylib_free();

int main() {
mylib_init();
mylib_free();
return 0;
}
---

I am compiling using:
$ dmd -c -lib mylib.d -oflibmylib.a
$ gcc -o main main.c -L. -lmylib -lphobos2

When I run ./main, I get a EXC_BAD_ACCESS (SIGSEGV) that seems 
related to thread local storage during runtime initialization.


Thread 0 Crashed:: Dispatch queue: com.apple.main-thread
0   main  	0x000103072dac 
__tls_get_addr + 196
1   main  	0x000103071d80 
thread_attachThis + 312
2   main  	0x000103071c3c thread_init 
+ 24

3   main0x000103073296 gc_init + 86
4   main0x000103079f79 rt_init + 29
5   main  	0x0001030705bb 
D4core7runtime7Runtime10initializeFDFC6object9ThrowableZvZb + 15
6   main  	0x000103069e5d mylib_init 
+ 13

7   main0x000103069e2f main + 15
8   main0x000103069e14 start + 52

Am I doing something wrong or trying to do something currently 
unsupported?


Thanks,
Nicolas




Re: D static lib called from C on Mac OS X

2012-04-25 Thread Nicolas Sicard

s/void/int in main.c


Bitarray size limits

2012-04-25 Thread ixid
These seem to be limited to uint.max length. Is there a way to 
make larger bit arrays?


Re: Strange measurements when reproducing issue 5650

2012-04-25 Thread Don Clugston

On 25/04/12 10:34, SomeDude wrote:

Discussion here: http://d.puremagic.com/issues/show_bug.cgi?id=5650

On my Windows box, the following program

import std.stdio, std.container, std.range;

void main() {
enum int range = 100;
enum int n = 1_000_000;

auto t = redBlackTree!int(0);

for (int i = 0; i  n; i++) {
if (i  range)
t.removeFront();
t.insert(i);
}

writeln(walkLength(t[]));
//writeln(t[]);
}

runs in about 1793 ms.
The strange thing is, if I comment out the writeln line, runtimes are in
average *slower* by about 20 ms, with timings varying a little bit more
than when the writeln is included.

How can this be ?


Very strange.
Maybe there is some std library cleanup which is slower if nothing got 
written?


Re: Strange measurements when reproducing issue 5650

2012-04-25 Thread Steven Schveighoffer
On Wed, 25 Apr 2012 07:27:29 -0400, SomeDude lovelyd...@mailmetrash.com  
wrote:



On Wednesday, 25 April 2012 at 08:34:40 UTC, SomeDude wrote:

Noone reproduces this ?


On my linux box, it runs in about 580ms, with or without the writeln.

This is what I would expect.

But things can be strange when dealing with GC timings.  Have you tried  
profiling the code to see where the time is being added?


-Steve


Re: Range of random numbers

2012-04-25 Thread Christophe Travert
bearophile , dans le message (digitalmars.D.learn:35148), a écrit :
 Why don't you write a little benchmark to compare the performance 
 of the two versions?

Because I'm interested in the code's meaning for the human reader, not 
the performance.

I actually think : map!(_= uniform(a, b))(repeat(0)) is better, 
because it puts the call to uniform first, and the _ indicates that the 
rest is unimportant.


For you curiosity, this is the benchmark using:
gdc (GCC) 4.7.0 20120322 (gdc 0.31 - r805:0414cec152c7, using dmd 2.057)
and
gdc -O3 testRNG.d  ./a.out


import std.array, std.range, std.algorithm, std.random, std.stdio, 
std.datetime;

auto uniformRange1(T1, T2, T3)(T1 a, T2 b, T3 gen)
{
  return map!((x) {return x();} ) (repeat((){return uniform(a, b, gen);}));
}

auto uniformRange2(T1, T2, T3)(T1 a, T2 b, T3 gen)
{
  return map!((x) { return uniform(a, b, gen); })(repeat(0));
}

auto uniformRange3(T1, T2, T3)(T1 a, T2 b, T3 gen)
{
  return map!((x) { return uniform(a, b, gen); })(cycle([0]));
}

void f1() { auto ur = array(take(uniformRange1(0, 10, Xorshift(1)), 
1000)); }
void f2() { auto ur = array(take(uniformRange2(0, 10, Xorshift(1)), 
1000)); }
void f3() { auto ur = array(take(uniformRange3(0, 10, Xorshift(1)), 
1000)); }

void main() {
  auto b=benchmark!(f1, f2, f3)(1000);
  writeln(b[0].to!(seconds,double),s,  , b[1].to!(seconds,double),
s, , b[2].to!(seconds,double),s. );
  // outputs :
  // 0.040437s,  0.0393537s, 0.0486439s
}


f2 performs 23% better than f3, and 3% better than f1.

-- 
Christophe


Re: Strange measurements when reproducing issue 5650

2012-04-25 Thread Jordi Sayol
Al 25/04/12 13:27, En/na SomeDude ha escrit:
 On Wednesday, 25 April 2012 at 08:34:40 UTC, SomeDude wrote:
 
 Noone reproduces this ?
 

My results in Linux:

32-bit executable: About 574ms, with or without writeln.

64-bit executable: About 798ms, with or without writeln.

-- 
Jordi Sayol


Re: Issue calling methods using std.concurrency

2012-04-25 Thread Casey
Stanislav: I think you just hit the nail on the head with the 
shared reference to this.  I didn't even think of that as the 
error message made me think of the parameter being shared.


As for solving it, I'll have to think about it.  I was hoping to 
just have a single queue class (Or any other in the future) that 
worked well single-threaded, but could be used in a 
multi-threaded environment if I used std.concurrency properly.  
Essentially, my goal was to have only a single queue, but use 
message passing to enqueue/dequeue items from the queue without 
having to create a custom thread-safe queue class.


Well, I guess I have to rethink what I'm doing.  Thanks!


Re: Bitarray size limits

2012-04-25 Thread bearophile

ixid:
These seem to be limited to uint.max length. Is there a way to 
make larger bit arrays?


Yeah, fixing the BigArray source :-)

It's limited by size_t.max length, that equals to uint.max on 32 
bit systems.


It contains code like:

struct BitArray {
size_t len;
size_t* ptr;

@property const size_t dim() {
return (len + (bitsPerSizeT-1)) / bitsPerSizeT;
}
@property const size_t length() {
return len;
}
...
}


To create larger arrays that 'len' needs to be a ulong. So it 
needs to become something like this:


struct BitArray(bool hugeToo=false) {
static if (hugeToo) {
alias ulong Tindex;
else
alias size_t Tindex;
Tindex len;
size_t* ptr;

@property const Tindex dim() {
return (len + (bitsPerSizeT - 1)) / bitsPerSizeT;
}
@property const Tindex length() {
return len;
}
...
}


But this may cause some problems because BitArray is trying to 
look like a dynamic array, while now it's something that can be 
longer (and with a larger struct) than any dynamic array, so 
_from the outside too_ the management of its length needs extra 
care.


Bye,
bearophile


ptrace (process trace system call) on Linux from D

2012-04-25 Thread Matej Nanut

Hello everyone,

I would like to know how to call ptrace (the system call) from D.

I don't know what to import or link.  If my understanding is 
correct, I need to create a .di file of some sort with stuff 
declared in it.  How would I do this?


Thanks, Matej


Re: Strange measurements when reproducing issue 5650

2012-04-25 Thread SomeDude
On Wednesday, 25 April 2012 at 15:35:44 UTC, Steven Schveighoffer 
wrote:
On Wed, 25 Apr 2012 07:27:29 -0400, SomeDude 
lovelyd...@mailmetrash.com wrote:



On Wednesday, 25 April 2012 at 08:34:40 UTC, SomeDude wrote:

Noone reproduces this ?


On my linux box, it runs in about 580ms, with or without the 
writeln.


This is what I would expect.

But things can be strange when dealing with GC timings.  Have 
you tried profiling the code to see where the time is being 
added?


-Steve


Did you compile with  dmd -O -inline -release ?

I'll do some profiling later.


Re: Strange measurements when reproducing issue 5650

2012-04-25 Thread Marco Leise
Am Wed, 25 Apr 2012 10:34:38 +0200
schrieb SomeDude lovelyd...@mailmetrash.com:

 Discussion here: 
 http://d.puremagic.com/issues/show_bug.cgi?id=5650
 
 On my Windows box, the following program
 
 import std.stdio, std.container, std.range;
 
 void main() {
  enum int range = 100;
  enum int n = 1_000_000;
 
  auto t = redBlackTree!int(0);
 
  for (int i = 0; i  n; i++) {
  if (i  range)
  t.removeFront();
  t.insert(i);
  }
 
  writeln(walkLength(t[]));
  //writeln(t[]);
 }
 
 runs in about 1793 ms.
 The strange thing is, if I comment out the writeln line, runtimes 
 are in average *slower* by about 20 ms, with timings varying a 
 little bit more than when the writeln is included.
 
 How can this be ?

GDC (using DMD 2.057) Linux 64-bit, Core 2 Duo @ 2 Ghz
+---
without writeln |   with writeln
3.150 s | 3.146 s .. 3,195 s

Besides the fact that my rig seems to be a bit dated, I can't reproduce what 
you see. Only that the writeln makes the timing less accurate.

-- 
Marco



Re: D static lib called from C on Mac OS X

2012-04-25 Thread Nicolas Sicard

More testing. This:

--- file mylib.d
module mylib;
import core.runtime;
import std.stdio;

extern(C) {
bool mylib_init() {
return Runtime.initialize();
}

bool mylib_free() {
return Runtime.terminate();
}

void mylib_hello() {
writeln(Hello from mylib);
}
}

void main() {} // Fake main
---

--- file main.c ---
extern int mylib_init();
extern int mylib_free();
extern void mylib_hello();

int main() {
mylib_init();
mylib_hello();
mylib_free();
return 0;
}
---

$ dmd -c mylib.d
$ gcc -o main main.c mylib.o -lphobos2 -lpthread -lrt
$ ./main

works on Linux (Unbuntu 11.10), but segfaults on OS X Lion.




Re: Strange measurements when reproducing issue 5650

2012-04-25 Thread H. S. Teoh
On Wed, Apr 25, 2012 at 10:34:38AM +0200, SomeDude wrote:
[...]
 import std.stdio, std.container, std.range;
 
 void main() {
 enum int range = 100;
 enum int n = 1_000_000;
 
 auto t = redBlackTree!int(0);
 
 for (int i = 0; i  n; i++) {
 if (i  range)
 t.removeFront();
 t.insert(i);
 }
 
 writeln(walkLength(t[]));
 //writeln(t[]);
 }
 
 runs in about 1793 ms.
 The strange thing is, if I comment out the writeln line, runtimes
 are in average *slower* by about 20 ms, with timings varying a
 little bit more than when the writeln is included.
[...]

First of all, differences as small as 20ms really should be considered
as background noise. The exact measurements depend on a lot of
system-specific and environment-specific factors, such as OS memory
usage, CPU cache behaviour, disk activity  speed, the exact points of
context switches, etc.. If you really want to check for substantial
performance differences, you need to magnify your test case so that
differences are measured 5 seconds.

Second, on my AMD hexacore 64-bit Linux system, the running time
consistently measures between 0.57 or 0.58 seconds for both cases. The
exact figure changes between runs, and as far as I can tell, there's no
discernible difference between the two.

Third, to make any timing differences stand out from the background
noise, I increased n to 20_000_000, and both versions of the program
consistently runs in about 11 seconds each time. There's no discernible
difference between the two.

What all this means is that a single call to writeln does not make
enough difference to be measurable compared to the rest of the program.
It doesn't mean that the version with writeln is faster, just that the
difference is too small and you're probably just seeing background
noise. If you put the writeln inside a loop, on the other hand, you'll
see a big difference, because now its cost is magnified by the number of
times the loop runs. (Say if you put it inside a foreach(i;0..1000) at
the end of the program, you'll see the difference when you comment it
out.)

So I'd chalk it up to inherent measurement inaccuracies.


T

-- 
He who sacrifices functionality for ease of use, loses both and deserves 
neither. -- Slashdotter


Power of D

2012-04-25 Thread bioinfornatics
i search some example of something easy (more easy)  to do in D an not
in another language if possible
- D - C++
- D - Haskell
- D - Java
- D - python

thanks a lot



Re: Docs: Section on local variables

2012-04-25 Thread Andrej Mitrovic
On 4/25/12, Stewart Gordon smjg_1...@yahoo.com wrote:
 Even if it's left over from debugging, it
 looks silly, and
 might lead other people reading the code to believe something's wrong.

There's about a million ways to make code unreadable, and nobody
writes pitch-perfect code that has absolutely no leftover code or
comments.

And what if you're refactoring and you do multiple builds every couple
of seconds? You add a variable, remove it, etc etc. Enabling this
warning will just make for a noisy compiler. Keeping variables clean
is the responsibility of the programmer and not the compiler.

If it doesn't affect the semantics of code the compiler should shut
up. Please don't turn the compiler into a reincarnation of Clippy.


Re: D static lib called from C on Mac OS X

2012-04-25 Thread Andrej Mitrovic
On 4/25/12, Nicolas Sicard dran...@gmail.com wrote:
 --- file main.c ---
 extern void mylib_init();
 extern void mylib_free();

Try changing void to bool there.


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

2012-04-25 Thread mta`chrono
Am 25.04.2012 18:36, schrieb Matej Nanut:
 Hello everyone,
 
 I would like to know how to call ptrace (the system call) from D.
 
 I don't know what to import or link.  If my understanding is correct, I
 need to create a .di file of some sort with stuff declared in it.  How
 would I do this?
 
 Thanks, Matej


Hello Matej,


I guess you're asking for http://linux.die.net/man/2/ptrace.

normally it's somewhere deep in druntime but I couldn't find it. Maybe
someone is able to add it for you. But neverthenless, you can just put
some declaration in your D file and call it.


import core.stdc.stdio;
import core.sys.posix.sys.types;

enum __ptrace_request
{
PTRACE_TRACEME = 0,
PTRACE_PEEKTEXT = 1,
PTRACE_PEEKDATA = 2,
PTRACE_PEEKUSER = 3,
PTRACE_POKETEXT = 4,
PTRACE_POKEDATA = 5,
PTRACE_POKEUSER = 6,
PTRACE_CONT = 7,
PTRACE_KILL = 8,
PTRACE_SINGLESTEP = 9,
PTRACE_GETREGS = 12,
PTRACE_SETREGS = 13,
PTRACE_GETFPREGS = 14,
PTRACE_SETFPREGS = 15,
PTRACE_ATTACH = 16,
PTRACE_DETACH = 17,
PTRACE_GETFPXREGS = 18,
PTRACE_SETFPXREGS = 19,
PTRACE_SYSCALL = 24,
PTRACE_SETOPTIONS = 0x4200,
PTRACE_GETEVENTMSG = 0x4201,
PTRACE_GETSIGINFO = 0x4202,
PTRACE_SETSIGINFO = 0x4203
}

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

void main()
{
   // just call ptrace like you would do in C
}






Re: D static lib called from C on Mac OS X

2012-04-25 Thread Nicolas Sicard
On Wednesday, 25 April 2012 at 17:59:38 UTC, Andrej Mitrovic 
wrote:

On 4/25/12, Nicolas Sicard dran...@gmail.com wrote:

--- file main.c ---
extern void mylib_init();
extern void mylib_free();


Try changing void to bool there.


This was a typo in my first post. The problem is elsewhere.

Thanks



[Kinda OT] httpd permissions

2012-04-25 Thread Nathan M. Swan

How do I deal with this (on OSX); are CGI programs not allowed to
write to files? How to change this?

Thanks, NMS

test.d:
#!/usr/local/bin/rdmd

import std.stdio;

void main() {
   writeln(Content-type: text/plain\r\n\r\nHello, World!);
}

error log:
[Wed Apr 25 00:03:01 2012] [error] [client ::1] sh:
/Users/nathanmswan/Sites/test.d.deps: Permission denied
[Wed Apr 25 00:03:01 2012] [error] [client ::1] Failed: dmd  -v
-o- '/Users/nathanmswan/Sites/test.d'
-I'/Users/nathanmswan/Sites' /Users/nathanmswan/Sites/test.d.deps
[Wed Apr 25 00:03:01 2012] [error] [client ::1] Premature end of
script headers: test.d



Re: [Kinda OT] httpd permissions

2012-04-25 Thread Nicolas Sicard

On Wednesday, 25 April 2012 at 18:11:16 UTC, Nathan M. Swan wrote:
How do I deal with this (on OSX); are CGI programs not allowed 
to

write to files? How to change this?

Thanks, NMS

test.d:
#!/usr/local/bin/rdmd

import std.stdio;

void main() {
   writeln(Content-type: text/plain\r\n\r\nHello, World!);
}

error log:
[Wed Apr 25 00:03:01 2012] [error] [client ::1] sh:
/Users/nathanmswan/Sites/test.d.deps: Permission denied
[Wed Apr 25 00:03:01 2012] [error] [client ::1] Failed: dmd  -v
-o- '/Users/nathanmswan/Sites/test.d'
-I'/Users/nathanmswan/Sites'
/Users/nathanmswan/Sites/test.d.deps
[Wed Apr 25 00:03:01 2012] [error] [client ::1] Premature end of
script headers: test.d


Have you checked that your web server has write access to 
/Users/nathanmswan/Sites/ ?





Re: Help with C struct by value on OSX 64bits

2012-04-25 Thread Johan Hernandez

On Wednesday, 25 April 2012 at 08:38:02 UTC, simendsjo wrote:
Could be this one: 
http://d.puremagic.com/issues/show_bug.cgi?id=5570


Thank you for your response.

It's a huge issue :(


Why is List(T) documented in std.concurrency?

2012-04-25 Thread Alex Rønne Petersen

Hi,

http://dlang.org/phobos/std_concurrency.html

Why does List(T) appear? Isn't it private?

--
- Alex


Using input ranges with std.regex?

2012-04-25 Thread H. S. Teoh
Does std.regex support input ranges to match()? Or do I need to convert
to string first?

Thanks!


T

-- 
Tell me and I forget. Teach me and I remember. Involve me and I understand. -- 
Benjamin Franklin


Re: Docs: Section on local variables

2012-04-25 Thread Stewart Gordon

On 25/04/2012 17:10, Andrej Mitrovic wrote:

On 4/25/12, Stewart Gordonsmjg_1...@yahoo.com  wrote:

Even if it's left over from debugging, it
looks silly, and
might lead other people reading the code to believe something's wrong.


There's about a million ways to make code unreadable, and nobody
writes pitch-perfect code that has absolutely no leftover code or
comments.


Exactly.  But good-quality compilers help programmers in that direction in 
various ways.


And what if you're refactoring and you do multiple builds every couple
of seconds? You add a variable, remove it, etc etc. Enabling this
warning will just make for a noisy compiler.


But if the spec stays the same, the compiler needs to generate an _error_ for it in order 
to conform.  If this statement is removed from the spec, then it will be a matter of 
adding a warning.  But this is part of why warnings are optional in DMD.  By enabling 
warnings in the compiler in the first place, the programmer is asking to be informed of 
things like this.



Keeping variables clean
is the responsibility of the programmer and not the compiler.

If it doesn't affect the semantics of code the compiler should shut
up. Please don't turn the compiler into a reincarnation of Clippy.


So you think that

import std.stdio;
void main() {
int a, b;
a + b;
return;
writefln(Hello, world!);
}

should generate no errors or warnings whatsoever?

Stewart.


Re: Help with C struct by value on OSX 64bits

2012-04-25 Thread simendsjo
On Wed, 25 Apr 2012 20:36:31 +0200, Johan Hernandez  
thepumpkin1...@gmail.com wrote:



On Wednesday, 25 April 2012 at 08:38:02 UTC, simendsjo wrote:

Could be this one: http://d.puremagic.com/issues/show_bug.cgi?id=5570


Thank you for your response.

It's a huge issue :(


I agree. Let's hope it gets fixed now that it's confirmed on OSX too.
You could use the voting system in bugzilla to give the issue more focus.


Re: Help with C struct by value on OSX 64bits

2012-04-25 Thread Jacob Carlborg

On 2012-04-25 20:36, Johan Hernandez wrote:

On Wednesday, 25 April 2012 at 08:38:02 UTC, simendsjo wrote:

Could be this one: http://d.puremagic.com/issues/show_bug.cgi?id=5570


Thank you for your response.

It's a huge issue :(


Compile as a 32bit binary. For DMD add -m32 do the flags. For gcc add 
-arch i386.


--
/Jacob Carlborg


Re: Why is List(T) documented in std.concurrency?

2012-04-25 Thread Jonathan M Davis
On Wednesday, April 25, 2012 21:00:33 Alex Rønne Petersen wrote:
 Hi,
 
 http://dlang.org/phobos/std_concurrency.html
 
 Why does List(T) appear? Isn't it private?

It's probably due to http://d.puremagic.com/issues/show_bug.cgi?id=2775

However, to make things weirder, it _doesn't_ have ddoc on it. It's

 /*
 *
 */
 struct List(T)

Since there's only one *, it shouldn't show up in the docs, even if it's 
public. So, there may actually be another bug relating to nested structs and 
ddoc which combines with 2775 to make List appear in the docs.

- Jonathan M Davis


Re: Docs: Section on local variables

2012-04-25 Thread Jonathan M Davis
On Wednesday, April 25, 2012 20:10:18 Stewart Gordon wrote:
 On 25/04/2012 17:10, Andrej Mitrovic wrote:
  On 4/25/12, Stewart Gordonsmjg_1...@yahoo.com wrote:
  Even if it's left over from debugging, it
  looks silly, and
  might lead other people reading the code to believe something's wrong.
  
  There's about a million ways to make code unreadable, and nobody
  writes pitch-perfect code that has absolutely no leftover code or
  comments.
 
 Exactly. But good-quality compilers help programmers in that direction in
 various ways.
  And what if you're refactoring and you do multiple builds every couple
  of seconds? You add a variable, remove it, etc etc. Enabling this
  warning will just make for a noisy compiler.
 
 But if the spec stays the same, the compiler needs to generate an _error_
 for it in order to conform. If this statement is removed from the spec,
 then it will be a matter of adding a warning. But this is part of why
 warnings are optional in DMD. By enabling warnings in the compiler in the
 first place, the programmer is asking to be informed of things like this.
 
  Keeping variables clean
  is the responsibility of the programmer and not the compiler.
  
  If it doesn't affect the semantics of code the compiler should shut
  up. Please don't turn the compiler into a reincarnation of Clippy.
 
 So you think that
 
 import std.stdio;
 void main() {
 int a, b;
 a + b;
 return;
 writefln(Hello, world!);
 }
 
 should generate no errors or warnings whatsoever?

The only part of that that I'd want to give an error is what currently gives 
an error - the line with a + b due to the fact that it has no effect. There's 
no reason for such a line to exist even while debugging. But having the 
compiler complain about unused variables and/or unreachable code gets to be 
_really_ annoying when editing code - especially when adding and removing stuff 
during debugging.

Unfortunately, the writefln line _does_ result in an error for unreachable code 
when compiled with -w, but at least it's not an error normally. Still, I'd 
prefer if it weren't even a warning - especially since increasingly I agree 
with Walter's take on warnings (that they shouldn't exist at all - something 
is an error or it isn't; none of this halfway stuff). Warnings are problematic 
in that a good programmer will _never_ leave them in their code and yet so 
many programmers do. So, they become useless noise. The _only_ advantage to 
them is that they can be used for stupid stuff like unreachable code, allowing 
you to leave warnings while editing code but remove them when your done. I 
don't really even like that though, truth be told.

- Jonathan M Davis


Re: Why is List(T) documented in std.concurrency?

2012-04-25 Thread Mirko Pilger

Since there's only one *, it shouldn't show up in the docs, even if it's
public.


If there is no documentation comment for a declaration, that 
declaration may not appear in the output. To ensure it does appear in 
the output, put an empty declaration comment for it.


http://dlang.org/ddoc.html


Re: Docs: Section on local variables

2012-04-25 Thread Andrej Mitrovic
On 4/25/12, Stewart Gordon smjg_1...@yahoo.com wrote:
 So you think that

 import std.stdio;
 void main() {
  int a, b;
  a + b;
  return;
  writefln(Hello, world!);
 }

 should generate no errors or warnings whatsoever?

I'm really only talking about:
void a() {
int x;
}

And of course:
void a() {
bool state;
...
if (state) { }
}

I'd like the warnings to be individually selectable, just like in GCC.

Btw, here's a trick question, should the compiler warn about this case?

void main() {
new Foo();  // or even Foo foo = new Foo;
}

:p


Re: Docs: Section on local variables

2012-04-25 Thread Andrej Mitrovic
On 4/25/12, Andrej Mitrovic andrej.mitrov...@gmail.com wrote:
 I'm really only talking about

Although I'm not a fan of warnings for unused variables, I would be a
fan of this: http://d.puremagic.com/issues/show_bug.cgi?id=3507

But again, other people might not like that. But if it was an option..


Re: Docs: Section on local variables

2012-04-25 Thread H. S. Teoh
On Wed, Apr 25, 2012 at 04:03:04PM -0400, Jonathan M Davis wrote:
[...]
 increasingly I agree with Walter's take on warnings (that they
 shouldn't exist at all - something is an error or it isn't; none of
 this halfway stuff). Warnings are problematic in that a good
 programmer will _never_ leave them in their code and yet so many
 programmers do. So, they become useless noise. The _only_ advantage to
 them is that they can be used for stupid stuff like unreachable code,
 allowing you to leave warnings while editing code but remove them when
 your done. I don't really even like that though, truth be told.
[...]

The other advantage is that they let you identify code written by
incompetent programmers.


T

-- 
No, John.  I want formats that are actually useful, rather than
over-featured megaliths that address all questions by piling on
ridiculous internal links in forms which are hideously over-complex. --
Simon St. Laurent on xml-dev


Re: Why is List(T) documented in std.concurrency?

2012-04-25 Thread Jonathan M Davis
On Wednesday, April 25, 2012 22:01:45 Mirko Pilger wrote:
  Since there's only one *, it shouldn't show up in the docs, even if it's
  public.
 
 If there is no documentation comment for a declaration, that
 declaration may not appear in the output. To ensure it does appear in
 the output, put an empty declaration comment for it.
 
 http://dlang.org/ddoc.html

Yes, but the comment on it is _not_ a ddoc comment. It's a regular comment. 
So, it shouldn't appear in the docs. For it to be a ddoc comment it would need 
to start with /++ or /**, but it starts with /*.

- Jonathan Davis


Re: Docs: Section on local variables

2012-04-25 Thread Jonathan M Davis
On Wednesday, April 25, 2012 22:12:02 Andrej Mitrovic wrote:
 I'd like the warnings to be individually selectable, just like in GCC.

Having sets of warnings that you can explicitly enable makes a lot of sense, 
because it enables the programmer to have the compiler warn about stuff that 
they care about, whereas if it's something that everyone is going to consider 
a problem, then why isn't it an error? However, Walter doesn't like having a 
lot of compiler flags, and warnings pretty much only exist at all because 
people begged him for them. So, I don't see dmd ever having a bunch of warning 
flags as much as it might be desirable.

- Jonathan M Davis


Re: Using input ranges with std.regex?

2012-04-25 Thread Dmitry Olshansky

On 25.04.2012 23:08, H. S. Teoh wrote:

Does std.regex support input ranges to match()? Or do I need to convert
to string first?



For now, yes you have to convert them. Any random access range of code 
units should do the trick but stringish template constraints might kill 
that.


I plan to extend this eventually. The problematic point is that match 
internally is delimited by integer offsets (indices). Forward ranges 
technically can work (the match then will return something like 
take(..., n);) with a bunch of extra .save calls. Input ranges can't be 
used at all.




--
Dmitry Olshansky


Re: Docs: Section on local variables

2012-04-25 Thread Stewart Gordon

On 25/04/2012 21:12, Andrej Mitrovic wrote:
snip

I'm really only talking about:
void a() {
int x;
}


What is the distinction you're making exactly?


And of course:
void a() {
bool state;
...
if (state) { }
}


You mean an empty if body should trigger something?  Or shouldn't?

OK, so I can see a similarity in that it's likely to occur when disabling portions of code 
for debugging purposes.  Not just debugging the program in which it is present, but also 
creating testcases for compiler/library bug reports.



I'd like the warnings to be individually selectable, just like in GCC.

Btw, here's a trick question, should the compiler warn about this case?

void main() {
 new Foo();  // or even Foo foo = new Foo;
}


An interesting one.  Sometimes a constructor may hook the object up to something.  I've 
probably done this myself on a number of occasions.  Though I can't think of examples 
OTTOMH.  But an example in C++ comes from my last job.  The application framework 
developed in-house includes a class template used to trigger events on construction and 
destruction.  To use it, one would just construct an object of that type.  In many cases, 
it would just be a declaration of a variable of that type (since C++ classes are value 
types) - the variable will never be used again, but the object's construction triggers stuff.


Stewart.


Re: Docs: Section on local variables

2012-04-25 Thread Andrej Mitrovic
On 4/25/12, Stewart Gordon smjg_1...@yahoo.com wrote:
 What is the distinction you're making exactly?
 You mean an empty if body should trigger something?  Or shouldn't?

I'm saying those are exactly the cases presented in the docs and I
don't want them to warn by default but have a setting. I mean, the
first case (warn on unused variables), I just might get used to. But
warning on reading before writing is an extreme change from current
behavior imho.

 Sometimes a constructor may hook the object up to
 something.

Yup. E.g.:

class Foo
{
static Foo[] objects;
this()
{
objects ~= this;
}
}

IIRC I've seen this used in Harmonia and maybe in some places on some
github project (I think it was actually the D forum software). The
compiler might not even know what the ctor does if all it has is the
.di file of the module where the class is defined.


Re: Internal error: ..\ztc\cod3.c 485

2012-04-25 Thread Era Scarecrow
On Wednesday, 25 April 2012 at 12:43:39 UTC, Steven Schveighoffer 
wrote:

http://d.puremagic.com/issues/show_bug.cgi?id=7254 maybe?


 Maybe... but it shouldn't be the wrong return; at least being a 
static length known at compile-time should convert to char[4]. 
I'm not sure how try/catch would make a difference either. But 
being as making a temporary to get the compiler to work around it 
means it isn't a critical problem.


 If these are related though, I could post my code there they are 
breaking at different points.


OT: Indent-sensitive languages again (was: Docs: Section on local variables)

2012-04-25 Thread Stewart Gordon

On 25/04/2012 12:30, Stewart Gordon wrote:

On 21/04/2012 17:26, Andrej Mitrovic wrote:
snip

Next thing you know the compiler will start warning me when I indent
my code with uneven number of spaces!


Or more usefully, warn if you have a mishmash of tab and space indentation.

How do indent-sensitive languages (Haskell, Python, whatever else) deal with 
mixed
tab/space indentation, for that matter?


Just found out
http://urchin.earth.li/~ian/style/haskell.html
http://www.secnetix.de/olli/Python/block_indentation.hawk
that both Haskell and Python uses a tab size of 8 characters.  Personally, I think they 
should error if the meaning of a section of code depends on the tab size.


OTOH, YAML avoids the issue in the strictest way possible: forbidding tab characters.  I 
wonder if there are other languages that require you to indent with tabs.


But one possible design for languages like these is to allow indentation to be either 
entirely spaces or entirely tabs, but not a mixture.  This would also be a good way for 
linters for a variety of languages to behave.


Another way would be to allow tabs, spaces or a mixture, with the only restriction being 
that a given block must be consistently indented with the same sequence of tabs/spaces.


Stewart.


Re: Why is List(T) documented in std.concurrency?

2012-04-25 Thread Ali Çehreli

On 04/25/2012 02:15 PM, Jonathan M Davis wrote:

On Wednesday, April 25, 2012 22:01:45 Mirko Pilger wrote:

Since there's only one *, it shouldn't show up in the docs, even if it's
public.


If there is no documentation comment for a declaration, that
declaration may not appear in the output. To ensure it does appear in
the output, put an empty declaration comment for it.

http://dlang.org/ddoc.html


Yes, but the comment on it is _not_ a ddoc comment. It's a regular comment.
So, it shouldn't appear in the docs. For it to be a ddoc comment it would need
to start with /++ or /**, but it starts with /*.

- Jonathan Davis


But the documentation is vague enough to also mean that even without 
comments it may appear. :)


... may not appear in the output. To ensure it does appear ...

Ali


Re: Power of D

2012-04-25 Thread Ary Manzana

On 4/26/12 1:51 AM, bioinfornatics wrote:

i search some example of something easy (more easy)  to do in D an not
in another language if possible
- D - C++


...


- D - Haskell
- D - Java
- D - python


A segmentation fault is really easy to do in D but hard in those 
languages. :-P


Pointer to variables in D

2012-04-25 Thread Victor Vicente de Carvalho

Hi there,

In c++ one can access a pointer to a class/struct variable using 
this semantic:


struct C {
 int x;
};

int C::* ptr = C::x;

C foo;

foo.*ptr = 10;
assert(foo.x == 10);

It is possible to do something like that on D? I've searched 
through the forum  documentation but didn't found anything.


Also, the reason of this is that I'm studying a way to map a POD 
structure based from a dynamic, data-driven structure. Something 
like get a JSON and map it to a structure automagically. There 
is a nicer way to do that in D?






Re: Why is List(T) documented in std.concurrency?

2012-04-25 Thread Jonathan M Davis
On Wednesday, April 25, 2012 17:31:29 Ali Çehreli wrote:
 On 04/25/2012 02:15 PM, Jonathan M Davis wrote:
  On Wednesday, April 25, 2012 22:01:45 Mirko Pilger wrote:
  Since there's only one *, it shouldn't show up in the docs, even if it's
  public.
  
  If there is no documentation comment for a declaration, that
  declaration may not appear in the output. To ensure it does appear in
  the output, put an empty declaration comment for it.
  
  http://dlang.org/ddoc.html
  
  Yes, but the comment on it is _not_ a ddoc comment. It's a regular
  comment.
  So, it shouldn't appear in the docs. For it to be a ddoc comment it would
  need to start with /++ or /**, but it starts with /*.
  
  - Jonathan Davis
 
 But the documentation is vague enough to also mean that even without
 comments it may appear. :)
 
 ... may not appear in the output. To ensure it does appear ...

Yeah. That wording is pretty atrocious. There should be no ambiguity. But it 
is my understanding that if you don't have a ddoc comment on a symbol, it 
should _never_ end up in the documentation, and prior to this particular case, 
I've never seen it happen.

- Jonathan M Davis


Re: [Kinda OT] httpd permissions

2012-04-25 Thread Nathan M. Swan
Have you checked that your web server has write access to 
/Users/nathanmswan/Sites/ ?


Yes, it works now, thanks!
NMS

P.S. Sorry this might be in the wrong forum, but now I can 
advertise my homepage as index.d instead of compiling it and 
having it be index.cgi


Re: Pointer to variables in D

2012-04-25 Thread Nathan M. Swan
On Thursday, 26 April 2012 at 02:43:35 UTC, Victor Vicente de 
Carvalho wrote:

Hi there,

In c++ one can access a pointer to a class/struct variable 
using this semantic:


struct C {
 int x;
};

int C::* ptr = C::x;

C foo;

foo.*ptr = 10;
assert(foo.x == 10);

It is possible to do something like that on D? I've searched 
through the forum  documentation but didn't found anything.



This is the closest thing:

---
struct C {
 int x;
 int* ptr() @property { return x; }
}

C foo;
*foo.ptr = 10;
assert(foo.x = 10);
---

Also, the reason of this is that I'm studying a way to map a 
POD structure based from a dynamic, data-driven structure. 
Something like get a JSON and map it to a structure 
automagically. There is a nicer way to do that in D?


Yes. My rule: don't use pointers unless doing it otherwise is 
really slow or really hard.


Re: Bitarray size limits

2012-04-25 Thread ixid

Thank you, I will have a play with that.


Re: Pointer to variables in D

2012-04-25 Thread EraScarecrow

On Thursday, 26 April 2012 at 03:54:25 UTC, Nathan M. Swan wrote:
Yes. My rule: don't use pointers unless doing it otherwise is 
really slow or really hard.


 Might be safe to use a pointer on an immutable struct (Say, 
globally accessible data?). I'm trying this; but it's only to get 
around the need to copy the variable when in fact since the data 
never changes why should I use more space by copying it?


 Not the best idea, but if the data never changes, memory isn't 
manually managed, don't do pointer arithmetic, then it should be 
safe (Long as it's not local variable data).


 That said, I agree, avoid pointers as much as you can.


Re: Pointer to variables in D

2012-04-25 Thread H. S. Teoh
On Thu, Apr 26, 2012 at 06:57:50AM +0200, EraScarecrow wrote:
 On Thursday, 26 April 2012 at 03:54:25 UTC, Nathan M. Swan wrote:
 Yes. My rule: don't use pointers unless doing it otherwise is
 really slow or really hard.
 
  Might be safe to use a pointer on an immutable struct (Say,
 globally accessible data?). I'm trying this; but it's only to get
 around the need to copy the variable when in fact since the data
 never changes why should I use more space by copying it?
 
  Not the best idea, but if the data never changes, memory isn't
 manually managed, don't do pointer arithmetic, then it should be
 safe (Long as it's not local variable data).
 
  That said, I agree, avoid pointers as much as you can.

I don't know about deliberately *avoiding* pointers, but D's awesome
features are such that you hardly ever *need* to use pointers. There are
some situations where it's called for; there's nothing wrong with using
it in those cases. Besides, D's auto-deferencing semantics on pointers
makes the difference barely noticeable anyway. Pointers auto-deference
when using '.' member notation, for example. Once in a while you have to
explicitly dereference a pointer or take an address, but those are very
rare.


T

-- 
A mathematician is a device for turning coffee into theorems. -- P. Erdos


Re: Pointer to variables in D

2012-04-25 Thread Era Scarecrow

On Thursday, 26 April 2012 at 05:16:51 UTC, H. S. Teoh wrote:

Besides, D's auto-deferencing semantics on  pointers
makes the difference barely noticeable anyway. Pointers 
auto-deference when using '.' member notation, for example. Once
in a while  you have to explicitly dereference a pointer or 
take an address, but those are very rare.


 I was noticing that. Thankfully in the case where I'm using it, 
I have maybe 4 dereferences, total, and likely most aren't needed 
if I go through them again. To paraphrase Andrei (from memory): 
'D is the only language that consistently does the right thing 
(most of the time)'


 Perhaps pointers can be considered 'finer control' in some cases 
with auto-dereference. The more the language changes and gets 
better, the more and more I want to see it become more popular.



 Although not directly related, I'm finding it funny how C++ is 
finally catching up on some of the features D has; But will 
always fall short (auto, lambdas, etc; and heavy discourage of 
using delete now)


Re: Power of D

2012-04-25 Thread Era Scarecrow

On Wednesday, 25 April 2012 at 17:52:36 UTC, bioinfornatics wrote:
i search some example of something easy (more easy)  to do in D 
an not

in another language if possible
- D - C++
- D - Haskell
- D - Java
- D - python

thanks a lot


 Associative arrays?

C++:
#include map
#include string

mapstring, string m;

Java:
import java.util.*;


MapString, String map = new HashMapString, String();

D:
string[string] map

(Don't know the other two... sorry)
--

 Source/syntax parsing (compared to C++ involving 
templates/generics).


 Let's see, what else. Utility functions as pseudo members? Since 
in other language you'd have to include all utility functions in 
with the class, and try to give it all functionality right away. 
Anything your missing you may have to inherit from another class 
(with those functions) or call as regular functions.