SubClass[] does not implicitly convert to SuperClass[], why?

2015-02-20 Thread Tobias Pankrath via Digitalmars-d-learn

What's the reason behind this design?

class Super {}
class Sub : Super {}

void foo(Super[] sup) {}

void main() {
Sub[] array;
foo(array); // error, cannot call foo(Super[]) with arguments 
(Sub[])

}


Re: GC deadlocks on linux

2015-02-20 Thread Kagamin via Digitalmars-d-learn
I think, it's better to diagnose, what problem the program 
encounters, than to explain, what happens in kernel and glibc. 
The first step is to see, where it hangs and get a stacktrace.


Re: SubClass[] does not implicitly convert to SuperClass[], why?

2015-02-20 Thread rumbu via Digitalmars-d-learn
On Friday, 20 February 2015 at 07:57:17 UTC, Tobias Pankrath 
wrote:

What's the reason behind this design?

class Super {}
class Sub : Super {}

void foo(Super[] sup) {}

void main() {
Sub[] array;
foo(array); // error, cannot call foo(Super[]) with 
arguments (Sub[])

}


Just make the sup parameter const:

void foo(in Super[] sup) {}

http://dlang.org/arrays.html (end of the page):

A dynamic array T[] can be implicitly converted to one of the 
following:

const(U)[]
const(U[])
Where U is a base class of T.

The reson behind the design - I wonder about that also.


Re: SubClass[] does not implicitly convert to SuperClass[], why?

2015-02-20 Thread Tobias Pankrath via Digitalmars-d-learn

On Friday, 20 February 2015 at 08:25:49 UTC, rumbu wrote:
On Friday, 20 February 2015 at 07:57:17 UTC, Tobias Pankrath 
wrote:

What's the reason behind this design?

class Super {}
class Sub : Super {}

void foo(Super[] sup) {}

void main() {
   Sub[] array;
   foo(array); // error, cannot call foo(Super[]) with 
arguments (Sub[])

}


Just make the sup parameter const:

void foo(in Super[] sup) {}

http://dlang.org/arrays.html (end of the page):

A dynamic array T[] can be implicitly converted to one of the 
following:

const(U)[]
const(U[])
Where U is a base class of T.

The reson behind the design - I wonder about that also.


Thanks, didn't know that. Makes sense.

Probably the reason is:

void foo(Super[] sup) { sup[3] = new AnotherDerivedClass(); }

After foo returns the slice passed as argument would violate its 
invariant.


Re: GC deadlocks on linux

2015-02-20 Thread via Digitalmars-d-learn
On Thursday, 19 February 2015 at 22:12:03 UTC, Steven 
Schveighoffer wrote:
I'm not sure what the issue here is, but I don't think forking 
is as unstable as you seem to think, or maybe I'm reading this 
wrong. I can agree there are many gotchas with forking.


You need to very carefully configure what happens to resources 
before/after forking. So if you use third party libraries with 
threading all bets are off... If you fork early in the process' 
lifespan, before acquiring resources, then it is much easier...


http://linux.die.net/man/2/fork


Re: SubClass[] does not implicitly convert to SuperClass[], why?

2015-02-20 Thread Jonathan M Davis via Digitalmars-d-learn
On Friday, February 20, 2015 08:25:48 rumbu via Digitalmars-d-learn wrote:
> On Friday, 20 February 2015 at 07:57:17 UTC, Tobias Pankrath
> wrote:
> > What's the reason behind this design?
> >
> > class Super {}
> > class Sub : Super {}
> >
> > void foo(Super[] sup) {}
> >
> > void main() {
> > Sub[] array;
> > foo(array); // error, cannot call foo(Super[]) with
> > arguments (Sub[])
> > }
>
> Just make the sup parameter const:
>
> void foo(in Super[] sup) {}
>
> http://dlang.org/arrays.html (end of the page):
>
> A dynamic array T[] can be implicitly converted to one of the
> following:
> const(U)[]
> const(U[])
> Where U is a base class of T.
>
> The reson behind the design - I wonder about that also.

