Re: D - Unsafe and doomed

2014-01-06 Thread Organic Farmer

Just found out that when I replace

struct NotNull(T) { T obj; }

with (http://arsdnet.net/dcode/notnull.d)'s definition of NotNull 
it all makes sense.


Greets.


Re: D - Unsafe and doomed

2014-01-05 Thread Organic Farmer
On Saturday, 4 January 2014 at 22:08:59 UTC, Andrej Mitrovic 
wrote:

On 1/4/14, Adam D. Ruppe destructiona...@gmail.com wrote:

The big thing people have asked for before is

Object foo;
if(auto obj = checkNull(foo)) {
obj == NotNull!Object
} else {
   // foo is null
}

and i haven't figured that out yet...


Here you go:

-
import std.stdio;

struct NotNull(T) { T obj; }

struct CheckNull(T)
{
private T _payload;

auto opCast(X = bool)() { return _payload !is null; }

@property NotNull!T getNotNull() { return 
NotNull!T(_payload); }

alias getNotNull this;
}

CheckNull!T checkNull(T)(T obj)
{
return CheckNull!T(obj);
}

class C { }

void main()
{
Object foo;
if (auto obj = checkNull(foo))
{
writeln(foo is not null);
}
else
{
writeln(foo is null);
}

foo = new C;

if (auto obj = checkNull(foo))
{
// note: : rather than == due to alias this.
static assert(is(typeof(obj) : NotNull!Object));

// assignment will work of course (alias this)
NotNull!Object obj2 = obj;

writeln(foo is not null);
}
else
{
writeln(foo is null);
}
}
-



Excuse me, not so fast ...

1. static assert asserts at compile time, so let's not put it in 
a runtime check of obj (converted to bool).


2.
Object foo;
assert(foo is null);
NotNull!Object test = NotNull!Object(foo);
assert(is(typeof(test) : NotNull!Object));
static assert(is(typeof(test) : NotNull!Object));

always passes for a null reference, with or without static, as it 
should. So what good is NotNull?


3. To wit: if you replace main with

void main() {
Object foo;

auto obj = checkNull(foo);
if (obj) {
static assert(is(typeof(obj) : NotNull!Object)); //let's 
leave this inside the dynamic if, just to demonstrate (yuck!)

assert(is(typeof(obj) : NotNull!Object));
NotNull!Object obj2 = obj;
writeln(foo is not null);
} else {
static assert(is(typeof(obj) : NotNull!Object));
assert(is(typeof(obj) : NotNull!Object));
writeln(foo is null);
}

foo = new C;

obj = checkNull(foo);
if (obj) {
static assert(is(typeof(obj) : NotNull!Object));
assert(is(typeof(obj) : NotNull!Object));
NotNull!Object obj2 = obj;
writeln(foo is not null);
} else {
static assert(is(typeof(obj) : NotNull!Object));
assert(is(typeof(obj) : NotNull!Object));
writeln(foo is null);
}
}

all asserts pass, be they static or dynamic. The output is 
correct as in the original, but it has nothing to do with obj 
being convertible to NotNull!Object or not. It always is. The 
output is correct due to the simple runtime check whether obj is 
null or not.


Greets.
(PS: I think I just poked me in the eye with my pencil.)


Re: D - Unsafe and doomed

2014-01-05 Thread Organic Farmer

* correct: whether obj converts to true or false


Re: DDOX search support and new 2.064.2 Phobos docs

2014-01-04 Thread Organic Farmer
Would be great to have template constraints back in the docs (see 
e.g. std.algorithm.all). Indispensable info on templates in and 
of themselves.


Gut gemacht!


Re: D - Unsafe and doomed

2014-01-04 Thread Organic Farmer
Oh my! My pencil is so *unsafe*. Whenever I try to write down 
this new no 1 chart hit, everybody just tells me my music sounds 
like crap.


Would someone here please provide me with a *safe* pencil?


Re: D - Unsafe and doomed

2014-01-04 Thread Organic Farmer
Are there any developers left who can afford to choose their 
programming language for its expressive power and not for the 
amount of safety nets it provides. That is why D became my #1 
language.


But I guess that's just someone speaking who, in the ol' days, 
didn't have a problem even in large C++ projects with matching 
each *new* at the start of a block with its *delete* at the end.


openMP

2012-10-02 Thread Farmer

Hi,
I am tempted to start D programming but for me it is crucrial to 
be able to parallelize for-loops as can be done with openMP for 
C/C++ (mainly @pragma omp parallel for, @pragma omp critical).
I have already seen the std.parallelism library but I'm unsure 
whether it can provide me with the same functionality.


Thanks


Re: openMP

2012-10-02 Thread Farmer

And is there also a pragma omp critical analogon?

On Tuesday, 2 October 2012 at 20:16:36 UTC, Peter Alexander wrote:

On Tuesday, 2 October 2012 at 19:15:19 UTC, Farmer wrote:

Hi,
I am tempted to start D programming but for me it is crucrial 
to be able to parallelize for-loops as can be done with openMP 
for C/C++ (mainly @pragma omp parallel for, @pragma omp 
critical).
I have already seen the std.parallelism library but I'm unsure 
whether it can provide me with the same functionality.


Thanks


It can. Here's an example from the docs of parallelising a 
simple for loop:


auto logs = new double[10_000_000];
foreach(i, ref elem; taskPool.parallel(logs, 100))
{
elem = log(i + 1.0);
}

This creates a pool of workers that each perform 100 iterations 
of the loop body in parallel.