Re: friends with phobos, workaround?

2015-09-10 Thread Daniel N via Digitalmars-d-learn

On Thursday, 10 September 2015 at 08:22:29 UTC, Daniel N wrote:

import std.typecons;

class Awesome1
{
private:
  int val;
  this(string caller = __MODULE__)(int val) if(caller == 
"std.conv") // Use scoped!Awesome

  {
this.val = val;
  }
}

class Awesome2
{
private:
  int val;
  this(string caller = __MODULE__)(int val)
  {
  	static assert(caller == "std.conv", "Use 
scoped!Awesome(...)!");


this.val = val;
  }
}

void main()
{
  static assert(__traits(compiles, scoped!Awesome1(1)));
  static assert(__traits(compiles, scoped!Awesome2(1)));
  static assert(!__traits(compiles, new Awesome1(1)));
  static assert(!__traits(compiles, new Awesome2(1)));
}


PS 'private:' should of course be 'public:' when using this idiom.



Re: using std.algorithm to find intersection of DateTime[][] arg

2015-09-10 Thread Artem Tarasov via Digitalmars-d-learn
On Wednesday, 9 September 2015 at 20:28:35 UTC, Laeeth Isharc 
wrote:


so setIntersection(arg[0],arg[1],arg[2] .. arg[$-1])
except that I don't know how many series are in arg at compile 
time.


what's the most efficient way to use Phobos to find these?  (I 
could write a loop, but I am trying to learn how to use 
std.algorithm better).


I'd use something like this, it works in O(Σ|arg[i]| * 
log|arg.length|)
arg.nWayUnion.group.filter!(g => g[1] == arg.length).map!(g => 
g[0]).array


Re: Status of Win32 C++ interop

2015-09-10 Thread Benjamin Thaut via Digitalmars-d-learn

On Tuesday, 8 September 2015 at 12:56:00 UTC, Laeeth Isharc wrote:



This is really very clear and helpful, and I appreciate your 
taking the time.  I will place it on the wiki if that's okay.


Thats ok.



Library support is surely one of the largest impediments to the 
adoption of D, and we ought to place some emphasis on updating 
the documentation here:

http://dlang.org/cpp_interface.html

How does it work when external APIs expect objects from the C++ 
standard library?  strings, and so on?  How about funny pointer 
types?  shared_ptr  etc?  std::vector, std::list?


For templated types like std::vector, std::list and shared_ptr 
you have two options:
- Redo the complete implementation on the D side ensuring that 
the data layout is the same
- Or, expose helper functions which call placement new / 
desturctor of your needed std::vector and do the hidden data 
trick I described above. The regular functions of std::vector 
just have to be declared and the linker will find them if and 
only if the c++ side instanciated the template.




Here is one C++ library used by many in finance (at least as a 
backup).  I think there might be a decent amount of value in 
making this usable from D.  (Trying to put my own interests 
aside!)  Paging Andy Smith ?


http://quantlib.org/reference/_bermudan_swaption_8cpp-example.html


I think you have to make this usable from D yourself ;-)



Are there any well-known C++ libraries that you have interfaced 
to ?  Could you give some examples of how long it takes ?


I'm interfacing to a 3D engine I'm working on in my spare time 
with some collegues. So no well known C++ library.




Would you be able to drop me an email about something else?  No 
contact info on your blog, but my domain is 
kaleidicassociates.com and my user id is laeeth@


Will do.

Kind Regards
Benjamin Thaut




Re: friends with phobos, workaround?

2015-09-10 Thread Daniel N via Digitalmars-d-learn

On Wednesday, 9 September 2015 at 23:44:14 UTC, Idan Arye wrote:
How about using a mixin 
template(http://dlang.org/template-mixin.html)?




Thanks, it's a good solution. My only reservation is I would 
prefer to find a way to directly invoke a symbol in std.* as 
otherwise different frameworks might invent their own 
conventions(well they might do that anyway). After a nights 
sleep, I actually found such a solution!


This opens the door to a new class of meta-programming; 
introspect the caller! :)


import std.typecons;

class Awesome1
{
private:
  int val;
  this(string caller = __MODULE__)(int val) if(caller == 
"std.conv") // Use scoped!Awesome

  {
this.val = val;
  }
}