Well, if it were legal, you could do something like

class Super {}
class Sub : Super {}
class Sub2 : Super {}

Sub[] subArr = createSubArr();
Super[] superArr = subArr;
superArr[2] = new Sub2;

Now, all of a sudden, subArr[2] is a Sub2, when a Sub2 is not a Sub. So, you
would have broken the type system. The issue is called array covariance.
const avoids the problem, because you can't assign any of the elements of
the array.

A similar question was asked an answered previously on SO:

http://stackoverflow.com/questions/18158106/array-of-concrete-class-not-covariant-with-array-of-interface

though it looks like at the time that the questioned was answered, the
conversion didn't work with const, so things have improved since then. But
it should never work with mutable arrays or the type system would be
violated.

- Jonathan M Davis



Re: C++ calling convention only

2015-02-20 Thread Benjamin Thaut via Digitalmars-d-learn

On Thursday, 19 February 2015 at 21:34:57 UTC, John Colvin wrote:


I would duplicate the declaration, once without extern(C++), 
once with, the use the .mangleof from the 1st to set the mangle 
of the 2nd with pragma(mangle


Yes that would work. But using pragma(mangle) feels so hacky...


Re: GC deadlocks on linux

2015-02-20 Thread ketmar via Digitalmars-d-learn
On Fri, 20 Feb 2015 08:03:00 +, Kagamin wrote:

> I think, it's better to diagnose, what problem the program encounters,
> than to explain, what happens in kernel and glibc.
> The first step is to see, where it hangs and get a stacktrace.

this way of thinking is exactly why i recommend to avoid `fork()` unless 
one can explain *everything* it does.

signature.asc
Description: PGP signature


Re: GC deadlocks on linux

2015-02-20 Thread ketmar via Digitalmars-d-learn
On Fri, 20 Feb 2015 09:04:29 +, Ola Fosheim Grøstad wrote:

> If you fork early in the process'
> lifespan, before acquiring resources, then it is much easier...

and even if `fork()` is the first line of code in `main()`, there cannot 
be any guarantees. there can be module constructor doing something, or 
3rd party library that already initialized something, or... you got the 
idea. ;-)

signature.asc
Description: PGP signature


Re: SubClass[] does not implicitly convert to SuperClass[], why?

2015-02-20 Thread rumbu via Digitalmars-d-learn
On Friday, 20 February 2015 at 10:41:04 UTC, Jonathan M Davis 
wrote:


class Super {}
class Sub : Super {}
class Sub2 : Super {}

Sub[] subArr = createSubArr();
Super[] superArr = subArr;
superArr[2] = new Sub2;

Now, all of a sudden, subArr[2] is a Sub2, when a Sub2 is not a 
Sub. So, you
would have broken the type system. The issue is called array 
covariance.
const avoids the problem, because you can't assign any of the 
elements of

the array.



A simple cast will always permit to destroy the type system 
entirely:


class Super {}

class Sub : Super {
string alpha() { return "I am Sub"; }
void SomeMethodNotFoundInSub2() { }
}

class Sub2: Super {
string beta() { return "I am Sub2"; }
}

void foo(in Super[] sup)
{
cast(Super)sup[0] = new Sub();
cast(Super)sup[1] = new Sub2();
}


int main(string[] argv)
{

Sub[] subArr = new Sub[2];
foo(subArr);

writeln(subArr[0]);
	writeln(subArr[1]); //look ma', my Sub[] array contains Sub2 
elements


writeln(subArr[0].alpha());
	writeln(subArr[1].alpha()); //look ma', I'm calling beta of 
Sub2, because it has the same vtbl offset as alpha in Sub


subArr[0].SomeMethodNotFoundInSub2(); //ok
	subArr[1].SomeMethodNotFoundInSub2(); // now we have an 
AccesViolation or maybe not, depending on Sub2 contents.


getchar();
return 0;
}

