Re: char[] ported from C to char[0] in the D core library

2015-09-10 Thread badlink via Digitalmars-d-learn
On Wednesday, 9 September 2015 at 19:37:54 UTC, Alex Parrill 
wrote:
It's a flexible array member [1], not a pointer. Changing it to 
`char*` would make it incompatible with the C functions using 
it.


[1]: https://en.wikipedia.org/wiki/Flexible_array_member


TIL a new detail about C on the D forum ;)



Re: Hello World Example with Glade?

2015-09-10 Thread Mike James via Digitalmars-d-learn

On Friday, 11 September 2015 at 06:45:07 UTC, Mike McKee wrote:

On Friday, 11 September 2015 at 06:00:39 UTC, Mike McKee wrote:

[...]


I think the start of this probably looks like the following, 
but I'm not certain:


import gtk;
import gobject.Type;
import std.stdio;
import std.c.process;

int main (string[] args)
{
Main.init(args);
Builder b = new Builder();
b.addFromFile("test1.glade");
Window w = cast(Window)b.getObject("window1");
w.showAll();
Main.run();
return 0;
}


Hi Mike,

There's a Glade example in the demos/builder directory...

Regards, --



...so, this assumed that I had a test1.glade file, and that I 
had this line inside it:




So now I need to figure out how to get GtkD installed on Ubuntu 
Linux 14.04.




Re: Hello World Example with Glade?

2015-09-10 Thread Mike McKee via Digitalmars-d-learn

On Friday, 11 September 2015 at 06:00:39 UTC, Mike McKee wrote:
On Ubuntu Linux, I can draw a simple Hello World interface (and 
a button on it to close the window) with Glade and save the 
file. Now how do I use GtkD and D to load that interface? 
Anyone got a super simple tutorial for that?


I think the start of this probably looks like the following, but 
I'm not certain:


import gtk;
import gobject.Type;
import std.stdio;
import std.c.process;

int main (string[] args)
{
Main.init(args);
Builder b = new Builder();
b.addFromFile("test1.glade");
Window w = cast(Window)b.getObject("window1");
w.showAll();
Main.run();
return 0;
}

...so, this assumed that I had a test1.glade file, and that I had 
this line inside it:




So now I need to figure out how to get GtkD installed on Ubuntu 
Linux 14.04.




Re: How To: Passing curried functions around

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

On 09/06/2015 12:05 PM, Bahman Movaqar wrote:
>  alias bool function(int n) validator_t;

There is the relatively newer alias syntax which is more intuitive:

alias Validator = bool function(int n);

>  bool isEven(int n) { ... }
>  bool isPrime(int n) { ... }
>  /**
>   * keeps asking for an int from the user until it passes
>   * the given validator.
>   */
>  int readInt(string prompt, validator_t validator) { ... }
>
>  // in part B of the application which knows nothing about part A
>  // -
>  /**
>   * does something that involves reading an integer from input
>   */
>  void foo(intReader_t reader) { ... }
>
> I'm trying to pass curried versions of `readInt` to `foo`.
> Obviously, I don't wish part B to know about the details of a "reader"
> nor I'd like to pass `prompt` and `validator` to `foo` (this really
> doesn't concern `foo` at all).
>
> I see that the solution is using `partial` from `std.functional`; for
> example:
>
>  partial!(partial!(readInt, "Enter an integer:"), &isEven)
>
> However, I'm not sure if this is correct

That does not compile because partial takes the function arguments as 
'value template parameters'. Unfortunately, a function pointer like 
&isEven cannot be 'value template parameters'; only fundamental types 
and strings can... So, 'partial' is not an option for this problem.


> (let alone idiomatic!)

Idiomatic D uses templates and passes behavior in the form of 'alias 
template parameters.' Alias template parameters are the convenient way 
of allowing anything that is callable, not just functions. For example, 
foo() would be much more useful if it allowed a delegate or a class 
object that has an overloaded opCall() operator.


> and even
> if this is the correct way of currying `readInt`, what should be the
> signature of `foo`?

I think 'int delegate()' would do because all foo needs is a function 
that returns an int.


The idiomatic way is to leave it as a template parameter. However, this 
has the problem of making each instantiation of the foo template a 
different type, making them incompatible to be elements of the same array:


[ &(foo!myReader), &(foo!yourReader) ]  // <-- compilation error

One solution is to do what std.parallelism.task does: to provide both a 
foo template and another foo function that takes an 'int delegate()':


  http://ddili.org/ders/d.en/parallelism.html

(Search for "The task function above has been specified as a template 
parameter" on that page.)


> I'd appreciate any help/hint on this.

Here is a solution:

import std.stdio;

bool isEven(int n) {
return !(n % 2);
}

int readValidInt(alias validator)(string prompt) {
while (true) {
int i;
write(prompt);
readf(" %s", &i);
if (validator(i)) {
return i;
} else {
writeln("Sorry, that's not acceptable");
}
}
}

void foo(alias reader)() {
reader();
}

void main() {
auto reader = () => readValidInt!isEven("Enter an integer: ");

foo!reader();
}

You can add template constraints to make the code easier to use.

Ali



Hello World Example with Glade?

2015-09-10 Thread Mike McKee via Digitalmars-d-learn
On Ubuntu Linux, I can draw a simple Hello World interface (and a 
button on it to close the window) with Glade and save the file. 
Now how do I use GtkD and D to load that interface? Anyone got a 
super simple tutorial for that?




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: 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: 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...


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: 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.


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.


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: 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...?


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.


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)).





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


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



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));

}


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


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: 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: 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: 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: 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: 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: 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: 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: 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: 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