Re: delegate with optional parameters

2017-04-02 Thread Inquie via Digitalmars-d-learn

On Monday, 3 April 2017 at 03:08:22 UTC, Ali Çehreli wrote:

On 04/02/2017 03:24 PM, Inquie wrote:

>> Show a usage, someone certainly propose a pattern that does
the job.
>
> int delegate() f;
> void delegate(int) f;

That won't work because both of those are variables and 
variables don't have overloading.


> These are effectively overload methods, but my guess is that
D won't
> support it like overloads.
> e.g.,
>
> int f();
> void f(int);

Yep, both 'f' are functions there.

I'm having difficulty understanding your actual need as well. 
:/ A guess: It is possible to determine delegate parameter list 
at compile time like std.concurrency.receive does.


Ali


Yes, but they are really not any different. They only look 
different. A field can be a function just like a method because 
they look exactly the same except on is in a vtable and the other 
is in the fields memory. But both point functions.


The only difference is that we can't write to the vtable to 
overwrite a value easily but we can to a delegate(no hackery).


So, it would be nice to be able to overload them. Effectively we 
can extend the vtable out in to the fields. (it would require a 
bit of work to make it work identical to a class, but it could, 
the outside world would know no difference).


If one wants: It essentially allows for methods to be modifiable 
at run time(something that classes can't do without unsafely 
hacking the vtable) and that is exactly why I have used it, but 
overloading causes a problem because only the name collides yet 
it works with the methods case but not the field delegates(a 
field delegate is essentially a method, is the point(for 
functional usage)).





Re: delegate with optional parameters

2017-04-02 Thread Ali Çehreli via Digitalmars-d-learn

On 04/02/2017 03:24 PM, Inquie wrote:

>> Show a usage, someone certainly propose a pattern that does the job.
>
> int delegate() f;
> void delegate(int) f;

That won't work because both of those are variables and variables don't 
have overloading.


> These are effectively overload methods, but my guess is that D won't
> support it like overloads.
> e.g.,
>
> int f();
> void f(int);

Yep, both 'f' are functions there.

I'm having difficulty understanding your actual need as well. :/ A 
guess: It is possible to determine delegate parameter list at compile 
time like std.concurrency.receive does.


Ali



Re: Covert a complex C header to D

2017-04-02 Thread Nicholas Wilson via Digitalmars-d-learn

On Sunday, 2 April 2017 at 21:43:52 UTC, biocyberman wrote:

template __KHASH_TYPE(string name){
  "struct  kh_" ~ name ~"_t { " ~
"khint_t n_buckets, size, n_occupied, 
upper_bound; " ~

"khint32_t *flags; " ~
"khkey_t *keys; " ~
"khval_t *vals; " ~
"}"

}


Not that you'll get bitten by it in this case but in D the 
pointer declarator * is left associative.


i.e. in C

 int *pInt, Int; // "Int" is int not an int*
 int *pInt, Int[3]; // Int is a static array of 3 ints.
but in D

misleading:
 int *pInt, Int; // Int is an int*!!

wrong:
 int *pInt, three_Ints[3]; // Error cannot mix declared types

not misleading
int* pInt, pInt2; // BOTH int*

int*pInt; //pointer to int
int[3] three_Ints; // static array of 3 ints.


Re: Covert a complex C header to D

2017-04-02 Thread Nicholas Wilson via Digitalmars-d-learn