The problem is in fact the line below, and that kind of 
assignment must be checked at runtime instead of limiting compile 
time features that can be anyway circumvented, by throwing a 
specific exception.


sup[1] = new Sub2();






Re: C++ calling convention only

2015-02-20 Thread John Colvin via Digitalmars-d-learn

On Friday, 20 February 2015 at 12:23:31 UTC, Benjamin Thaut wrote:
On Thursday, 19 February 2015 at 21:34:57 UTC, John Colvin 
wrote:


I would duplicate the declaration, once without extern(C++), 
once with, the use the .mangleof from the 1st to set the 
mangle of the 2nd with pragma(mangle


Yes that would work. But using pragma(mangle) feels so hacky...


I agree. Wrap it in a mixin / mixin template?

Why do you need this? Presumably it'll be hidden in the depths of 
some library / bindings where beauty is somewhat optional? Using 
the .mangleof from an extern(D) function should mean it's robust.


Re: SubClass[] does not implicitly convert to SuperClass[], why?

2015-02-20 Thread ketmar via Digitalmars-d-learn
On Fri, 20 Feb 2015 12:58:01 +, rumbu wrote:

> A simple cast will always permit to destroy the type system entirely:

`cast` is the thing programmer does explicitly. it's assumed that 
programmer is know what he is doing, and he takes full responsibility for 
that.

hidden type system breakage is completely different thing.

moving checks to runtime turns language to JS.

signature.asc
Description: PGP signature


Re: C++ calling convention only

2015-02-20 Thread Benjamin Thaut via Digitalmars-d-learn

On Friday, 20 February 2015 at 13:00:39 UTC, John Colvin wrote:


I agree. Wrap it in a mixin / mixin template?

Why do you need this? Presumably it'll be hidden in the depths 
of some library / bindings where beauty is somewhat optional? 
Using the .mangleof from an extern(D) function should mean it's 
robust.


Well the use case is creating a function which sole purpose it is 
to create a function pointer from it and pass it to C++. If it 
recieves C++ mangling however I have to pay attention that it 
does not conflict with any other C++ symbols. The same goes for 
extern(C). Sometimes you want to create functions with a C 
calling convetion so you can create a function pointer from it. 
With extern(C) its even a bigger problem because the C mangling 
conflicts a lot easier.


Re: GC deadlocks on linux

2015-02-20 Thread Martin Nowak via Digitalmars-d-learn

On Wednesday, 18 February 2015 at 20:27:08 UTC, Byron Heads wrote:

Adding core.memory.GC.disable; to main causes the application to
work correctly (and quickly till it runs out of memory :D )


GC.disable shouldn't run OOM, BTW.
http://dlang.org/phobos/core_memory.html#.GC.disable


Re: GC deadlocks on linux

2015-02-20 Thread Martin Nowak via Digitalmars-d-learn

On Wednesday, 18 February 2015 at 20:27:08 UTC, Byron Heads wrote:

I have a medium size daemon application that uses several
threads, libasync, and daemonize.  On windows it runs correctly
with GC enabled, but on linux the GC causes a deadlock while
allocating memory.


Can you reliably reproduce the deadlock?
If so please attach a gdb to the deadlocked process and provide 
us back traces of all threads? If you can share the code (maybe 
privately), that would help as well.


Re: GC deadlocks on linux

2015-02-20 Thread Martin Nowak via Digitalmars-d-learn

On Wednesday, 18 February 2015 at 20:41:12 UTC, Dicebot wrote:

Any chance you are using gdm-3.12.x?

I was so mad when I have encountered this:
https://issues.dlang.org/show_bug.cgi?id=4890


Indeed, maybe 
http://dlang.org/phobos-prerelease/core_thread.html#.thread_setGCSignals 
might help.
We should probably try to improve druntime to only use a single 
signal for suspending and resuming.


Re: GC deadlocks on linux

2015-02-20 Thread via Digitalmars-d-learn

On Friday, 20 February 2015 at 12:47:58 UTC, ketmar wrote:
3rd party library that already initialized something, or... you 
got the idea. ;-)


