Re: Pointer to private structure

2016-12-18 Thread Nikhil Jacob via Digitalmars-d-learn

On Monday, 19 December 2016 at 06:21:10 UTC, ketmar wrote:
i bet that just trying this with D compiler will take less time 
than writing forum post.


I did try but it seems to give compilation failure... Let me try 
once more and I will get back with more details.


Re: Pointer to private structure

2016-12-18 Thread ketmar via Digitalmars-d-learn
i bet that just trying this with D compiler will take less time 
than writing forum post.


Re: Pointer to private structure

2016-12-18 Thread Nicholas Wilson via Digitalmars-d-learn

On Monday, 19 December 2016 at 05:51:09 UTC, Nikhil Jacob wrote:
In C, we can define a struct without body in an include file 
and use pointer to that structure


For examples in public header file.

struct data;
data* new_data();


We can then define the elements of struct data privately inside 
the implementation of library.


Can we do this in D without using void* ?


Yes.


Pointer to private structure

2016-12-18 Thread Nikhil Jacob via Digitalmars-d-learn
In C, we can define a struct without body in an include file and 
use pointer to that structure


For examples in public header file.

struct data;
data* new_data();


We can then define the elements of struct data privately inside 
the implementation of library.


Can we do this in D without using void* ?




Re: Returning structs from COM

2016-12-18 Thread evilrat via Digitalmars-d-learn

On Saturday, 3 December 2016 at 09:51:00 UTC, John C wrote:
Some DirectX methods return structs by value, but when I try 
calling them I either get garbage or an access violation.


Usually COM methods return structs by pointer as a parameter, 
but these are returning the struct as the actual return value, 
as in this definition:


  extern(Windows):
  struct D2D1_SIZE_F { float width, height; }

  interface ID2D1Bitmap : ID2D1Image {
D2D1_SIZE_F GetSize();
  }

If I rewrite GetSize to return by pointer as a parameter, it 
appears to work and I get the correct width and height without 
an AV being thrown. And I can add a helper method that returns 
by value:


  interface ID2D1Bitmap : ID2D1Image {
void GetSize(D2D1_SIZE_F* size);

final D2D1_SIZE_F GetSize() {
  D2D1_SIZE_F size;
  GetSize();
  return size;
}
  }

But does anyone know why the original definition works in C++ 
but not D? Is it a bug? (I'm compiling with -m64.)


most likely due to this
https://issues.dlang.org/show_bug.cgi?id=16527

and that is annoying. i tried ldc2 1.1.beta6 and dmd 2.072.1 both 
x86 and x64 and it crashes anyways, sooner or later.


btw if you make call with return pointer instead of value it can 
probably messed up your stack already and crash any moment later.


in my DirectX bindings i used to return ref struct(though i think 
this is dirty hack), and it was working until recent.


Re: sort, .array and folding on immutable data (finding most common character in column of matrix)

2016-12-18 Thread Nicholas Wilson via Digitalmars-d-learn

On Sunday, 18 December 2016 at 22:26:50 UTC, Ali wrote:
Hey, so I have this data file that has a list of a string of 
characters separated by new lines. The task is to find the most 
common letter in each column. Ie if file is:


abc
axy
cxc

Then the letters are a (column 1), x and c.

I've written the code to do this at compile time. But I have a 
few questions about sorting, immutablity casting and the need 
to use .array. The code follows:


==
import std.stdio, std.range, std.algorithm;

// Line 1
static immutable data = 
import("data_06.txt").split("\n").transposed.map!"a.array.sort().group.array".array;


// Line 2
alias T = typeof(data[0][0]);

auto redux(T[] charData) {
return charData.fold!((a, b) {
return a[1] > b[1] ? a : typeof(a)(b[0], b[1]);
})(T.init);
}

// Line 3
static immutable word = data.map!redux.array.map!"a[0]".array;

void main() {
word.writeln;
}
==

Well mostly I'm looking for how to simplify this even further. 
So any hints there would be awesome.


And, there're a few questions I have about the code (I guess 
mainly because of my of my lack of understanding of the type 
system)


1. The first line with the splitting, I need to use .array 
three times. The last one I understand is because on "line 2" I 
alias T as the type of the data, and if I don't call .array 
then it's a MapResult type which has no opIndex. Yes?


2. On "line 1" I have to call .array.sort(). Why can't I just 
call .sort() on the transposed rows?




Because sort requires a random access range.

3. If I call .sort (without parenthesis) I get a compiler 
error. What up with that?


Unfortunately arrays have a builtin property sort that for some 
reason takes precedence of std.algorithm.sort when called without 
(). The compiler error is probably because .sort is a mutating 
operation and you're working with immutable data.