class Awesome2
{
private:
  int val;
  this(string caller = __MODULE__)(int val)
  {
  	static assert(caller == "std.conv", "Use 
scoped!Awesome(...)!");


this.val = val;
  }
}

void main()
{
  static assert(__traits(compiles, scoped!Awesome1(1)));
  static assert(__traits(compiles, scoped!Awesome2(1)));
  static assert(!__traits(compiles, new Awesome1(1)));
  static assert(!__traits(compiles, new Awesome2(1)));
}





Re: using std.algorithm to find intersection of DateTime[][] arg

2015-09-10 Thread deed via Digitalmars-d-learn
On Wednesday, 9 September 2015 at 20:28:35 UTC, Laeeth Isharc 
wrote:

I have a DateTime[][] arg ...
I would like to find the intersection of the dates.


A suggestion:

auto minLength  = arg.map!(a => a.length).reduce!min;
auto minIdx = arg.map!(a => 
a.length).countUntil(minLength);
auto intersection   = arg[minIdx].filter!(e => 
chain(arg[0..minIdx], arg[minIdx..$]).all!(a => 
a.assumeSorted.contains(e)));


reduce with setIntersection seems the most straightforward, but 
needs array AFAIK, i.e.:


auto intersection =
//reduce!((r, x) => setIntersection(r, x))(arg);  // doesn't 
work
  reduce!((r, x) => setIntersection(r, x).array)(arg);// does, 
but with array




Re: What is the difference between D and C++ regarding Unique, RefCounted and Scoped?

2015-09-10 Thread Gary Willoughby via Digitalmars-d-learn

On Wednesday, 9 September 2015 at 23:22:49 UTC, ponce wrote:

- RefCounted

Only for D structs. std::shared_ptr works for all.


RefCounted works with classes as well.

http://dlang.org/phobos/std_typecons.html#.RefCounted


Re: What is the difference between D and C++ regarding Unique, RefCounted and Scoped?

2015-09-10 Thread Gary Willoughby via Digitalmars-d-learn
On Thursday, 10 September 2015 at 12:34:54 UTC, Daniel Kozák 
wrote:


On Thu, 10 Sep 2015 11:38:35 +
"Gary Willoughby"  wrote:


On Wednesday, 9 September 2015 at 23:22:49 UTC, ponce wrote:
> - RefCounted
>
> Only for D structs. std::shared_ptr works for all.

RefCounted works with classes as well.

http://dlang.org/phobos/std_typecons.html#.RefCounted


struct RefCounted(T, RefCountedAutoInitialize autoInit =
RefCountedAutoInitialize.yes) if (!is(T == class) && !is(T ==
interface));

if (!is(T == class) && !is(T ==interface)); // So it does not 
work

with classes


Sorry my mistake.


Re: friends with phobos, workaround?

2015-09-10 Thread Adam D. Ruppe via Digitalmars-d-learn

On Wednesday, 9 September 2015 at 23:44:14 UTC, Idan Arye wrote:

public:
mixin MakeUnique!(int);



I actually think that should be a free function in the module 
because then it can be used by derived classes too without having 
to mix it in each of them as well.


Re: friends with phobos, workaround?

2015-09-10 Thread Adam D. Ruppe via Digitalmars-d-learn

On Thursday, 10 September 2015 at 08:22:29 UTC, Daniel N wrote:
  this(string caller = __MODULE__)(int val) if(caller == 
"std.conv") // Use scoped!Awesome


That's disgustingly genius. I'm a bit jealous I didn't think 
of it myself!


One slight problem though: you couldn't call super() from a 
derived class, since the constructor wouldn't even exist due to 
the constraint.


You could just put the hard work in a protected helper function 
though and then child classes call it instead of the super 
ctor
then you could do a mixin template that forwards to that to kinda 
automate this ctor too.


this is fairly usable!


Re: ref parameter qualifier and static arrays

2015-09-10 Thread Paul via Digitalmars-d-learn

On Wednesday, 9 September 2015 at 20:35:53 UTC, anonymous wrote:

When you pass a slice (without ref), what's actually passed is 
a pointer and length. The contents are not copied. That means, 
when you alter an array element, the change will be done the 
original, even without ref:


Thanks both.

I see, temporary variable and no ref will do the job and I 
suppose it's better than just using a global...




Re: What is the difference between D and C++ regarding Unique, RefCounted and Scoped?

2015-09-10 Thread Daniel Kozák via Digitalmars-d-learn

On Thu, 10 Sep 2015 11:38:35 +
"Gary Willoughby"  wrote:

> On Wednesday, 9 September 2015 at 23:22:49 UTC, ponce wrote:
> > - RefCounted
> >
> > Only for D structs. std::shared_ptr works for all.
> 
> RefCounted works with classes as well.
> 
> http://dlang.org/phobos/std_typecons.html#.RefCounted

struct RefCounted(T, RefCountedAutoInitialize autoInit =
RefCountedAutoInitialize.yes) if (!is(T == class) && !is(T ==
interface));

if (!is(T == class) && !is(T ==interface)); // So it does not work
with classes


Re: Sum and other algorithm functions

2015-09-10 Thread anonymous via Digitalmars-d-learn
On Thursday 10 September 2015 15:48, Namal wrote:

> Hello,
> 
> how can I define the range for the sum function which I want to 
> sum up? For instance how do I sum up the first 3 elements of an 
> array
> 
> int[] a = [1,2,3,4,5,6,7,8,9];
> 
> or the last 3?

First you slice the first/last 3, then you sum them.

first 3: a[0 .. 3].sum
last 3: a[$ - 3 .. $].sum


Re: Sum and other algorithm functions

2015-09-10 Thread Andrea Fontana via Digitalmars-d-learn

On Thursday, 10 September 2015 at 13:48:16 UTC, Namal wrote:

Hello,

how can I define the range for the sum function which I want to 
sum up? For instance how do I sum up the first 3 elements of an 
array


int[] a = [1,2,3,4,5,6,7,8,9];

or the last 3?


In this case, you can simply slice array using   a[0..3] or 
a[$-3..$].


If you have a range you can use take() and/or drop()

void main()
{

int[] a = [1,2,3,4,5,6];

writeln(a[0..3]);
writeln(a[$-3..$]);
writeln(a.take(3));
writeln(a.drop(a.length-3));

}


Sum and other algorithm functions

2015-09-10 Thread Namal via Digitalmars-d-learn

Hello,

how can I define the range for the sum function which I want to 
sum up? For instance how do I sum up the first 3 elements of an 
array


int[] a = [1,2,3,4,5,6,7,8,9];

or the last 3?


Re: Version for windows/console compilation?

2015-09-10 Thread Adam D. Ruppe via Digitalmars-d-learn

On Thursday, 10 September 2015 at 18:06:43 UTC, Prudence wrote:
Is there a flag for knowing when a project is compiling for 
windows(Uses WinMain) vs a console(normal main)?


You'd have to choose the main yourself anyway, so document what 
process you use for that for people to use.


BTW it is pretty rare that you should actually write a WinMain in 
D. The right thing to do in most cases is to write a normal main 
function. You can still get the windows gui subsystem with a 
linker flag.


Re: Detecting premature end of spawned threads with std.concurrency

2015-09-10 Thread Ali Çehreli via Digitalmars-d-learn

On 09/03/2015 02:20 PM, Matt Kline wrote:
> neither the docs nor the current Phobos implementation
> make any mention of such exceptions.

There is LinkTerminated but you must use spawnLinked():

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