Yeah, either use plain C or avoid 3rd party libraries... I guess 
that includes phobos ;)


Re: GC deadlocks on linux

2015-02-20 Thread MartinNowak via Digitalmars-d-learn

On Wednesday, 18 February 2015 at 20:27:08 UTC, Byron Heads wrote:

System:
DMD 2.066.1


Can you also try the latest beta please, maybe we already fixed 
something.

http://forum.dlang.org/post/54e49e2d.2010...@dawg.eu


Re: vibe-d basic build errors

2015-02-20 Thread MartinNowak via Digitalmars-d-learn

On Friday, 20 February 2015 at 04:48:09 UTC, Charles wrote:

They're installer versions, dub is 0.9.22 Nov 22 I want to say,
and DMD is 2.066.1


Same ones I tried.

With --force dmd seems to fail but there is not output.
Can you run only the link command with a -v to get verbose dmd 
output?


dmd -c 
-of.dub\build\application-debug-windows-x86-dmd_2066-7FF336D92D4F5796EA8623FC9A6A9B90\web.obj 
-debug -g -w -version=VibeDefaultMain -version=VibeWin32Driver 
-version=Have_web -version=Have_vibe_d -version=Have_openssl 
-Isource 
-IC:\Users\Charles\AppData\Roaming\dub\packages\vibe-d-0.7.22\source 
-IC:\Users\Charles\AppData\Roaming\dub\packages\openssl-1.1.3_1.0.1g 
-Jviews source\app.d 
C:\Users\Charles\AppData\Roaming\dub\packages\vibe-d-0.7.22\source\vibe\appmain.d


Re: GC deadlocks on linux

2015-02-20 Thread ketmar via Digitalmars-d-learn
On Fri, 20 Feb 2015 14:33:29 +, Ola Fosheim Grøstad wrote:

> On Friday, 20 February 2015 at 12:47:58 UTC, ketmar wrote:
>> 3rd party library that already initialized something, or... you got the
>> idea. ;-)
> 
> Yeah, either use plain C or avoid 3rd party libraries... I guess that
> includes phobos ;)

maybe even druntime to some extent. and, of course, no other D libraries, 
as who knows what they can do in their module initialisers...

signature.asc
Description: PGP signature


Re: GC deadlocks on linux

2015-02-20 Thread Martin Nowak via Digitalmars-d-learn

On 02/18/2015 09:35 PM, Byron Heads wrote:





I am in the daemonize library

https://github.com/NCrashed/daemonize


Might want to try using libasync without multiple threads.
http://www.linuxprogrammingblog.com/threads-and-fork-think-twice-before-using-them


Re: GC deadlocks on linux

2015-02-20 Thread MartinNowak via Digitalmars-d-learn

On Friday, 20 February 2015 at 15:17:22 UTC, Martin Nowak wrote:

Might want to try using libasync without multiple threads.
http://www.linuxprogrammingblog.com/threads-and-fork-think-twice-before-using-them


Or you temporarily disable the GC before forking and enable it 
again afterwards.


Re: How to make Application bundle from Executable? (Mac)

2015-02-20 Thread Israel via Digitalmars-d-learn

On Friday, 20 February 2015 at 06:19:29 UTC, Gan wrote:
On Friday, 20 February 2015 at 06:10:51 UTC, Nicholas Wilson 
wrote:

On Friday, 20 February 2015 at 03:26:47 UTC, Gan wrote:
Also I can't get my application to load images that I place 
in the Resources folder(or any folder in the bundle for that 
matter).
I suggest to have a look at the projects generated by SFML 
regarding locating the resources in C++/ObjC and translate 
them to C/ObjC/D.
As for code (i.e frameworks and .dylibs) i don't know as 
shared libraries are still a murky area for D. Probably just 
better to stick to static libs.
Is there an official way to turn a D executable into a Mac 
Application Bundle?

