Re: Call C function - Access violation

2016-01-03 Thread TheDGuy via Digitalmars-d-learn

On Sunday, 3 January 2016 at 21:20:35 UTC, anonymous wrote:

On 03.01.2016 21:32, TheDGuy wrote:

If i type:
gcc -c -otest.c.o

the 'test.c.o' file is generated but if i type:

dmd main.d test.c.o i get: 'Error: unrecognized file extension 
o'?


You're probably on Windows then? dmd doesn't recognize the .o 
extension on Windows, use .obj instead.


If i rename "test.o" to "test.obj" i get:

'Error: Module or Dictionary corrupt'



Re: Deit Editor

2016-01-03 Thread Jason Jeffory via Digitalmars-d-learn
On Sunday, 3 January 2016 at 18:53:10 UTC, Martin Tschierschke 
wrote:

On Friday, 1 January 2016 at 00:14:08 UTC, Jason Jeffory wrote:
Vibe.d uses deit files for html generation. Seems nice but 
haven't dived into it(just removes a but of nonsense and 
allows code integration for dynamic generation... sounds 
great!!).


But what about visual editing? It's nice to be ale to see the 
layout of the page directly without all the hassle of toggling 
back and forth between a browser and code editor. Also, there 
is the issue of syntax highlighting, code validation, etc...


I came across this

http://stackoverflow.com/questions/16080350/using-jade-with-wysiwyg-markdown-to-allow-users-to-edit-content

Any ideas to make life easier? (isn't that what it's all 
about?)


There was an answer pointing to 
https://www.npmjs.org/package/html2jade
and this to http://html2jade.aaron-powell.com/, which  allows 
you to convert online HTML to JADE.
So you can make your HTML layout on an traditional way and get 
the jade code.


Doesn't help much because it is not maintainable?

But one could modify the tool to add additional functionality 
with a custom tag?


Use any standard html editor that won't crap out on a custom tag 
and then insert D code into it?


e.g.






Hello


Re: Call C function - Access violation

2016-01-03 Thread TheDGuy via Digitalmars-d-learn


Use an import.

import std.string;
import std.conv;

void main(string[] args) {
auto value  = toStringz("Hello World");
auto result = write(value);
auto s  = to!(string)(result);
writeln(s);
}

Also all string literals in D are zero terminated so you could 
write the call like this:


auto result = write("Hello World");


Okay, thank you very much!

But now i get the access violation error again.

If i type:
gcc -c -otest.c.o

the 'test.c.o' file is generated but if i type:

dmd main.d test.c.o i get: 'Error: unrecognized file extension o'?


Re: Call C function - Access violation

2016-01-03 Thread Gary Willoughby via Digitalmars-d-learn

On Sunday, 3 January 2016 at 19:24:46 UTC, TheDGuy wrote:
On Sunday, 3 January 2016 at 13:25:04 UTC, Gary Willoughby 
wrote:
On Sunday, 3 January 2016 at 13:23:25 UTC, Gary Willoughby 
wrote:
I think I've noticed one problem with the code above. You are 
using `text.ptr`. You shouldn't do that because you are 
passing a pointer not an array. Just use `text`.


Forget this line, my mistake. Use `toStringz` and pass a 
pointer instead of an array.


Hi and thanks for your answer. My code looks now like this:

void main(string[] args){
const(char)* val = "Hello World".std.string.toStringz;
char* result = write(val);
const(char)[] s = cstr2dstr(result);
writeln(s);
readln(); //keep the window open
}

But now i get the error: "function expected before(), not 
package std of type void" (refering to line 2).


And if i define the variable 'value' as 'const(char)*' i also 
have to rewrite my C-function to accept const(char)*...


Use an import.

import std.string;
import std.conv;

void main(string[] args) {
auto value  = toStringz("Hello World");
auto result = write(value);
auto s  = to!(string)(result);
writeln(s);
}

Also all string literals in D are zero terminated so you could 
write the call like this:


auto result = write("Hello World");




Re: Call C function - Access violation

2016-01-03 Thread anonymous via Digitalmars-d-learn

On 03.01.2016 21:32, TheDGuy wrote:

If i type:
gcc -c -otest.c.o

the 'test.c.o' file is generated but if i type:

dmd main.d test.c.o i get: 'Error: unrecognized file extension o'?


You're probably on Windows then? dmd doesn't recognize the .o extension 
on Windows, use .obj instead.