  http://ddili.org/ders/d.en/concurrency.html#ix_concurrency.LinkTerminated

Ali



Version for windows/console compilation?

2015-09-10 Thread Prudence via Digitalmars-d-learn
Is there a flag for knowing when a project is compiling for 
windows(Uses WinMain) vs a console(normal main)?


version(Windows) is always valid for a console app, so it is 
useless to disambiguate between a console app and a windows app. 
(Say I have both a main and a winmain in my code, I need to 
select between them(it's a bit more complex than this but)).





Re: private selective import not so private ?

2015-09-10 Thread Adam D. Ruppe via Digitalmars-d-learn

On Friday, 11 September 2015 at 00:52:00 UTC, BBasile wrote:
While trying to get why some call to memmove without the right 
import didn't lead to a compilation failure i've found that 
imported symbols are not private ! Is that a bug ? The specs 
don't say that a selective import is public !


Yes, it is one of the oldest, most infamous bugs D has, the 
dreaded #314:


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


Re: Version for windows/console compilation?

2015-09-10 Thread Mike Parker via Digitalmars-d-learn
On Thursday, 10 September 2015 at 18:10:43 UTC, Adam D. Ruppe 
wrote:


BTW it is pretty rare that you should actually write a WinMain 
in D. The right thing to do in most cases is to write a normal 
main function. You can still get the windows gui subsystem with 
a linker flag.


Specifically, add the following when using the Microsoft linker 
(compiling with -m64 or -m32mscoff):


-L/SUBSYSTEM:windows,6.00 -L/ENTRY:mainCRTStartup

And this when using OPTLINK:

-L/SUBSYSTEM:windows,5.01

The version numbers are optional. I use 6.00 with the MS linker 
because it covers both 32-bit and 64-bit apps on Vista and later, 
and is the default for the VS 2015 linker.


[1] https://msdn.microsoft.com/en-us/library/fcc1zstk.aspx

I don't think I've written a WinMain in D in 10 years.


Re: shared array?

2015-09-10 Thread Adam D. Ruppe via Digitalmars-d-learn

On Friday, 11 September 2015 at 00:48:28 UTC, Prudence wrote:

static Array!(bool delegate(int, WPARAM, LPARAM)) callbacks;


Try just using a regular array instead of the library Array.


static bool delegate(int, WPARAM, LPARAM)[] callbacks;

my guess is the Array library thing isn't marked as shared 
internally.


shared array?

2015-09-10 Thread Prudence via Digitalmars-d-learn

I can't create a shared array:

static Array!(bool delegate(int, WPARAM, LPARAM)) callbacks;

(prepending shared produce a ton of errors with the Array class)

I've tried making it a pointer and other things.


The array must be static and must be shared. Without shared 
everything works but I don't get, obviously, a useful array(it's 
empty because all the updating goes on in the main thread).


1. How do I create a shared array?
2. Why prepending shared produces any problems? I thought shared 
simply made a variable global to all threads?


Re: private selective import not so private ?

2015-09-10 Thread BBasile via Digitalmars-d-learn

On Friday, 11 September 2015 at 00:55:41 UTC, Adam D. Ruppe wrote:

On Friday, 11 September 2015 at 00:52:00 UTC, BBasile wrote:
While trying to get why some call to memmove without the right 
import didn't lead to a compilation failure i've found that 
imported symbols are not private ! Is that a bug ? The specs 
don't say that a selective import is public !


Yes, it is one of the oldest, most infamous bugs D has, the 
dreaded #314:


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


Damn, you break my joy...


private selective import not so private ?

2015-09-10 Thread BBasile via Digitalmars-d-learn
While trying to get why some call to memmove without the right 
import didn't lead to a compilation failure i've found that 
imported symbols are not private ! Is that a bug ? The specs 
don't say that a selective import is public !


-- other.d --
module other;
private import core.stdc.string: memmove;
-

-- main.d --
module main;
import other;
void main()
{
void* a,b;
memmove(a,b,0);
}


command `dmd main.d other.d: ok compiles without error.
win32, tested with latest beta, and 2 previous versions.


Re: Version for windows/console compilation?

2015-09-10 Thread Prudence via Digitalmars-d-learn

On Friday, 11 September 2015 at 01:36:31 UTC, Mike Parker wrote:
On Thursday, 10 September 2015 at 18:10:43 UTC, Adam D. Ruppe 
wrote:


BTW it is pretty rare that you should actually write a WinMain 
in D. The right thing to do in most cases is to write a normal 
main function. You can still get the windows gui subsystem 
with a linker flag.


Specifically, add the following when using the Microsoft linker 
(compiling with -m64 or -m32mscoff):


-L/SUBSYSTEM:windows,6.00 -L/ENTRY:mainCRTStartup

And this when using OPTLINK:

-L/SUBSYSTEM:windows,5.01

The version numbers are optional. I use 6.00 with the MS linker 
because it covers both 32-bit and 64-bit apps on Vista and 
later, and is the default for the VS 2015 linker.


[1] https://msdn.microsoft.com/en-us/library/fcc1zstk.aspx

I don't think I've written a WinMain in D in 10 years.


I'm using Visual D and I assume it takes care of all this. It 
works so that's not a huge problem.


I'm simply creating my own version flags in VD properties. Not 
the best way because I'll have to remember to set the flags every 
time I use the library or I'll get errors about stuff missing. I 
was hoping D had a flag to disambiguate console and windows 
apps(or some type to CT way to check).





Re: shared array?

2015-09-10 Thread Prudence via Digitalmars-d-learn

On Friday, 11 September 2015 at 00:50:15 UTC, Adam D. Ruppe wrote:

On Friday, 11 September 2015 at 00:48:28 UTC, Prudence wrote:

static Array!(bool delegate(int, WPARAM, LPARAM)) callbacks;


Try just using a regular array instead of the library Array.


static bool delegate(int, WPARAM, LPARAM)[] callbacks;

my guess is the Array library thing isn't marked as shared 
internally.


I thought about that but then I have to rely on the GC for some 
simple things. Doesn't seem like the right way to go.




Re: Can we get a video tutorial?

2015-09-10 Thread Stephen via Digitalmars-d-learn

On Friday, 4 September 2015 at 16:11:59 UTC, Stephen wrote:

On Friday, 4 September 2015 at 05:51:02 UTC, Mike Parker wrote:
* What was previously said *


* Bump * Hopefully I'm allowed to bump this...?


Calling D from C, C++, Python…

2015-09-10 Thread Russel Winder via Digitalmars-d-learn
Is there an easy way of knowing when you do not have to initialize the
D runtime system to call D code from, in this case, Python via a C
adapter?

I naïvely transformed some C++ to D, without consideration of D runtime
systems, compiled it and it all worked. Which is good, but…

-- 
Russel.
=
Dr Russel Winder  t: +44 20 7585 2200   voip: sip:russel.win...@ekiga.net
41 Buckmaster Roadm: +44 7770 465 077   xmpp: rus...@winder.org.uk
London SW11 1EN, UK   w: www.russel.org.uk  skype: russel_winder



signature.asc
Description: This is a digitally signed message part