Dunno

Good luck!


Frameworks aren't an issue, I put them into the Frameworks 
folder in my hand made bundle and they load fine.
When running the executable, it loads the image when it's in 
the same folder but when running the executable through the 
bundle, it doesn't find the image anywhere.


It seems OSX is doing some weird stuff then...


Re: How to make Application bundle from Executable? (Mac)

2015-02-20 Thread Nicholas Wilson via Digitalmars-d-learn

On Friday, 20 February 2015 at 06:19:29 UTC, Gan wrote:
On Friday, 20 February 2015 at 06:10:51 UTC, Nicholas Wilson 
wrote:

On Friday, 20 February 2015 at 03:26:47 UTC, Gan wrote:
Also I can't get my application to load images that I place 
in the Resources folder(or any folder in the bundle for that 
matter).
I suggest to have a look at the projects generated by SFML 
regarding locating the resources in C++/ObjC and translate 
them to C/ObjC/D.
As for code (i.e frameworks and .dylibs) i don't know as 
shared libraries are still a murky area for D. Probably just 
better to stick to static libs.
Is there an official way to turn a D executable into a Mac 
Application Bundle?

Dunno

Good luck!


Frameworks aren't an issue, I put them into the Frameworks 
folder in my hand made bundle and they load fine.
When running the executable, it loads the image when it's in 
the same folder but when running the executable through the 
bundle, it doesn't find the image anywhere.


are you loading using relative or absolute addresses ( 
../../Resources/img.png or $BUNDLE_ROOT/Resources/img.png ) ?
also check the cwd when launched from the executable vs, the 
bundle. Also does Console give any output?


Disallow destroy(structPtr)?

2015-02-20 Thread Nick Treleaven via Digitalmars-d-learn

Hi,

The following code is supposed to destroy the struct instance:

import std.stdio;
struct S{
~this(){"destruct".writeln;}
}
auto p = new S;
destroy(p);
"end".writeln;

It works correctly if I use destroy(*p), but the above code could 
perhaps be statically rejected by object.destroy to help prevent bugs. 
Currently, the pointer p is set to null without calling the destructor 
(with recent dmd the destructor is called, but only after "end" is printed).


Here is the destroy overload:

void destroy(T)(ref T obj)
if (!is(T == struct) && !is(T == interface) && !is(T == class) && 
!_isStaticArray!T)

{
obj = T.init;
}


Re: How to make Application bundle from Executable? (Mac)

2015-02-20 Thread Gan via Digitalmars-d-learn
On Friday, 20 February 2015 at 17:28:48 UTC, Nicholas Wilson 
wrote:

On Friday, 20 February 2015 at 06:19:29 UTC, Gan wrote:
On Friday, 20 February 2015 at 06:10:51 UTC, Nicholas Wilson 
wrote:

On Friday, 20 February 2015 at 03:26:47 UTC, Gan wrote:
Also I can't get my application to load images that I place 
in the Resources folder(or any folder in the bundle for that 
matter).
I suggest to have a look at the projects generated by SFML 
regarding locating the resources in C++/ObjC and translate 
them to C/ObjC/D.
As for code (i.e frameworks and .dylibs) i don't know as 
shared libraries are still a murky area for D. Probably just 
better to stick to static libs.
Is there an official way to turn a D executable into a Mac 
Application Bundle?

Dunno

Good luck!


Frameworks aren't an issue, I put them into the Frameworks 
folder in my hand made bundle and they load fine.
When running the executable, it loads the image when it's in 
the same folder but when running the executable through the 
bundle, it doesn't find the image anywhere.


are you loading using relative or absolute addresses ( 
../../Resources/img.png or $BUNDLE_ROOT/Resources/img.png ) ?
also check the cwd when launched from the executable vs, the 
bundle. Also does Console give any output?