Re: Call C function - Access violation

2016-01-03 Thread anonymous via Digitalmars-d-learn

On 03.01.2016 22:37, TheDGuy wrote:

If i rename "test.o" to "test.obj" i get:

'Error: Module or Dictionary corrupt'


My guess is, that means that dmd can't handle the object file format 
that gcc produces.


Disclaimer: I don't know much about object formats, gcc, and Windows. I 
may be mistaken in what I write here.


I think there are three object file formats that are relevant for us:

* OMF: the format dmd uses by default on 32 bit Windows,
* COFF: the format dmd uses on 64 bit Windows and in 32 bit mode with 
the -m32mscoff switch,
* ELF: the format gcc uses by default (on Linux at least, maybe on 
Windows too).


You should probably try to find out what gcc produces (by reading gcc 
documentation, I supppose).


Then you need to get your D compiler and your C compiler to produce the 
same format.


As mentioned, dmd has some switches that affect the used/expected object 
format: -m32 (OMF) -m64 (COFF) -m32mscoff (COFF).


You can also try to get gcc to produce another format. I don't know how 
to do that.


Or you can use another C compiler that produces a different format than gcc.

I'd expect Digital Mars's (i.e. Walter's) dmc to be compatible with dmd 
and produce OMF files. Looks like there's a free download:

http://www.digitalmars.com/download/freecompiler.html

dmd's COFF mode is supposed to be compatible with Microsoft's Visual 
Studio. So that would be another option.


question about the implementation of Variant

2016-01-03 Thread aki via Digitalmars-d-learn
Following function will return the reference to a object Foo 
embedded in a Variant.


class Foo {}
Variant fun() {
Variant v;
v = new Foo();
return v;
}

According to the source code of VariantN.opAssign,
the assignment is done by:

memcpy(, , rhs.sizeof);
fptr = !(T);

where rhs is new Foo(), store is embedded storage in Variant,
and fptr is accessor function also work as data tag.
Very efficient implementation.
But wait, how does GC detect there still be a live reference to 
the object Foo?

Because store is just a fix sized array of bytes.
ubyte[size] store;
GC cannot be aware of the reference, right?

Thanks, aki



Re: Help convert a C++ header to D

2016-01-03 Thread Rikki Cattermole via Digitalmars-d-learn

On 03/01/16 9:26 PM, Arialis Majoris wrote:

On Sunday, 3 January 2016 at 06:07:09 UTC, Rikki Cattermole wrote:

On 03/01/16 7:04 PM, Arialis Majoris wrote:

I have a rather large header file used to write plugins for reaper:

http://pastebin.com/Eebv1e0M

This file, unfortunately may change quite often depending on the version
of reaper. Reaper automatically generates the C++ file. Is there a way
to automatically convert this without too much trouble?

Thanks


It's really quite simple.
You would probably only need like 2 regex's to do it.


If that's the case, shouldn't there be an automated utility? Seems like
it would be more complex? I thought there was a D tool that converted C
headers into D?


htod is pretty old at this point and is Windows only.

Most headers don't port well. This one will.
Its just so simple and constant the pattern.



Re: Help convert a C++ header to D

2016-01-03 Thread Arialis Majoris via Digitalmars-d-learn

On Sunday, 3 January 2016 at 06:07:09 UTC, Rikki Cattermole wrote:

On 03/01/16 7:04 PM, Arialis Majoris wrote:
I have a rather large header file used to write plugins for 
reaper:


http://pastebin.com/Eebv1e0M

This file, unfortunately may change quite often depending on 
the version
of reaper. Reaper automatically generates the C++ file. Is 
there a way

to automatically convert this without too much trouble?

Thanks


It's really quite simple.
You would probably only need like 2 regex's to do it.


If that's the case, shouldn't there be an automated utility? 
Seems like it would be more complex? I thought there was a D tool 
that converted C headers into D?




Re: Password Storage

2016-01-03 Thread sarn via Digitalmars-d-learn

On Friday, 27 November 2015 at 00:17:34 UTC, brian wrote:
3) pre- or post-pend the salt to the password entered 
(apparently there is a difference??)
Sorry to revive an old thread, but I wrote a blog post about this 
question:

https://theartofmachinery.com/2016/01/03/What%20Difference%20Can%20Order%20Make%20When%20Hashing.html


RedBlackTree and myClass

2016-01-03 Thread AntonSotov via Digitalmars-d-learn