4. On "line 3" I call map with the free function "redux". In 
this function, if I return just "a", it's all good. If I return 
"b", then I get "Incompatible function/seed/element: 
__lambda2/Tuple!(dchar, uint)/immutable(Tuple!(dchar, uint))". 
So my seed is not immutable, but the elements of "data" are. I 
can understand that. But how do I work around it without having 
to do "typeof(a)(b[0], b[1])" ?


Try
 })(ImmutableOf!T.init);
or use the seedless version of fold.
may need to import std.traits(?)


5. Is there anyway to get rid of the the "alias" I have in the 
code and just use an inline lambda in my map call on "line 3"?


does `data.map!fold!"a[1] > b[1] ? a : b".map!"a[0]".array;` work?



sort, .array and folding on immutable data (finding most common character in column of matrix)

2016-12-18 Thread Ali via Digitalmars-d-learn
Hey, so I have this data file that has a list of a string of 
characters separated by new lines. The task is to find the most 
common letter in each column. Ie if file is:


abc
axy
cxc

Then the letters are a (column 1), x and c.

I've written the code to do this at compile time. But I have a 
few questions about sorting, immutablity casting and the need to 
use .array. The code follows:


==
import std.stdio, std.range, std.algorithm;

// Line 1
static immutable data = 
import("data_06.txt").split("\n").transposed.map!"a.array.sort().group.array".array;


// Line 2
alias T = typeof(data[0][0]);

auto redux(T[] charData) {
return charData.fold!((a, b) {
return a[1] > b[1] ? a : typeof(a)(b[0], b[1]);
})(T.init);
}

// Line 3
static immutable word = data.map!redux.array.map!"a[0]".array;

void main() {
word.writeln;
}
==

Well mostly I'm looking for how to simplify this even further. So 
any hints there would be awesome.


And, there're a few questions I have about the code (I guess 
mainly because of my of my lack of understanding of the type 
system)


1. The first line with the splitting, I need to use .array three 
times. The last one I understand is because on "line 2" I alias T 
as the type of the data, and if I don't call .array then it's a 
MapResult type which has no opIndex. Yes?


2. On "line 1" I have to call .array.sort(). Why can't I just 
call .sort() on the transposed rows?


3. If I call .sort (without parenthesis) I get a compiler error. 
What up with that?


4. On "line 3" I call map with the free function "redux". In this 
function, if I return just "a", it's all good. If I return "b", 
then I get "Incompatible function/seed/element: 
__lambda2/Tuple!(dchar, uint)/immutable(Tuple!(dchar, uint))". So 
my seed is not immutable, but the elements of "data" are. I can 
understand that. But how do I work around it without having to do 
"typeof(a)(b[0], b[1])" ?


5. Is there anyway to get rid of the the "alias" I have in the 
code and just use an inline lambda in my map call on "line 3"?


Re: meaning of "auto ref const"?

2016-12-18 Thread Picaud Vincent via Digitalmars-d-learn

On Sunday, 18 December 2016 at 14:32:08 UTC, kinke wrote:

TypeInfo_Struct apparently requires (or used to require) an 
`int opCmp(ref const T rhs)` overload, i.e., a version taking 
the rhs lvalue argument by reference (see 
https://dlang.org/spec/operatoroverloading.html#compare). Note 
that there are other overloads afterwards which take the rhs 
argument by value, thereby allowing rhs rvalues too.


Thank you for your complementary answer and explanation. All 
these look less strange to me now.


Re: meaning of "auto ref const"?

2016-12-18 Thread Picaud Vincent via Digitalmars-d-learn

On Sunday, 18 December 2016 at 14:25:04 UTC, Basile B. wrote:

...
As you can see, auto ref is more flexible with the parameter. 
This make sense for templated functions.


Thank you for your detailed answer, things are perfectly clear 
now. Also sorry for the doc linksI should have found it before 
asking my question.


Re: meaning of "auto ref const"?

2016-12-18 Thread kinke via Digitalmars-d-learn

On Sunday, 18 December 2016 at 13:14:08 UTC, Picaud Vincent wrote:

bool opEquals()(auto ref const BigInt y) const pure @nogc
{
   return sign == y.sign && y.data == data;
}

my problem is that I do not understand the role/meaning of 
"auto" in this context.


See https://dlang.org/spec/template.html#auto-ref-parameters. 
It's used to end up with an `opEquals(ref const BigInt y)` for 
lvalue args (passed by reference) and with an `opEquals(const 
BigInt y)` for rvalue args (passed by value => implicitly moved 
in D (as they are rvalues)).


Moreover in the opCmp code, "auto" is not present anymore, 
which is an extra source of confusions for me.


int opCmp(ref const BigInt y) pure nothrow @nogc const
{
   // Simply redirect to the "real" opCmp implementation.
   return this.opCmp!BigInt(y);
}


TypeInfo_Struct apparently requires (or used to require) an `int 
opCmp(ref const T rhs)` overload, i.e., a version taking the rhs 
lvalue argument by reference (see 
https://dlang.org/spec/operatoroverloading.html#compare). Note 
that there are other overloads afterwards which take the rhs 
argument by value, thereby allowing rhs rvalues too.


Re: meaning of "auto ref const"?

2016-12-18 Thread Basile B. via Digitalmars-d-learn

On Sunday, 18 December 2016 at 13:14:08 UTC, Picaud Vincent wrote:

Reading std/bigint.d code:

https://github.com/dlang/phobos/blob/00c1cc3b0d354363793c8b419ce84da722578138/std/bigint.d#L589

I have seen this:

bool opEquals()(auto ref const BigInt y) const pure @nogc
{
   return sign == y.sign && y.data == data;
}

my problem is that I do not understand the role/meaning of 
"auto" in this context.



With auto ref, the parameter can be either a LValue or a RValue.
When passing a struct as auto ref, it's taken as ref.
for example:

struct Foo{this(this){writeln("copy");}}
struct Bar{@disable this(this);}
void testAsRef(T)(ref T t){}
void testAsValue(T)(T t){}
void testRefOrValue(T)(auto ref T t){}

Foo foo;
Bar bar;

testAsRef(1); // error 1 is not ref
testAsRef(foo); // ok, not copied
testAsRef(bar); // ok, not copied

testAsValue(1); // ok
testAsValue(foo); // ok but copied
testAsValue(bar); // error, could only be copied but postblit is 
disabled


testRefOrValue(1); // ok, not taken as ref
testRefOrValue(foo); // ok, not copied
testRefOrValue(bar); // ok, taken as ref

As you can see, auto ref is more flexible with the parameter. 
This make sense for templated functions.


meaning of "auto ref const"?

2016-12-18 Thread Picaud Vincent via Digitalmars-d-learn

Reading std/bigint.d code:

https://github.com/dlang/phobos/blob/00c1cc3b0d354363793c8b419ce84da722578138/std/bigint.d#L589

I have seen this:

bool opEquals()(auto ref const BigInt y) const pure @nogc
{
   return sign == y.sign && y.data == data;
}

my problem is that I do not understand the role/meaning of "auto" 
in this context.


Moreover in the opCmp code, "auto" is not present anymore, which 
is an extra source of confusions for me.


int opCmp(ref const BigInt y) pure nothrow @nogc const
{
   // Simply redirect to the "real" opCmp implementation.
   return this.opCmp!BigInt(y);
}

What is the rational?

-

Another interrogation for me, who come from C++, is how to 
translate into D:


template void foo(T&& t);




Re: Using tango with dub

2016-12-18 Thread albert-j via Digitalmars-d-learn
Have you seen https://github.com/economicmodeling/containers it 
has a HashSet 
http://economicmodeling.github.io/containers/containers/hashset.HashSet.html


Just curious, how is it different from Tango's implementation?




Re: Using tango with dub

2016-12-18 Thread Jacob Carlborg via Digitalmars-d-learn

On 2016-12-17 21:15, bauss wrote:


I thought Tango was obsolete a long time ago.


It's a third party library like any other library.

--
/Jacob Carlborg


Re: Using tango with dub

2016-12-18 Thread Jacob Carlborg via Digitalmars-d-learn

On 2016-12-18 10:43, albert-j wrote:

Try an older version.


Before resorting to that, I am also trying to "dub build
--compiler=gdc". Getting different types of errors:

../../../.dub/packages/tango-1.0.3_2.068/tango/tango/math/IEEE.d:614:17:
error: instead of C-style syntax, use D-style syntax 'real[3][] vals'
[-Werror]
 static real vals[][3] = // x,frexp,exp
...
  ^
cc1d: all warnings being treated as errors

I tried to suppress the errors by adding
buildRequirements:["allowWarnings", "silenceWarnings"] to dub.json, but
no luck. Is it possible to get around them?


I don't know. Seems like the -Werror flag is passed, somewhere.

--
/Jacob Carlborg


Re: Using tango with dub

2016-12-18 Thread albert-j via Digitalmars-d-learn

Try an older version.


Before resorting to that, I am also trying to "dub build 
--compiler=gdc". Getting different types of errors:


../../../.dub/packages/tango-1.0.3_2.068/tango/tango/math/IEEE.d:614:17: error: 
instead of C-style syntax, use D-style syntax 'real[3][] vals' [-Werror]
 static real vals[][3] = // x,frexp,exp
...
  ^
cc1d: all warnings being treated as errors

I tried to suppress the errors by adding 
buildRequirements:["allowWarnings", "silenceWarnings"] to 
dub.json, but no luck. Is it possible to get around them?