I fixed it by using:
string path = thisExePath();
int index = to!int(path.lastIndexOf("/"));
if (!GameFont.loadFromFile(path[0 .. index]~"/vertiup2.ttf")) {
writeln("Font failed to load");
}

Now my hand made bundle works fine and loads resources from 
inside the bundle.


Re: Disallow destroy(structPtr)?

2015-02-20 Thread Steven Schveighoffer via Digitalmars-d-learn

On 2/20/15 1:05 PM, Nick Treleaven wrote:

Hi,

The following code is supposed to destroy the struct instance:

 import std.stdio;
 struct S{
 ~this(){"destruct".writeln;}
 }
 auto p = new S;
 destroy(p);
 "end".writeln;

It works correctly if I use destroy(*p), but the above code could
perhaps be statically rejected by object.destroy to help prevent bugs.
Currently, the pointer p is set to null without calling the destructor
(with recent dmd the destructor is called, but only after "end" is
printed).

Here is the destroy overload:

void destroy(T)(ref T obj)
 if (!is(T == struct) && !is(T == interface) && !is(T == class) &&
!_isStaticArray!T)
{
 obj = T.init;
}


I'm beginning to think this is the right thing to do. It confuses so 
many people, and setting a pointer/class reference/array to null is easy 
enough without needing a special function to do it. In other words, if 
you are using destroy, you aren't just trying to nullify a pointer. You 
want to destroy what the pointer represents.


The only problem is, how does this affect existing code?

-Steve


To GC or Not To GC in std.container.*

2015-02-20 Thread Nordlöw

What's the policy on using GC or not in std.container.* ?

- std.container.Array uses malloc for its allocation but
- RedBlackTree.allocate() returns a: new RBNode!Elem*

Is this because RBNode* should be reachable outside of 
RedBlackTree or is this a todo?


Re: GC deadlocks on linux

2015-02-20 Thread Kagamin via Digitalmars-d-learn
On Friday, 20 February 2015 at 14:33:31 UTC, Ola Fosheim Grøstad 
wrote:
Yeah, either use plain C or avoid 3rd party libraries... I 
guess that includes phobos ;)


AFAIK, in early days of unix there were no threads, processes 
were single-threaded, fork was the way to concurrency and exec 
was the most efficient way to run a program in memory-constrained 
conditions of 70s (kbytes of RAM!). Plain unix-like 
single-threaded processes, plain C heap, which didn't serialize 
access, because no threads, and worked fine with just virtual 
memory, which fork got right. That's the model, which should work 
the best with fork+exec.


Re: Undefined symbol?

2015-02-20 Thread Kagamin via Digitalmars-d-learn

https://issues.dlang.org/show_bug.cgi?id=13172


Re: State of Windows x64 COFF support?

2015-02-20 Thread Kagamin via Digitalmars-d-learn
Implementations can have bugs, probably COFF support wasn't 
stress tested.


Re: GC deadlocks on linux

2015-02-20 Thread via Digitalmars-d-learn

On Friday, 20 February 2015 at 22:07:56 UTC, Kagamin wrote:
AFAIK, in early days of unix there were no threads, processes 
were single-threaded, fork was the way to concurrency and exec 
was the most efficient way to run a program in 
memory-constrained conditions of 70s (kbytes of RAM!). Plain 
unix-like single-threaded processes, plain C heap, which didn't 
serialize access, because no threads, and worked fine with just 
virtual memory, which fork got right. That's the model, which 
should work the best with fork+exec.


Indeed, actually, not only for the early days, but for the first 
20 years or so! :-D


Single thread, C,  fork and a pipe + limited use of shared memory 
is a quite clean model. The underlying principle in Unix is to 
build complex software from many isolated simple units.


This robust philosophy somehow got lost in the quest for bleeding 
edge.


Re: Disallow destroy(structPtr)?

2015-02-20 Thread Ali Çehreli via Digitalmars-d-learn

On 02/20/2015 12:30 PM, Steven Schveighoffer wrote:

> On 2/20/15 1:05 PM, Nick Treleaven wrote:

>> It works correctly if I use destroy(*p), but the above code could
>> perhaps be statically rejected by object.destroy to help prevent bugs.

> I'm beginning to think this is the right thing to do. It confuses so
> many people, and setting a pointer/class reference/array to null is easy
> enough without needing a special function to do it. In other words, if
> you are using destroy, you aren't just trying to nullify a pointer. You
> want to destroy what the pointer represents.
>
> The only problem is, how does this affect existing code?

And templated containers...

Despite the issue, I favor the current behavior partly because I am used 
to it from C++: A pointer going out of scope does not delete what it 
points to. (It can't do that because the pointer does not know about the 
object's ownership.)


Ali



reflect on this function

2015-02-20 Thread Vlad Levenfeld via Digitalmars-d-learn

I'd like to do something like this:

  @reflexive @transitive bool relation (T)(T a, T b)
  out (result) {
mixin(property_verification!result);
  }
  body {
...
  }

which becomes

  out (result) {
 // generated from @reflexive
assert (result == skip_contract!relation (b,a));

// generated from @transitive
static typeof(result) c;
if (result)
  assert (skip_contract!relation (b,c) == 
skip_contract!relation (a,c));

c = b;
  }

or something like that. I don't see a way to get exactly this, 
but does anyone have any thoughts on something similar?


Re: reflect on this function

2015-02-20 Thread ketmar via Digitalmars-d-learn
On Fri, 20 Feb 2015 22:32:53 +, Vlad Levenfeld wrote:

> I'd like to do something like this:
> 
>@reflexive @transitive bool relation (T)(T a, T b)
>out (result) {
>  mixin(property_verification!result);
>}
>body {
>  ...
>}
> 
> which becomes
> 
>out (result) {
>   // generated from @reflexive
>  assert (result == skip_contract!relation (b,a));
> 
>  // generated from @transitive static typeof(result) c;
>  if (result)
>assert (skip_contract!relation (b,c) ==
> skip_contract!relation (a,c));
>  c = b;
>}
> 
> or something like that. I don't see a way to get exactly this,
> but does anyone have any thoughts on something similar?

can you go with `relationImpl` and mixin/template that generates 
`relation` with contract then? something like:

  @reflexive @transitive bool relationImpl (T)(T a, T b) { ... }
  alias relation = buildWithContracts!relationImpl;

then you can simply call `relationImpl` in your out section.

sorry if i didn't understand what you want and just throws in some noise.

signature.asc
Description: PGP signature


Re: GC deadlocks on linux

2015-02-20 Thread ketmar via Digitalmars-d-learn
On Fri, 20 Feb 2015 22:29:04 +, Ola Fosheim Grøstad wrote:

> This robust philosophy somehow got lost in the quest for bleeding edge.

i still missing it. sure, we can write our code in this style today, but 
with all that libraries that can create threads without you knowing about 
it (heh, have you ever used `getaddrinfo_a(GAI_NOWAIT)`? a hideous 
"solution"!)...

signature.asc
Description: PGP signature


Re: reflect on this function

2015-02-20 Thread Vlad Levenfeld via Digitalmars-d-learn

On Friday, 20 February 2015 at 22:44:35 UTC, ketmar wrote:

can you go with `relationImpl` and mixin/template that generates
`relation` with contract then? something like:

  @reflexive @transitive bool relationImpl (T)(T a, T b) { ... }
  alias relation = buildWithContracts!relationImpl;

then you can simply call `relationImpl` in your out section.


Yeah, this looks pretty good. As much as I hate the pimpl idiom, 
having a one-liner alias right next to the definition should 
minimize the eye-bleeding. Thanks!


Compiler instrinsics

2015-02-20 Thread rumbu via Digitalmars-d-learn

Is there a complete list of DMD compiler intrinsics?

I searched the compiler source code but they seem scattered 
around. I discovered some of them (strlen, memcpy, strcmp, etc) 
but I suppose there is a list somewhere.


Re: reflect on this function

2015-02-20 Thread Ali Çehreli via Digitalmars-d-learn

On 02/20/2015 02:32 PM, Vlad Levenfeld wrote:

I'd like to do something like this:

   @reflexive @transitive bool relation (T)(T a, T b)
   out (result) {
 mixin(property_verification!result);
   }
   body {
 ...
   }

which becomes

   out (result) {
  // generated from @reflexive
 assert (result == skip_contract!relation (b,a));

 // generated from @transitive
 static typeof(result) c;
 if (result)
   assert (skip_contract!relation (b,c) == skip_contract!relation
(a,c));
 c = b;
   }

or something like that. I don't see a way to get exactly this, but does
anyone have any thoughts on something similar?


Apparently, __FUNCTION__ is valid in an out block:

import std.stdio;
import std.string;

struct reflexive
{}

struct transitive
{}

string property_verification(alias var)(string func = __FUNCTION__)
{
return format(
`writefln("We are in %s; and the value of '%s' is '%%s'.", %s);`,
func, var.stringof, var.stringof);
}

@reflexive @transitive bool relation (T)(T a, T b)
out (result) {
mixin(property_verification!result);

} body {
return false;
}

void main()
{
int a, b;
relation(a, b);
}

The output printed inside the out block:

We are in deneme.relation!int.relation; and the value of 'result' is 
'false'.


Ali



Re: reflect on this function

2015-02-20 Thread ketmar via Digitalmars-d-learn
On Fri, 20 Feb 2015 22:51:21 +, Vlad Levenfeld wrote:

> On Friday, 20 February 2015 at 22:44:35 UTC, ketmar wrote:
>> can you go with `relationImpl` and mixin/template that generates
>> `relation` with contract then? something like:
>>
>>   @reflexive @transitive bool relationImpl (T)(T a, T b) { ... }
>>   alias relation = buildWithContracts!relationImpl;
>>
>> then you can simply call `relationImpl` in your out section.
> 
> Yeah, this looks pretty good. As much as I hate the pimpl idiom,
> having a one-liner alias right next to the definition should minimize
> the eye-bleeding. Thanks!

and you can have an alias even before definition! ;-) this way it will 
not lost.