On Sunday, 2 April 2017 at 21:43:52 UTC, biocyberman wrote:
khash.h 
(http://attractivechaos.github.io/klib/#Khash%3A%20generic%20hash%20table) is a part of klib library in C. I want to covert it to D in the process of learning deeper about D.


First I tried with Dstep 
(https://github.com/jacob-carlborg/dstep) and read the C to D 
article (https://dlang.org/ctod.html). I managed to covert the 
basic statements to D, but all multiline 'define' macros are 
stripped off. So I am trying to recreate them with D way. For 
example:



#define __KHASH_TYPE(name, khkey_t, khval_t) \
typedef struct kh_##name##_s { \
khint_t n_buckets, size, n_occupied, upper_bound; \
khint32_t *flags; \
khkey_t *keys; \
khval_t *vals; \
} kh_##name##_t;


I changed to:

template __KHASH_TYPE(string name){
  "struct  kh_" ~ name ~"_t { " ~
"khint_t n_buckets, size, n_occupied, 
upper_bound; " ~

"khint32_t *flags; " ~
"khkey_t *keys; " ~
"khval_t *vals; " ~
"}"

}

// NEXT: use mixin with this template.

I am currently get a bit intimidated looking at KHASH_INIT2 
macro in khash.c. How do I convert this to the equivalent and 
idiomatic D?


You are on the right track, converting #define's that declare 
symbols to template strings to be mixed in. But you also need to 
parameterise the key type and the value type as they are also 
arguments to the macro.


so you'd go

mixin( __KHASH_TYPE("mytype",string, int));

However it is generally considered better to use templates where 
possible as they are generally astir to reason about (and look 
nicer). Since this is a relatively simple case we could just go:


struct kh_hashtable_t(string name,K,V) {
//kh_hashtable_t is a struct parameterised on the types K and 
V

khint_t n_buckets, size, n_occupied, upper_bound;
khint32_t *flags;
 K *keys;
 V *vals;
}
and not worry about "name", the compiler will generate an 
internal name for us. Doesn't matter what it is, but it is 
guaranteed to be unique which is the main property we want. We 
probably don't even need the nam parameter at all.


(there is also the builtin hash table declared V[K] e.g. 
int[string] i.e. a hash table of ints indexed by strings.).


So for KHASH_INIT2:

the argument to the macro are
name: a string
scope: a protection modifier (in C they use static inline, in 
D this would be pragma(inline, true) private. But I would ignore 
this parameter.

khkey_t: the key type
khval_t: the value type
kh_is_map: a bool (not sure of its purpose).
__hash_func: the function used to generate a hash from the key
   __hash_equal:

so you'd want something like

template KHASH_INIT(string name,K,V,bool kh_is_map, alias 
keyhash, alias equal =  (V a , V b) => a==b)

{
//...
}

where K and V are types, "alias keyhash" is a function that 
transforms a key into a hash and alias equal is a function that 
deternimes if two values(keys?) are equal.


you'd call it like
KHASH_INIT!("some_name",string,int,true, (string a) => 
myFancyHash(a) /* leave equal as a default*/);


Let me know if you get stuck.
Nic


Re: delegate with optional parameters

2017-04-02 Thread Inquie via Digitalmars-d-learn

On Sunday, 2 April 2017 at 21:47:55 UTC, Basile B. wrote:

On Sunday, 2 April 2017 at 20:48:09 UTC, Inquie wrote:

On Sunday, 2 April 2017 at 20:02:56 UTC, Basile B. wrote:

On Sunday, 2 April 2017 at 19:24:14 UTC, Inquie wrote:
is it possible to create a delegate that takes an optional 
number of parameters and/or return type?


T delegate(S...)(S) special_delegate;

I guess this is impossible?


alias Dg(Return, Params...) = Return delegate(Params);

Dg!(int,float, string) myDg;


What I mean is that I want to be able to overload delegates 
like one can do with normal members.


Show a usage, someone certainly propose a pattern that does the 
job.


int delegate() f;
void delegate(int) f;

These are effectively overload methods, but my guess is that D 
won't support it like overloads.

e.g.,

int f();
void f(int);



Re: Write file at compile time?

2017-04-02 Thread Basile B. via Digitalmars-d-learn

On Sunday, 2 April 2017 at 19:42:52 UTC, Inquie wrote:
I would like to write the output of a manifest constant at 
compile time to a file instead of console using pragma(msg). Is 
this possible?


No.


Re: delegate with optional parameters

2017-04-02 Thread Basile B. via Digitalmars-d-learn

On Sunday, 2 April 2017 at 20:48:09 UTC, Inquie wrote:

On Sunday, 2 April 2017 at 20:02:56 UTC, Basile B. wrote:

On Sunday, 2 April 2017 at 19:24:14 UTC, Inquie wrote:
is it possible to create a delegate that takes an optional 
number of parameters and/or return type?


T delegate(S...)(S) special_delegate;

I guess this is impossible?


alias Dg(Return, Params...) = Return delegate(Params);

Dg!(int,float, string) myDg;


What I mean is that I want to be able to overload delegates 
like one can do with normal members.


Show a usage, someone certainly propose a pattern that does the 
job.


Covert a complex C header to D

2017-04-02 Thread biocyberman via Digitalmars-d-learn
khash.h 
(http://attractivechaos.github.io/klib/#Khash%3A%20generic%20hash%20table) is a part of klib library in C. I want to covert it to D in the process of learning deeper about D.


First I tried with Dstep 
(https://github.com/jacob-carlborg/dstep) and read the C to D 
article (https://dlang.org/ctod.html). I managed to covert the 
basic statements to D, but all multiline 'define' macros are 
stripped off. So I am trying to recreate them with D way. For 
example:



#define __KHASH_TYPE(name, khkey_t, khval_t) \
typedef struct kh_##name##_s { \
khint_t n_buckets, size, n_occupied, upper_bound; \
khint32_t *flags; \
khkey_t *keys; \
khval_t *vals; \
} kh_##name##_t;


I changed to:

template __KHASH_TYPE(string name){
  "struct  kh_" ~ name ~"_t { " ~
"khint_t n_buckets, size, n_occupied, 
upper_bound; " ~

"khint32_t *flags; " ~
"khkey_t *keys; " ~
"khval_t *vals; " ~
"}"

}

// NEXT: use mixin with this template.

I am currently get a bit intimidated looking at KHASH_INIT2 macro 
in khash.c. How do I convert this to the equivalent and idiomatic 
D?






Re: delegate with optional parameters

2017-04-02 Thread Inquie via Digitalmars-d-learn

On Sunday, 2 April 2017 at 20:02:56 UTC, Basile B. wrote:

On Sunday, 2 April 2017 at 19:24:14 UTC, Inquie wrote:
is it possible to create a delegate that takes an optional 
number of parameters and/or return type?


T delegate(S...)(S) special_delegate;

I guess this is impossible?


alias Dg(Return, Params...) = Return delegate(Params);

Dg!(int,float, string) myDg;


What I mean is that I want to be able to overload delegates like 
one can do with normal members.


Re: delegate with optional parameters

2017-04-02 Thread Basile B. via Digitalmars-d-learn

On Sunday, 2 April 2017 at 19:24:14 UTC, Inquie wrote:
is it possible to create a delegate that takes an optional 
number of parameters and/or return type?


T delegate(S...)(S) special_delegate;

I guess this is impossible?


alias Dg(Return, Params...) = Return delegate(Params);

Dg!(int,float, string) myDg;


Write file at compile time?

2017-04-02 Thread Inquie via Digitalmars-d-learn
I would like to write the output of a manifest constant at 
compile time to a file instead of console using pragma(msg). Is 
this possible?





delegate with optional parameters

2017-04-02 Thread Inquie via Digitalmars-d-learn
is it possible to create a delegate that takes an optional number 
of parameters and/or return type?


T delegate(S...)(S) special_delegate;

I guess this is impossible?


Re: pointer not aligned

2017-04-02 Thread Jon Degenhardt via Digitalmars-d-learn

On Friday, 31 March 2017 at 04:41:10 UTC, Joel wrote:

Linking...
ld: warning: pointer not aligned at address 0x10017A4C9 
(_D30TypeInfo_AxS3std4file8DirEntry6__initZ + 16 from 
.dub/build/application-debug-posix.osx-x86_64-dmd_2072-EFDCDF4D45F944F7A9B1AEA5C32F81ED/spellit.o)

...

and this goes on forever!


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


Re: pointer not aligned

2017-04-02 Thread Timothee Cour via Digitalmars-d-learn
indeed. NOTE: ldmd2/ldc2 doens't have this issue

to reproduce:

```
rdmd --force --eval='writeln(`hello`)'
```

ld: warning: pointer not aligned at address 0x1000BE0B9
(_D53TypeInfo_S3std5array17__T8AppenderTAyaZ8Appender4Data6__initZ +
24 from .rdmd-501/rdmd-eval.o)

with `--compiler=ldmd2`  there's no error

On Fri, Mar 31, 2017 at 2:06 AM, Adam Wilson via Digitalmars-d-learn
 wrote:
> On 3/30/17 10:47 PM, H. S. Teoh via Digitalmars-d-learn wrote:
>>
>> On Fri, Mar 31, 2017 at 04:41:10AM +, Joel via Digitalmars-d-learn
>> wrote:
>>>
>>> Linking...
>>> ld: warning: pointer not aligned at address 0x10017A4C9
>>> (_D30TypeInfo_AxS3std4file8DirEntry6__initZ + 16 from
>>> .dub/build/application-debug-posix.osx-x86_64-dmd_2072-EFDCDF4D45F944F7A9B1AEA5C32F81ED/spellit.o)
>>> ...
>>>
>>> and this goes on forever!
>>
>>
>> More information, please.  What was the code you were trying to compile?
>> What compile flags did you use? Which compiler?
>>
>>
>> T
>>
>
> I see this on OSX as well. Any code referencing Phobos appears to produce
> this. It appear after updating the XCode command line tools. It does not
> appear to effect program execution, but the pages of warnings are really
> quite annoying.
>
> DMD 2.073.2
>
> --
> Adam Wilson
> IRC: LightBender
> import quiet.dlang.dev;


Re: Interfacing C++ to D

2017-04-02 Thread FreeSlave via Digitalmars-d-learn

On Sunday, 2 April 2017 at 16:03:51 UTC, FreeSlave wrote:

On Sunday, 2 April 2017 at 16:02:06 UTC, FreeSlave wrote:

On Sunday, 2 April 2017 at 09:58:19 UTC, ANtlord wrote:

[...]


Now I see. 'Using C++ Classes From D' crashes for me too. It 
also returns the wrong value from 'field' method. Should be 5, 
but it returns 0. Probably regression.


Funny thing: If I replace interface with abstract class, it 
works as it should.


Reported issue: https://issues.dlang.org/show_bug.cgi?id=17293


Re: Interfacing C++ to D

2017-04-02 Thread FreeSlave via Digitalmars-d-learn

On Sunday, 2 April 2017 at 16:02:06 UTC, FreeSlave wrote:

On Sunday, 2 April 2017 at 09:58:19 UTC, ANtlord wrote:

On Saturday, 1 April 2017 at 16:39:28 UTC, FreeSlave wrote:
This page has many examples. Which exactly do you try to run 
and how do you build it? Which compilers, OS?


My bad. I've tested example under caption Using C++ Classes 
From D. I used several combinations of compilers, and no one 
works for me.

OS: ArchLinux
D compilers: DMD 2.073.2, LDC 1.1.0
C++ compiler: gcc 6.3.1, clang 3.9

Also I've created reposotory contains test project. Anyone can 
take a look on that for clearance. 
https://github.com/ANtlord/cpp_to_d_test


Now I see. 'Using C++ Classes From D' crashes for me too. It 
also returns the wrong value from 'field' method. Should be 5, 
but it returns 0. Probably regression.


Funny thing: If I replace interface with abstract class, it works 
as it should.


Re: Interfacing C++ to D

2017-04-02 Thread FreeSlave via Digitalmars-d-learn

On Sunday, 2 April 2017 at 09:58:19 UTC, ANtlord wrote:

On Saturday, 1 April 2017 at 16:39:28 UTC, FreeSlave wrote:
This page has many examples. Which exactly do you try to run 
and how do you build it? Which compilers, OS?


My bad. I've tested example under caption Using C++ Classes 
From D. I used several combinations of compilers, and no one 
works for me.

OS: ArchLinux
D compilers: DMD 2.073.2, LDC 1.1.0
C++ compiler: gcc 6.3.1, clang 3.9

Also I've created reposotory contains test project. Anyone can 
take a look on that for clearance. 
https://github.com/ANtlord/cpp_to_d_test


Now I see. 'Using C++ Classes From D' crashes for me too. It also 
returns the wrong value from 'field' method. Should be 5, but it 
returns 0. Probably regression.


Re: Experimental xml set up

2017-04-02 Thread Begah via Digitalmars-d-learn

On Sunday, 2 April 2017 at 14:15:50 UTC, rikki cattermole wrote:

On 02/04/2017 2:58 PM, Begah wrote:

[...]


Quite out of date.

Here is some code that worked for me:

string raw_input = ...;

auto domBuilder = raw_input
.lexer
.parser
.cursor((CursorError err){})
.domBuilder;
domBuilder.setSource(raw_input);
domBuilder.buildRecursive;
auto dom = domBuilder.getDocument;


Dub/Dmd still complains that there is no property/function 
'lexer' for type string.




Re: Need advice on using DUB registry

2017-04-02 Thread drug via Digitalmars-d-learn

02.04.2017 17:43, Eric пишет:

On Sunday, 2 April 2017 at 04:14:56 UTC, rikki cattermole wrote:

On 02/04/2017 2:37 AM, Eric wrote:


I'm planning on some day putting a package in the DUB registry.  My
package
is dependent on my "util" package which is a collection of stuff I use
across
all my projects.  Does this mean I also have to put my util package in
the DUB registry?
Could I just make "util" a git sub module of the package I want to
release?


Dub can't use git submodules.
In fact it doesn't know anything about git!


So is the answer to my question, "yes, you need to put your util package
in the DUB registry also?"
You can clone git submodules using preBuildCommands in dub.sdl. Also you 
can use add-local and add this package to dub locally (but of course it 
won't work for others). And the finally put your package to dub 
registry. I do all of them depending on the situation.


Re: Need advice on using DUB registry

2017-04-02 Thread Eric via Digitalmars-d-learn

On Sunday, 2 April 2017 at 04:14:56 UTC, rikki cattermole wrote:

On 02/04/2017 2:37 AM, Eric wrote:


I'm planning on some day putting a package in the DUB 
registry.  My package
is dependent on my "util" package which is a collection of 
stuff I use

across
all my projects.  Does this mean I also have to put my util 
package in

the DUB registry?
Could I just make "util" a git sub module of the package I 
want to release?


Dub can't use git submodules.
In fact it doesn't know anything about git!


So is the answer to my question, "yes, you need to put your util 
package in the DUB registry also?"


Re: Experimental xml set up

2017-04-02 Thread rikki cattermole via Digitalmars-d-learn

On 02/04/2017 2:58 PM, Begah wrote:

To load up 3D models in my application, i decided to use the COLLADA
model format, to do that i need to be able to extract information out of
an xml file.

Since std.xml is going to be deprecated eventually, is opted to try and
use std.experiment.xml.

So i created a test project and added this dependency to dub.json :
"std-experimental-xml": "~>0.1.2"

I then tried and compiled the exemple i found on the xml wiki at
https://lodo1995.github.io/experimental.xml/std/experimental/xml.html

Code :
// Made sure I imported Everything
import std.experimental.xml.appender;
import std.experimental.xml.cursor;
import std.experimental.xml.dom;
import std.experimental.xml.domimpl;
import std.experimental.xml.domparser;
import std.experimental.xml.dtd;
import std.experimental.xml.faststrings;
import std.experimental.xml.interfaces;
import std.experimental.xml.lexers;
import std.experimental.xml.parser;
import std.experimental.xml.sax;
import std.experimental.xml.validation;
import std.experimental.xml.writer;
import std.experimental.xml.legacy;
import std.stdio;

void main() {
string input = q"{


   
   The D Programming Language
   A. Alexandrescu
   
   
   Programming in D
   Ali Çehreli
   
   
   Modern C++ Design
   A. Alexandrescu
   

}";

// the following steps are all configurable
auto domBuilder =
input
   .lexer  // instantiate the best lexer based on the
type of input
   .parser // instantiate a parser on top of the lexer
   .cursor // instantiate a cursor on top of the parser
   .domBuilder;// and finally the DOM builder on top of the
cursor

// the source is forwarded down the parsing chain and everything is
initialized
domBuilder.setSource(input);

// recursively build the entire DOM tree
domBuilder.buildRecursive;
auto dom = domBuilder.getDocument;

// find and substitute all matching authors
foreach (author; dom.getElementsByTagName("author"))
   if (author.textContent == "A. Alexandrescu")
   author.textContent = "Andrei Alexandrescu";

// write it out to "catalogue.xml"
auto file = File("catalogue.xml", "w");
file.lockingTextWriter
.writerFor!string   // instatiates an xml writer on top of an
output range
.writeDOM(dom); // write the document with all of its children

readln();
}

When trying to compile, i get the following error :
Error: no property 'lexer' for type 'string'

There are also a lot of variable types not recognized by the compiler.

Is the wiki example completely outdated or am i missing something?


Quite out of date.

Here is some code that worked for me:

string raw_input = ...;

auto domBuilder = raw_input
.lexer
.parser
.cursor((CursorError err){})
.domBuilder;
domBuilder.setSource(raw_input);
domBuilder.buildRecursive;
auto dom = domBuilder.getDocument;


Experimental xml set up

2017-04-02 Thread Begah via Digitalmars-d-learn
To load up 3D models in my application, i decided to use the 
COLLADA model format, to do that i need to be able to extract 
information out of an xml file.


Since std.xml is going to be deprecated eventually, is opted to 
try and use std.experiment.xml.


So i created a test project and added this dependency to dub.json 
:

"std-experimental-xml": "~>0.1.2"

I then tried and compiled the exemple i found on the xml wiki at 
https://lodo1995.github.io/experimental.xml/std/experimental/xml.html


Code :
// Made sure I imported Everything
import std.experimental.xml.appender;
import std.experimental.xml.cursor;
import std.experimental.xml.dom;
import std.experimental.xml.domimpl;
import std.experimental.xml.domparser;
import std.experimental.xml.dtd;
import std.experimental.xml.faststrings;
import std.experimental.xml.interfaces;
import std.experimental.xml.lexers;
import std.experimental.xml.parser;
import std.experimental.xml.sax;
import std.experimental.xml.validation;
import std.experimental.xml.writer;
import std.experimental.xml.legacy;
import std.stdio;

void main() {
string input = q"{


   
   The D Programming Language
   A. Alexandrescu
   
   
   Programming in D
   Ali Çehreli
   
   
   Modern C++ Design
   A. Alexandrescu
   

}";

// the following steps are all configurable
auto domBuilder =
input
	   .lexer  // instantiate the best lexer based on 
the type of input
	   .parser // instantiate a parser on top of the 
lexer
	   .cursor // instantiate a cursor on top of the 
parser
	   .domBuilder;// and finally the DOM builder on top of 
the cursor


	// the source is forwarded down the parsing chain and everything 
is initialized

domBuilder.setSource(input);

// recursively build the entire DOM tree
domBuilder.buildRecursive;
auto dom = domBuilder.getDocument;

// find and substitute all matching authors
foreach (author; dom.getElementsByTagName("author"))
   if (author.textContent == "A. Alexandrescu")
   author.textContent = "Andrei Alexandrescu";

// write it out to "catalogue.xml"
auto file = File("catalogue.xml", "w");
file.lockingTextWriter
		.writerFor!string   // instatiates an xml writer on top of an 
output range
		.writeDOM(dom); // write the document with all of its 
children


readln();
}

When trying to compile, i get the following error :
Error: no property 'lexer' for type 'string'

There are also a lot of variable types not recognized by the 
compiler.


Is the wiki example completely outdated or am i missing something?


Re: Interfacing C++ to D

2017-04-02 Thread ANtlord via Digitalmars-d-learn

On Sunday, 2 April 2017 at 09:58:19 UTC, ANtlord wrote:

On Saturday, 1 April 2017 at 16:39:28 UTC, FreeSlave wrote:
This page has many examples. Which exactly do you try to run 
and how do you build it? Which compilers, OS?


My bad. I've tested example under caption Using C++ Classes 
From D. I used several combinations of compilers, and no one 
works for me.

OS: ArchLinux
D compilers: DMD 2.073.2, LDC 1.1.0
C++ compiler: gcc 6.3.1, clang 3.9

Also I've created reposotory contains test project. Anyone can 
take a look on that for clearance. 
https://github.com/ANtlord/cpp_to_d_test


I've tested also on Ubuntu 16.04 and DMD v2.073.1 with gcc 5.4.0


Re: Interfacing C++ to D

2017-04-02 Thread ANtlord via Digitalmars-d-learn

On Saturday, 1 April 2017 at 16:39:28 UTC, FreeSlave wrote:
This page has many examples. Which exactly do you try to run 
and how do you build it? Which compilers, OS?


My bad. I've tested example under caption Using C++ Classes From 
D. I used several combinations of compilers, and no one works for 
me.

OS: ArchLinux
D compilers: DMD 2.073.2, LDC 1.1.0
C++ compiler: gcc 6.3.1, clang 3.9

Also I've created reposotory contains test project. Anyone can 
take a look on that for clearance. 
https://github.com/ANtlord/cpp_to_d_test