import std.container.rbtree;

class myClass {
string str;
}


int main()
{
auto tree = new RedBlackTree!myClass;
return 0;
}


Error: mutable method object.Object.opCmp is not callable using a 
inout object
Error: template instance std.functional.binaryFun!("a < b", "a", 
"b").binaryFun!(inout(myClass), myClass) error instantiating

/

How to use RedBlackTree of the objects of of its class?



Re: Help convert a C++ header to D

2016-01-03 Thread Arialis Majoris via Digitalmars-d-learn

On Sunday, 3 January 2016 at 08:29:22 UTC, Rikki Cattermole wrote:

On 03/01/16 9:26 PM, Arialis Majoris wrote:
On Sunday, 3 January 2016 at 06:07:09 UTC, Rikki Cattermole 
wrote:

On 03/01/16 7:04 PM, Arialis Majoris wrote:

[...]


It's really quite simple.
You would probably only need like 2 regex's to do it.


If that's the case, shouldn't there be an automated utility? 
Seems like
it would be more complex? I thought there was a D tool that 
converted C

headers into D?


htod is pretty old at this point and is Windows only.

Most headers don't port well. This one will.
Its just so simple and constant the pattern.


Could you explain instead of telling me that? ;) It doesn't help 
me if I don't know it too!


What won't convert well that I need to worry about?


Re: RedBlackTree and myClass

2016-01-03 Thread Tobi G. via Digitalmars-d-learn

On Sunday, 3 January 2016 at 14:49:59 UTC, tsbockman wrote:

On Sunday, 3 January 2016 at 10:55:05 UTC, AntonSotov wrote:

import std.container.rbtree;

class myClass {
string str;
}


int main()
{
auto tree = new RedBlackTree!myClass;
return 0;
}


Error: mutable method object.Object.opCmp is not callable 
using a inout object
Error: template instance std.functional.binaryFun!("a < b", 
"a", "b").binaryFun!(inout(myClass), myClass) error 
instantiating

/

How to use RedBlackTree of the objects of of its class?


Anyway, it's not too hard if you understand what's going on, 
and all of the functions I added are good things to have 
anyway, because lots of generic code expects some or all of 
them. But, the error messages aren't all that helpful if you 
didn't already know most of that.


But as a beginner it is quite disappointing to write all of these 
functions to just get it work.
Maybe i am a bad programmer, but i don't write and use the member 
functions above that often.
I often use the 'less' in the template arguments to get such 
things as comparison done, and implement these functions only if 
i have to..

To get it work this should be enough:

import std.container.rbtree;

class myClass {
string str;

override string toString() const {
return "{myClass: " ~ str ~ "}"; }
}

void main()
{
auto tree = new RedBlackTree!(myClass, "a.str < b.str");
}

~ togrue




Re: RedBlackTree and myClass

2016-01-03 Thread AntonSotov via Digitalmars-d-learn

tsbockman,

Many thanks! Now I work for me



Re: Call C function - Access violation

2016-01-03 Thread anonymous via Digitalmars-d-learn

On 03.01.2016 14:12, anonymous wrote:

You shouldn't get a segfault, though. You should get some compile/link
error. Are you compiling the right files? Can the segfault be from
something other than your program?


Oh, I see what's probably happening:

You compile the D program, but you don't compile and/or don't link the C 
object file. It segfaults then when trying to call the C function.


You need to compile the C file and pass it to dmd or the linker. For 
example:


gcc -c -otest.c.o test.c
dmd test.d test.c.o
./test


Re: RedBlackTree and myClass

2016-01-03 Thread Jonathan M Davis via Digitalmars-d-learn
On Sunday, January 03, 2016 10:55:05 AntonSotov via Digitalmars-d-learn wrote:
> import std.container.rbtree;
>
> class myClass {
>  string str;
> }
>
>
> int main()
> {
>  auto tree = new RedBlackTree!myClass;
>  return 0;
> }
>
>
> Error: mutable method object.Object.opCmp is not callable using a
> inout object
> Error: template instance std.functional.binaryFun!("a < b", "a",
> "b").binaryFun!(inout(myClass), myClass) error instantiating
> /
>
> How to use RedBlackTree of the objects of of its class?

You either have to define opCmp on your class so that it can be compared
with the < operator, or you need to provide RedBlackTree's constructor a
function to use to compare instead of using <.

- Jonathan M Davis



Call C function - Access violation