signature.asc
Description: PGP signature


const member function

2015-02-20 Thread rumbu via Digitalmars-d-learn

Often I'm using the following code pattern:

class S
{
   private SomeType cache;

   public SomeType SomeProp() @property
   {
  if (cache is null)
cache = SomeExpensiveOperation();
  return cache;
   }
}

Is there any way to mark SomeProp() as const? Because I want to 
call somewhere const(S).SomeProp, which for the outside world is 
must be "const", returning just a value, even that internaly it 
modifies the internal S structure.


Re: const member function

2015-02-20 Thread Baz via Digitalmars-d-learn

---
class S
{
private SomeType cache;

public const(SomeType) SomeProp() @property
{
   if (cache is null)
 cache = SomeExpensiveOperation();
   return cache;
}
}
---

the result of the getter will be read-only


Re: const member function

2015-02-20 Thread rumbu via Digitalmars-d-learn

On Saturday, 21 February 2015 at 07:01:12 UTC, Baz wrote:

---
class S
{
private SomeType cache;

public const(SomeType) SomeProp() @property
{
   if (cache is null)
 cache = SomeExpensiveOperation();
   return cache;
}
}
---

the result of the getter will be read-only


My intention is not to have a read-only getter, I want to call 
SomeProp on a const object:


class S
{
private int cache = -1;
private int SomeExpensiveOp() { return 12345; }

public @property const(int) SomeProp()
{
if (cache = -1)
cache = SomeExpensiveOp();
return cache;
}
}

unittest
{
const(S) s = new S();
auto i = s.SomeProp;  //mutable method S.SomeProp is not 
callable using a const object

}