2016-01-03 Thread TheDGuy via Digitalmars-d-learn

I get an access violation with this code:

extern(C) char* write(char* text);

void main(string[] args){
char[] text = "Hello World".dup; //.dup converts string to char[]
text ~= '\0'; //append

char* result = write(text.ptr); //you need .ptr
const(char)[] s = cstr2dstr(result);
writeln(s);
readln(); //keep the window open
}

auto cstr2dstr(inout(char)* cstr)
{
return cstr ? cstr[0 .. strlen(cstr)] : "";
}

--

#include std.stdio;

char* write(char* text){
return text;
}



Re: Call C function - Access violation

2016-01-03 Thread TheDGuy via Digitalmars-d-learn


Works for me after adding the needed imports and removing the 
wrong include from the C file. Is this really the actual code 
you're running? Doesn't your C compiler reject that include? 
gcc does.


Okay, i think this C code should work (checked with cpp.sh):

#import 
char* write(char* text){
return text;
}
int main(void){
return 0;
}


but i still get the access violation.




Re: Help convert a C++ header to D

2016-01-03 Thread Rikki Cattermole via Digitalmars-d-learn

On 04/01/16 12:42 AM, Arialis Majoris wrote:

On Sunday, 3 January 2016 at 08:29:22 UTC, Rikki Cattermole wrote:

On 03/01/16 9:26 PM, Arialis Majoris wrote:

On Sunday, 3 January 2016 at 06:07:09 UTC, Rikki Cattermole wrote:

On 03/01/16 7:04 PM, Arialis Majoris wrote:

[...]


It's really quite simple.
You would probably only need like 2 regex's to do it.


If that's the case, shouldn't there be an automated utility? Seems like
it would be more complex? I thought there was a D tool that converted C
headers into D?


htod is pretty old at this point and is Windows only.

Most headers don't port well. This one will.
Its just so simple and constant the pattern.


Could you explain instead of telling me that? ;) It doesn't help me if I
don't know it too!

What won't convert well that I need to worry about?


The reason I didn't go into it any further is because I don't want to 
(lazy).


You can safely remove the file guard #ifndef and #defines along with the 
last #endif.


Replace with regex:
- class ...; with struct ...;
- typedef ... ...; with alias ... = ...;
- #if defined(REAPERAPI_WANT___mergesort) || !defined(REAPERAPI_MINIMAL)
  with static if (__traits(compiles, REAPERAPI_WANT__mergesort) || 
!__traits(compiles, REAPERAPI_MINIMAL)) {

- #endif with }
- bool (*AddExtensionsMainMenu)(); with bool AddExtensionsMainMenu();
- REAPERAPI_DEF with (nothing)
-

Code like:

#ifdef REAPERAPI_DEF
#undef REAPERAPI_DEF
#endif
#ifdef REAPERAPI_IMPLEMENT
  #define REAPERAPI_DEF
#else
  #define REAPERAPI_DEF extern
#endif

Just remove it


You may need to be a bit carefil with those classes and changing them to 
structs. Worse case scenario have it alias'd to a pointer of the struct.


Overall its hacky but between e.g. awk and sed you'll get it done.


Re: Call C function - Access violation

2016-01-03 Thread anonymous via Digitalmars-d-learn

On 03.01.2016 14:01, TheDGuy wrote:

Okay, i think this C code should work (checked with cpp.sh):

#import 
char* write(char* text){
 return text;
}
int main(void){
 return 0;
}


Uh, no. 1) In C it's include, not import. 2) Now you have two main 
functions, that can't work.


You shouldn't get a segfault, though. You should get some compile/link 
error. Are you compiling the right files? Can the segfault be from 
something other than your program?


Re: Call C function - Access violation

2016-01-03 Thread Gary Willoughby via Digitalmars-d-learn

On Sunday, 3 January 2016 at 12:30:34 UTC, TheDGuy wrote:

I get an access violation with this code:

...


There are a few things you can do to improve your code to make it 
easier to debug.


1. When converting a D string to a char pointer for use with C, 
use `std.string.toStringz`:


http://dlang.org/phobos/std_string.html#.toStringz

2. When converting a char pointer to a D string use 
`std.conv.to!(string)`:


http://dlang.org/phobos/std_conv.html#.to

3. Define C-style char pointers in D as `const(char)*`.
4. Don't use the variable name `text` as it conflicts with 
`std.conv.text`.



I think I've noticed one problem with the code above. You are 
using `text.ptr`. You shouldn't do that because you are passing a 
pointer not an array. Just use `text`.


Re: Repeated struct definitions for graph data structures and in/out naming conflict in C library

2016-01-03 Thread anonymous via Digitalmars-d-learn

On 03.01.2016 14:30, data pulverizer wrote:

I am trying to access functionality in the glpk C library using
extern(C). It has graph structs in its header file that are specified in
an odd recurring manner that I cannot reproduce in D:


I don't see what's odd about this. What exactly are your struggling with?


typedef struct glp_graph glp_graph;
typedef struct glp_vertex glp_vertex;
typedef struct glp_arc glp_arc;


You can just drop these. http://dlang.org/ctod.html#tagspace


struct glp_graph
{
  ...
   glp_vertex **v; /* glp_vertex *v[1+nv_max];
};


Drop the semicolon after the struct declaration, and move the asterisks 
one place to the left (purely style):


struct glp_graph
{
glp_vertex** v;
}


struct glp_vertex
{
   ...
   glp_arc *in;
   glp_arc *out;
};


As above, and rename in/out to something else, e.g. in_ and out_:

struct glp_vertex
{
glp_arc* in_;
glp_arc* out_;
}


struct glp_arc
{
   glp_vertex *tail;
   glp_vertex *head;
   glp_arc *t_prev;
   glp_arc *t_next;
   glp_arc *h_prev;
   glp_arc *h_next;
   
};


Nothing new here.


you may also spot that the in, and out keywords are used as members in
the struct, which gives an error in D. These structs are required for
functions in the library so need to be included in the D interface file.


Just rename them to something else. In D code that uses the struct, you 
use the new names. C code doesn't need to be changed, as the name 
doesn't matter when compiled.


Re: Access Violation when accessing Dynamic Array

2016-01-03 Thread tsbockman via Digitalmars-d-learn

On Sunday, 3 January 2016 at 03:41:12 UTC, Jack wrote:
I didn't actually knew it works that way because last time I 
tried using '==' directly it won't compare.


Well, there are always many ways that things can go wrong 
(including compiler bugs), but it SHOULD work, so ask on the 
forums if it doesn't.



Yeah I did that in a panic attempt to fix the problem.


Ah yes. I've been there before, myself...

I always thought that associative arrays are much more faster 
and more clean than this. Thanks I'll keep that in mind


The associative array itself is quite fast, as long as the data 
set doesn't have too many hash collisions. But, the lookup as a 
whole is limited by the speed of the key type's hash code 
function, and of its comparison function.


For, say, an integer, both the hash code and the comparison can 
be accomplished with about two instructions. Thus, a 
value_type[int] array would be very fast.


However, you are using `string` values as the keys, which means 
that getting the hash code or doing a comparison involves looping 
over each character in the `string`. Obviously this is going to 
be slower.


This is not to say it's actually *slow* - don't feel bad about 
using an associative array with `string` keys if that's really 
what you need. (And you're likely to need one for *something* 
since you're writing a parser.)


Assuming all the key values (variable names) are known at compile 
time, using boolean variables directly like I suggested is 
(nearly) the fastest method possible, because the `string` lookup 
is done during compilation, and is translated into a simple 
memory access or register access in the generated assembly code.



And this is news to me too. I'll keep enums in mind.

Overall thank you for your help and advice. I'll seriously keep 
these in mind.


You're welcome.


Re: Repeated struct definitions for graph data structures and in/out naming conflict in C library

2016-01-03 Thread data pulverizer via Digitalmars-d-learn

Thanks library now compiles.

On Sunday, 3 January 2016 at 13:45:13 UTC, anonymous wrote:

On 03.01.2016 14:30, data pulverizer wrote:

I am trying to access functionality in the glpk C library using
extern(C). It has graph structs in its header file that are 
specified in

an odd recurring manner that I cannot reproduce in D:


I don't see what's odd about this. What exactly are your 
struggling with?



typedef struct glp_graph glp_graph;
typedef struct glp_vertex glp_vertex;
typedef struct glp_arc glp_arc;


You can just drop these. http://dlang.org/ctod.html#tagspace


struct glp_graph
{
  ...
   glp_vertex **v; /* glp_vertex *v[1+nv_max];
};


Drop the semicolon after the struct declaration, and move the 
asterisks one place to the left (purely style):


struct glp_graph
{
glp_vertex** v;
}


struct glp_vertex
{
   ...
   glp_arc *in;
   glp_arc *out;
};


As above, and rename in/out to something else, e.g. in_ and 
out_:


struct glp_vertex
{
glp_arc* in_;
glp_arc* out_;
}


struct glp_arc
{
   glp_vertex *tail;
   glp_vertex *head;
   glp_arc *t_prev;
   glp_arc *t_next;
   glp_arc *h_prev;
   glp_arc *h_next;
   
};


Nothing new here.

you may also spot that the in, and out keywords are used as 
members in
the struct, which gives an error in D. These structs are 
required for
functions in the library so need to be included in the D 
interface file.


Just rename them to something else. In D code that uses the 
struct, you use the new names. C code doesn't need to be 
changed, as the name doesn't matter when compiled.





Re: Call C function - Access violation

2016-01-03 Thread anonymous via Digitalmars-d-learn

On 03.01.2016 13:30, TheDGuy wrote:

I get an access violation with this code:

extern(C) char* write(char* text);

void main(string[] args){
 char[] text = "Hello World".dup; //.dup converts string to char[]
 text ~= '\0'; //append

 char* result = write(text.ptr); //you need .ptr
 const(char)[] s = cstr2dstr(result);
 writeln(s);
 readln(); //keep the window open
}

auto cstr2dstr(inout(char)* cstr)
{
 return cstr ? cstr[0 .. strlen(cstr)] : "";
}

--

#include std.stdio;

char* write(char* text){
 return text;
}



Works for me after adding the needed imports and removing the wrong 
include from the C file. Is this really the actual code you're running? 
Doesn't your C compiler reject that include? gcc does.


Re: Call C function - Access violation

2016-01-03 Thread Gary Willoughby via Digitalmars-d-learn

On Sunday, 3 January 2016 at 13:23:25 UTC, Gary Willoughby wrote:
I think I've noticed one problem with the code above. You are 
using `text.ptr`. You shouldn't do that because you are passing 
a pointer not an array. Just use `text`.


Forget this line, my mistake. Use `toStringz` and pass a pointer 
instead of an array.


Repeated struct definitions for graph data structures and in/out naming conflict in C library

2016-01-03 Thread data pulverizer via Digitalmars-d-learn

Dear D Gurus,

I am trying to access functionality in the glpk C library using 
extern(C). It has graph structs in its header file that are 
specified in an odd recurring manner that I cannot reproduce in D:


typedef struct glp_graph glp_graph;
typedef struct glp_vertex glp_vertex;
typedef struct glp_arc glp_arc;

struct glp_graph
{
 ...
  glp_vertex **v; /* glp_vertex *v[1+nv_max];
};

struct glp_vertex
{
  ...
  glp_arc *in;
  glp_arc *out;
};

struct glp_arc
{
  glp_vertex *tail;
  glp_vertex *head;
  glp_arc *t_prev;
  glp_arc *t_next;
  glp_arc *h_prev;
  glp_arc *h_next;
  
};


you may also spot that the in, and out keywords are used as 
members in the struct, which gives an error in D. These structs 
are required for functions in the library so need to be included 
in the D interface file.


How do I reproduce these structs in in D?

Thanks


Re: RedBlackTree and myClass

2016-01-03 Thread tsbockman via Digitalmars-d-learn

On Sunday, 3 January 2016 at 10:55:05 UTC, AntonSotov wrote:

import std.container.rbtree;

class myClass {
string str;
}


int main()
{
auto tree = new RedBlackTree!myClass;
return 0;
}


Error: mutable method object.Object.opCmp is not callable using 
a inout object
Error: template instance std.functional.binaryFun!("a < b", 
"a", "b").binaryFun!(inout(myClass), myClass) error 
instantiating

/

How to use RedBlackTree of the objects of of its class?


Internally, `RedBlackTree` needs to be able to compare two 
instances of your class. It cannot do so because you haven't 
implemented the comparison operators:

http://dlang.org/spec/operatoroverloading.html#eqcmp

It's trying to use the default Object.opCmp() method, but can't 
because it doesn't accept constant inputs (`inout` is one of the 
many ways to mark something as constant in D: 
http://dlang.org/spec/const3.html). You wouldn't want it to use 
the default method anyway, since it doesn't understand the 
meaning of your class's data fields.


class myClass {
string str;

override string toString() const {
return "{myClass: " ~ str ~ "}"; }

override size_t toHash() const {
return hashOf(str); }

override bool opEquals(Object that) const {
return opEquals(cast(myClass) that); }
bool opEquals(const myClass that) const {
return this.str == that.str; }

int opCmp(const myClass that) const {
if(this.str < that.str)
return -1; // less than
if(this.str > that.str)
return  1; // greater than

return 0; // equal
}
}

Note that all of the new methods are marked `const` on the right. 
This is to indicate that they do not mutate (change the value of) 
`this` instance when they are called. Similarly, the `const` in 
`const myClass that` is a promise by the method not to modify 
`that`.


This use of `const` is required to make `RedBlackTree` work, 
because it is apparently marking things as `inout` somewhere. 
Constant values (of whatever sort) cannot be passed to a function 
unless that function promises not to modify them.


It won't compile without the `toString()` either, because 
somewhere inside it wants to generate a `string` representation 
of a `myClass` instance, probably as part of an exception message.


`toHash()` is not required to make your trivial example work, but 
it will be needed if you want to be able to use your class as the 
key type for associative arrays:

http://dlang.org/spec/hash-map.html

Anyway, it's not too hard if you understand what's going on, and 
all of the functions I added are good things to have anyway, 
because lots of generic code expects some or all of them. But, 
the error messages aren't all that helpful if you didn't already 
know most of that.


Re: Deit variable referencing

2016-01-03 Thread Martin Tschierschke via Digitalmars-d-learn

On Saturday, 2 January 2016 at 00:15:32 UTC, Jason Jeffory wrote:

Ok, So Deit allows D code inside html... looks great.

But how do external variables work? If I create a variable in 
the server(such as a class), can an html file access it easily? 
(not having to jump through hoops)



doctype html
html
head
title D string interpolations test
body
- import std.algorithm : min;
p Four items ahead:
- foreach( i; 0 .. 4 )
- auto num = i+1;
p Item #{ num + extvar }
//- Unescaped output
p Prints 8: !{ min(10, 2*6, 8) }


here, extvar is a int located somewhere else(other deit html 
file that has ran(hopefully) or vibe.d project that created it.


(Obviously there has to be a way to get extvar)

E.g., If deit can have a special variable called context that 
each html file can access and along with the vibe.d project, 
then we can easily pass the variable.



doctype html
html
head
title D string interpolations test
body
- import std.algorithm : min;
p Four items ahead:
- foreach( i; 0 .. 4 )
- auto num = i+1;
p Item #{ num + (int)context["extvar"] }
//- Unescaped output
p Prints 8: !{ min(10, 2*6, 8) }

or whatever


I am a very new at this, but I think "the solution" is to call  
res.render! with all alias names you need.

Defining int extvar =... before; you use:

  res.render!("your-template.dt",extvar,..)

hope this helps. Regards mt.


Re: Deit Editor

2016-01-03 Thread Martin Tschierschke via Digitalmars-d-learn

On Friday, 1 January 2016 at 00:14:08 UTC, Jason Jeffory wrote:
Vibe.d uses deit files for html generation. Seems nice but 
haven't dived into it(just removes a but of nonsense and allows 
code integration for dynamic generation... sounds great!!).


But what about visual editing? It's nice to be ale to see the 
layout of the page directly without all the hassle of toggling 
back and forth between a browser and code editor. Also, there 
is the issue of syntax highlighting, code validation, etc...


I came across this

http://stackoverflow.com/questions/16080350/using-jade-with-wysiwyg-markdown-to-allow-users-to-edit-content

Any ideas to make life easier? (isn't that what it's all about?)


There was an answer pointing to 
https://www.npmjs.org/package/html2jade
and this to http://html2jade.aaron-powell.com/, which  allows you 
to convert online HTML to JADE.
So you can make your HTML layout on an traditional way and get 
the jade code.


Re: RedBlackTree and myClass

2016-01-03 Thread tsbockman via Digitalmars-d-learn

On Sunday, 3 January 2016 at 16:25:31 UTC, Tobi G. wrote:

On Sunday, 3 January 2016 at 14:49:59 UTC, tsbockman wrote:
Anyway, it's not too hard if you understand what's going on, 
and all of the functions I added are good things to have 
anyway, because lots of generic code expects some or all of 
them. But, the error messages aren't all that helpful if you 
didn't already know most of that.


But as a beginner it is quite disappointing to write all of 
these functions to just get it work.
Maybe i am a bad programmer, but i don't write and use the 
member functions above that often.
I often use the 'less' in the template arguments to get such 
things as comparison done, and implement these functions only 
if i have to..

To get it work this should be enough:

import std.container.rbtree;

class myClass {
string str;

override string toString() const {
return "{myClass: " ~ str ~ "}"; }
}

void main()
{
auto tree = new RedBlackTree!(myClass, "a.str < b.str");
}

~ togrue


It just depends on what the class is for.

If it's a private internal data structure which is only used a 
few places, then sure - just use the minimum code required to get 
the job done.


But, if it's a part of the public API for a module and the class 
logically has a natural ordering, it's better to specify its 
behavior once in the class definition, rather than re-implement 
it everywhere that needs it.


Obviously there isn't much point to creating a class that does 
nothing bug wrap `string`, so I tried to provide the generic 
solution which can be easily extended to do the right thing when 
the class is fleshed out to actually do whatever it is really 
supposed to do.


And if you really just want a named `string` wrapper, why not do 
this?


class myClass {
string str;
alias str this;
}


Re: @property not available for classes?

2016-01-03 Thread Steven Schveighoffer via Digitalmars-d-learn

On 1/1/16 9:08 PM, Shriramana Sharma wrote:

John wrote:


It's nothing to do with the @property attribute. So you need to
define a constructor. Also, use "new" when creating instances.


Thanks Simon and John. First actual usage of D classes and mistaken
assumption that C++ syntax is valid. :-)



class constructor requirements are much different from struct 
constructor requirements. There's also no implicit constructor that 
initializes all members as there is for structs.


-Steve


Re: CMake support for D

2016-01-03 Thread Trent Forkert via Digitalmars-d-learn
On Sunday, 3 January 2016 at 17:30:15 UTC, Dibyendu Majumdar 
wrote:

Does CMake recognise D in the enable_language command?


No.


If not is there a workaround?


I have a fork of CMake that adds D support here: 
https://github.com/trentforkert/cmake
It's been a while since I published updates, but it should still 
work. If you encounter bugs please file them.


Re: RedBlackTree and myClass

2016-01-03 Thread Tobi G. via Digitalmars-d-learn

On Sunday, 3 January 2016 at 16:44:35 UTC, tsbockman wrote:
If it's a private internal data structure which is only used a 
few places, then sure - just use the minimum code required to 
get the job done.


But, if it's a part of the public API for a module and the 
class logically has a natural ordering, it's better to specify 
its behavior once in the class definition, rather than 
re-implement it everywhere that needs it.


Yes. I totally agree with you.
In the public API of a module, it can make your life so much 
easier if it's reuseable in all useful ways.




CMake support for D

2016-01-03 Thread Dibyendu Majumdar via Digitalmars-d-learn

Does CMake recognise D in the enable_language command?

If not is there a workaround?

Thanks and Regards
Dibyendu


Re: Call C function - Access violation

2016-01-03 Thread TheDGuy via Digitalmars-d-learn

On Sunday, 3 January 2016 at 13:25:04 UTC, Gary Willoughby wrote:
On Sunday, 3 January 2016 at 13:23:25 UTC, Gary Willoughby 
wrote:
I think I've noticed one problem with the code above. You are 
using `text.ptr`. You shouldn't do that because you are 
passing a pointer not an array. Just use `text`.


Forget this line, my mistake. Use `toStringz` and pass a 
pointer instead of an array.


Hi and thanks for your answer. My code looks now like this:

void main(string[] args){
const(char)* val = "Hello World".std.string.toStringz;
char* result = write(val);
const(char)[] s = cstr2dstr(result);
writeln(s);
readln(); //keep the window open
}

But now i get the error: "function expected before(), not package 
std of type void" (refering to line 2).


And if i define the variable 'value' as 'const(char)*' i also 
have to rewrite my C-function to accept const(char)*...


Re: @property not available for classes?

2016-01-03 Thread Jacob Carlborg via Digitalmars-d-learn

On 2016-01-03 18:48, Steven Schveighoffer wrote:


class constructor requirements are much different from struct
constructor requirements. There's also no implicit constructor that
initializes all members as there is for structs.


To clarify, there's a default (implicit) constructor that initializes 
all members to what they are set to in the class declaration. But you 
cannot pass in any arguments to the default constructor. Hmm, 
technically that might actually not be the constructor that initializes 
the members, not sure.


--
/Jacob Carlborg