Re: UFCS and "with" statement

2013-06-12 Thread Rob T

On Wednesday, 12 June 2013 at 11:05:22 UTC, bearophile wrote:

Rob T:

Rather disappointing that this fails. Anyone know if this is 
an expected limitation of UFCS or a bug?


Probably no one thought on this case. Why do you think it's 
useful?


Bye,
bearophile


If we're to use UFCS as a means to externally extend classes and 
structs, then UFCS should work as an extension under a with 
statement, otherwise UFCS is not living up to its full potential.


struct S
{
   void a(){ ... }
   void b(){ ... }
   void d(){ ... }
}

void c(S s){ ... }

void main()
{
  with (s)
  {
 a;
 b;
 c; // should be able to infer s.c just as it can infer s.b, 
etc

 d;
  }
}

--rt


Re: GtkD: Best way to get TreeStore out of TreeView.Model

2013-06-12 Thread Alex Horvat

On Wednesday, 12 June 2013 at 21:44:55 UTC, Mike Wey wrote:

On 06/11/2013 07:55 PM, Alex Horvat wrote:

On Tuesday, 11 June 2013 at 17:41:59 UTC, Mike Wey wrote:

On 06/11/2013 05:56 PM, Alex Horvat wrote:

TreeStore store = cast(TreeStore)tvTreeView.getModel();
In this case store == null


I think that one should work, how are you setting/creating 
the TreeStore?


getModel returns an interface, so try the following:

TreeStore store = cast(TreeStore)cast(void*)tv.getModel();


Thanks

Ok, just tried this and after setting store if I call
writeln(store) the output is what looks like a memory dump,
definitly something wrong there.
And the program crashes if I try to use store.


Re: Redirecting C++ ostreams

2013-06-12 Thread Jeremy DeHaan
On Wednesday, 12 June 2013 at 16:15:08 UTC, Craig Dillabaugh 
wrote:


Do you have access to the source code of the library? Or are you
just linking to it?


I do some-what.

I don't want to change the source code of the library itself, but 
I can change the C/C++ code of the binding that is used by D.


If nothing else, I suppose I could write to the ostream directly, 
but I thought that it would be nice to be able to use stderr in 
all of the D code.


Re: So I found this using 2 to the power of >= 31

2013-06-12 Thread bearophile

Carlos:


What do I have to know about how D works with data ?


If you want to avoid the overflow, then use a BigInt from 
std.bigint:


import std.stdio, std.bigint;

void main() {
foreach (immutable i; 0 .. 100)
writeln(i, " ", 2.BigInt ^^ i);
}

Bye,
bearophile


Re: So I found this using 2 to the power of >= 31

2013-06-12 Thread Carlos

On Thursday, 13 June 2013 at 02:03:35 UTC, Jonathan M Davis wrote:

On Thursday, June 13, 2013 03:46:59 Carlos wrote:

import std.stdio;
import std.math : pow;

void main()
{
cast(ulong)count;


That line won't compile.


foreach (count; 1 .. 33){
write((2)^^(count), " : ", count, "\n");
}
}

same output.


If you want to set the type of count, then give it a type 
instead of letting
foreach infer it. Integeral literals are inferred to be int, so 
if you don't

give count a type, it'll be int.

import std.stdio;
import std.math : pow;

void main()
{
    foreach(ulong count; 1 .. 33)
        writefln("%s: %s", 2^^count, count);
}

- Jonathan M Davis


Great! Thanks!

I would use another space before the ":" .
 writefln("%s : %s", 2^^count, count);


Re: So I found this using 2 to the power of >= 31

2013-06-12 Thread Jonathan M Davis
On Thursday, June 13, 2013 03:46:59 Carlos wrote:
> import std.stdio;
> import std.math : pow;
> 
> void main()
> {
> cast(ulong)count;

That line won't compile.

> foreach (count; 1 .. 33){
> write((2)^^(count), " : ", count, "\n");
> }
> }
> 
> same output.

If you want to set the type of count, then give it a type instead of letting 
foreach infer it. Integeral literals are inferred to be int, so if you don't 
give count a type, it'll be int.

import std.stdio;
import std.math : pow;

void main()
{
    foreach(ulong count; 1 .. 33)
        writefln("%s: %s", 2^^count, count);
}

- Jonathan M Davis


Re: So I found this using 2 to the power of >= 31

2013-06-12 Thread Carlos

import std.stdio;
import std.math : pow;

void main()
{
cast(ulong)count;
foreach (count; 1 .. 33){
write((2)^^(count), " : ", count,  "\n");
}
}

same output.


So I found this using 2 to the power of >= 31

2013-06-12 Thread Carlos

I have this code :

import std.stdio;
import std.c.stdlib;

void main()
{
foreach (count; 1 .. 33){
write((2)^^(count), " : ", count,  "\n");
}
exit (0);
}
And here is the output :

2 : 1
4 : 2
8 : 3
16 : 4
32 : 5
64 : 6
128 : 7
256 : 8
512 : 9
1024 : 10
2048 : 11
4096 : 12
8192 : 13
16384 : 14
32768 : 15
65536 : 16
131072 : 17
262144 : 18
524288 : 19
1048576 : 20
2097152 : 21
4194304 : 22
8388608 : 23
16777216 : 24
33554432 : 25
67108864 : 26
134217728 : 27
268435456 : 28
536870912 : 29
1073741824 : 30
-2147483648 : 31
0 : 32


---

Everything goes well until the power of 31 and then above that it 
will be cero.


What do I have to know about how D works with data ?


Re: How to use Power on D

2013-06-12 Thread Carlos

On Thursday, 13 June 2013 at 00:27:33 UTC, bearophile wrote:

Carlos:


Thank you for your time.


That's one bitwise operator. You want ^^

Bye,
bearophile


I didn't understoof in the first try but Infiltrator told me on 
the #d irc chat and here is the new code.


import std.stdio;
import std.c.stdlib;

void main()
{
foreach (count; 1 .. 16){
write("Result : ", (2)^^(count), " from : ", count, "\n");
}
}


Re: How to use Power on D

2013-06-12 Thread bearophile

Carlos:


Thank you for your time.


That's one bitwise operator. You want ^^

Bye,
bearophile


Re: How to use Power on D

2013-06-12 Thread Infiltrator

On Thursday, 13 June 2013 at 00:24:18 UTC, Carlos wrote:
So I have this code I'm working on but I get weird results. 
What am I doing wrong ?


Code :

import std.stdio;
import std.c.stdlib;

void main()
{
foreach (count; 1 .. 16){
write("Result : ", (2)^(count), " from : ", count, "\n");
}
}

Prints:

Result : 3 from : 1
Result : 0 from : 2
Result : 1 from : 3
Result : 6 from : 4
Result : 7 from : 5
Result : 4 from : 6
Result : 5 from : 7
Result : 10 from : 8
Result : 11 from : 9
Result : 8 from : 10
Result : 9 from : 11
Result : 14 from : 12
Result : 15 from : 13
Result : 12 from : 14
Result : 13 from : 15

Thank you for your time.



I don't see the problem.  You're XORing 2 with 1..16 and getting 
the correct results.


Unless you actually want std.math.pow 
[http://dlang.org/phobos/std_math.html#.pow]?


How to use Power on D

2013-06-12 Thread Carlos
So I have this code I'm working on but I get weird results. What 
am I doing wrong ?


Code :

import std.stdio;
import std.c.stdlib;

void main()
{
foreach (count; 1 .. 16){
write("Result : ", (2)^(count), " from : ", count, "\n");
}
}

Prints:

Result : 3 from : 1
Result : 0 from : 2
Result : 1 from : 3
Result : 6 from : 4
Result : 7 from : 5
Result : 4 from : 6
Result : 5 from : 7
Result : 10 from : 8
Result : 11 from : 9
Result : 8 from : 10
Result : 9 from : 11
Result : 14 from : 12
Result : 15 from : 13
Result : 12 from : 14
Result : 13 from : 15

Thank you for your time.


Re: Compiler errors and recursive nested functions

2013-06-12 Thread Ali Çehreli

On 06/12/2013 03:49 PM, Dylan Knutson wrote:

> DPaste has stopped responding to requests at the time of writing,

Being an old ;) old-timer I never understood the need for those sites.

> 
> import std.algorithm : map;
> import std.range : isInputRange, ElementType;

As a workaround, make that lambda a free-standing function here:

auto intermediate(T, alias pred)(T a)
{
return a.deepMap!(pred)();
}

> // Neither does this, for the same reason
> auto deepMap(alias pred, Range)(Range r)
> if(isInputRange!Range)
> {
>  static if(isInputRange!(ElementType!Range))
>  {
>  return r.map!( a => a.deepMap!(pred)() )();

And use that free-standing instead:

return r.map!(intermediate!(ElementType!Range, pred))();

Ali



Compiler errors and recursive nested functions

2013-06-12 Thread Dylan Knutson
Hello! I'm trying to make a deepMap function which operates on 
the deepest non-range element of an array. I think that this 
function should theoretically work, however there are some 
compiler errors that I can't quite understand.


Here's the code: http://dpaste.dzfl.pl/97918311

And the compiler error because DPaste's compiler service seems to 
be offline:


src\algorithm_util.d(26): Error: function 
algorithm_util.__unittestL34_5.deepMap!(__lambda4, int[]).deepMap 
is a nested function and cannot be accessed from 
algorithm_util.__unittestL34_5.deepMap!(__lambda4, 
int[][]).deepMap.__lambda2300!(int[]).__lambda2300DMD

v2.064 DEBUG

deepMap works just fine if I pass in an array that isn't nested, 
so this will work:


auto a = [1, 2, 3];
auto b = a.deepMap!(a => a + 1)();
writeln(b);
// "[2, 3, 4]"

Can someone explain what is going on?

Thank you,
Dylan Knutson


Re: Compiler errors and recursive nested functions

2013-06-12 Thread Dylan Knutson
DPaste has stopped responding to requests at the time of writing, 
so here's the code in case its still down by the time someone 
reads this:




import std.algorithm : map;
import std.range : isInputRange, ElementType;

// Doesn't work
//template deepMap(alias pred, Range)
//if(isInputRange!Range)
//{
//  static if(isInputRange!(ElementType!Range))
//  {
//  alias deepMap = map!(a => deepMap!(pred, typeof(a))(a));
//  }
//  else
//  {
//  alias deepMap = map!(pred);
//  }
//}

// Neither does this, for the same reason
auto deepMap(alias pred, Range)(Range r)
if(isInputRange!Range)
{
static if(isInputRange!(ElementType!Range))
{
return r.map!( a => a.deepMap!(pred)() )();
}
else
{
return r.map!(pred)();
}
}

unittest {
import std.stdio;

auto a = [[1, 1], [1, 1]];
auto b = a.deepMap!(a => a + 1)();
writeln(b);
//assert(b == [[2, 2], [2, 2]]);
}




Re: Compiler errors and recursive nested functions

2013-06-12 Thread Ali Çehreli

On 06/12/2013 03:45 PM, Dylan Knutson wrote:

Hello! I'm trying to make a deepMap function which operates on the
deepest non-range element of an array. I think that this function should
theoretically work, however there are some compiler errors that I can't
quite understand.

Here's the code: http://dpaste.dzfl.pl/97918311

And the compiler error because DPaste's compiler service seems to be
offline:

src\algorithm_util.d(26): Error: function
algorithm_util.__unittestL34_5.deepMap!(__lambda4, int[]).deepMap is a
nested function and cannot be accessed from
algorithm_util.__unittestL34_5.deepMap!(__lambda4,
int[][]).deepMap.__lambda2300!(int[]).__lambda2300DMD
v2.064 DEBUG

deepMap works just fine if I pass in an array that isn't nested, so this
will work:

 auto a = [1, 2, 3];
 auto b = a.deepMap!(a => a + 1)();
 writeln(b);
 // "[2, 3, 4]"

Can someone explain what is going on?

Thank you,
Dylan Knutson


Perhaps this limitation:

  http://d.puremagic.com/issues/show_bug.cgi?id=5710

Ali



Re: Template Trick

2013-06-12 Thread Ali Çehreli

On 06/12/2013 02:56 PM, matovitch wrote:

On Wednesday, 12 June 2013 at 21:52:46 UTC, Ali Çehreli wrote:


Here is one way:

void func(V, alias def_val) (uint i, V v)
if (is (typeof(def_val == V.type)))


Oops. It should be:

if (is (typeof(def_val) == V.type))

Hmmm. How come the other one worked as well? Because the type of 
(def_val == V.type) is compiled as bool and since bool is a valid type, 
'is' passes.



{
if (i < v.dimension) {
v.data[i] = def_val;
}
}

Ali


Thank you !


Ali



Re: Template Trick

2013-06-12 Thread bearophile

matovitch:


void func(V, V.type def_val) (uint i, V v)
{
if (i < v.dimension) {
v.data[i] = def_val;
}
}


I think something like that is not yet possible, but maybe it 
will be possible later.


Your code has also allowed me to find a new small compiler bug 
that I have just filed:


http://d.puremagic.com/issues/show_bug.cgi?id=10346

Bye,
bearophile


Re: Template Trick

2013-06-12 Thread matovitch

On Wednesday, 12 June 2013 at 21:52:46 UTC, Ali Çehreli wrote:


Here is one way:

void func(V, alias def_val) (uint i, V v)
if (is (typeof(def_val == V.type)))
{
if (i < v.dimension) {
v.data[i] = def_val;
}
}

Ali


Thank you !


Re: Template Trick

2013-06-12 Thread Ali Çehreli

On 06/12/2013 02:47 PM, matovitch wrote:

To be more precise the code below doesn't compile :

struct Vector(T, uint N)
{
 alias T type;
 enum dimension = N;
 T data[N];
}

void func(V, V.type def_val) (uint i, V v)
{
 if (i < v.dimension) {
 v.data[i] = def_val;
 }
}

void main()
{
 alias Vector!(int, 3) Vec3i;
 Vec3i v;
 func!(Vec3i, 42)(2, v);
}

With the error message :

test.d(8): Error: no property 'type' for type 'V'
test.d(8): Error: V.type is used as a type




Here is one way:

void func(V, alias def_val) (uint i, V v)
if (is (typeof(def_val == V.type)))
{
if (i < v.dimension) {
v.data[i] = def_val;
}
}

Ali



Re: Template Trick

2013-06-12 Thread matovitch

To be more precise the code below doesn't compile :

struct Vector(T, uint N)
{
alias T type;
enum dimension = N;
T data[N];
}

void func(V, V.type def_val) (uint i, V v)
{
if (i < v.dimension) {
v.data[i] = def_val;
}
}

void main()
{
alias Vector!(int, 3) Vec3i;
Vec3i v;
func!(Vec3i, 42)(2, v);
}

With the error message :

test.d(8): Error: no property 'type' for type 'V'
test.d(8): Error: V.type is used as a type




Re: Template Trick

2013-06-12 Thread Ali Çehreli

On 06/12/2013 02:26 PM, matovitch wrote:> Hello,
>
> I got a simple vector template :
>
> struct Vector(T, uint N)
> {
>alias type T;

You later corrected that it should be

  alias T type;

But still, prefer the new syntax over the backward C syntax:

  alias type = T;

>T data[N];
> }
>
> And I'd like to call a function like :
>
> void func(V, V.type default_value)(args...);

So, that function is already defined and you are trying to call it? How 
are you calling it? What is the error message?


> But this (of course) doesn't work. Is there a simple and nice way
> to do this ? (I'm sure there is ;-))

It is not clear to me what the purpose is. :)

Ali



Re: GtkD: Best way to get TreeStore out of TreeView.Model

2013-06-12 Thread Mike Wey

On 06/11/2013 07:55 PM, Alex Horvat wrote:

On Tuesday, 11 June 2013 at 17:41:59 UTC, Mike Wey wrote:

On 06/11/2013 05:56 PM, Alex Horvat wrote:

TreeStore store = cast(TreeStore)tvTreeView.getModel();
In this case store == null


I think that one should work, how are you setting/creating the TreeStore?


getModel returns an interface, so try the following:

TreeStore store = cast(TreeStore)cast(void*)tv.getModel();

--
Mike Wey


Re: Template Trick

2013-06-12 Thread matovitch

On Wednesday, 12 June 2013 at 21:36:38 UTC, H. S. Teoh wrote:

On Wed, Jun 12, 2013 at 11:26:40PM +0200, matovitch wrote:

Hello,

I got a simple vector template :

struct Vector(T, uint N)
{
  alias type T;

[...]

This line should read:

alias type = T;

And it should work as you wanted.


T


This was a mistake (it's quite late) :

alias T type;


Re: Template Trick

2013-06-12 Thread H. S. Teoh
On Wed, Jun 12, 2013 at 11:26:40PM +0200, matovitch wrote:
> Hello,
> 
> I got a simple vector template :
> 
> struct Vector(T, uint N)
> {
>   alias type T;
[...]

This line should read:

alias type = T;

And it should work as you wanted.


T

-- 
If you look at a thing nine hundred and ninety-nine times, you are perfectly 
safe; if you look at it the thousandth time, you are in frightful danger of 
seeing it for the first time. -- G. K. Chesterton


Template Trick

2013-06-12 Thread matovitch

Hello,

I got a simple vector template :

struct Vector(T, uint N)
{
  alias type T;
  T data[N];
}

And I'd like to call a function like :

void func(V, V.type default_value)(args...);

But this (of course) doesn't work. Is there a simple and nice way
to do this ? (I'm sure there is ;-))

Thanks.


Re: Friend class and methods

2013-06-12 Thread Ali Çehreli

On 06/12/2013 12:12 PM, Maxime Chevalier-Boisvert wrote:

The closest is to put both classes and the function in the same
module. Things all in the same module can see the private members of
each other.


Good enough for my needs. Thanks.


Sometimes 'package' is more suitable than 'private'.

Ali



Re: Strange seg fault

2013-06-12 Thread Adam D. Ruppe
I think you've hit bug city in the associative array 
implementation. The "this" in there doesn't seem to point at 
anything meaningful. This is probably worthy of a bug report.


A potential workaround is to store pointers to your structs in 
the associative array, that way you can set them up ahead of 
time, then just do data["test"] = ptr; instead.


Re: Strange seg fault

2013-06-12 Thread Ali Çehreli

On 06/12/2013 01:12 PM, gedaiu wrote:

> Hi,
>
> Can anyone help me why this code goes wrong? I get exit code 139 with
> DMD 2.063
>
>
> import std.stdio;
>
>
>
> struct Test {
>  string val = "";
>  string[] key;
>
>  /**
>   * operator "=" overload
>   */
>  Test opAssign(string val){
>  this.val = val;
>
>  string[1] t;
>  t = val;
>
>  key ~= t;

Instead of the last three lines:

key ~= val;

However, if you really wanted a 1-element fixed-length array then:

string[1] t = [ val ];

>  return this;
>  }
>

Remove the following opAssign altogether:

>  Test opAssign(Test val){
>  this.val = val.val;
>
>  key ~= val.key;
>
>  return this;
>  }

As general rules:

* Define a post-blit only if the code would be incorrect if you don't do 
that. (For example, it may be incorrect that two objects share the same 
array.)


* Define an opAssign from the same type only if your implementation will 
be more efficient than the compiler's safe alternative, which happens to 
be "first copy, then swap." For example, instead of copying a member 
array, you may decide to reuse it.


>  void append(Test t) {
>  val ~= t.val;
>  key ~= t.key;
>  };
> }
>
> void main() {
>
>  Test val;
>  val = "test";
>
>  Test[string] data;
>  Test v;
>  v = "asd";
>  data["test"] = v;
>
>  writeln(v);
>  writeln(data["test"]);
>
>  data["test"].append(val);
>
>
>  writeln(data["test"].key);
>  writeln("done");
> }
>
> Thanks,
> Bogdan

Ali



Re: Associative multidimensional Arrays

2013-06-12 Thread Ali Çehreli

On 06/12/2013 01:20 PM, MaB wrote:

Hi!

I want to bulid up a IndexArray with a structure like this (PHP code):

$arrIndex = array(
  "A" => array(
   "B" => array()
 ),
  "B" => array(
   "B" => array("C" => array())
 )

);

The Keys are of Type string and the values can be arrays with the same
structure.
The Array-Depth has to be variable..
Is there a way in D to make it possible? I am trying it now since hours :(

Greetings





Pretty complex but I think this is it:

import std.stdio;
import std.array;

struct Node
{
Node[][string] children;
}

void main()
{
Node[] table;
table ~= Node();

table.back.children["A"] ~= Node();
table.back.children["A"].back.children["B"] ~= Node();

writeln(table);
}

Ali



Re: what keeps a COM object alive?

2013-06-12 Thread Richard Webb


I was referring to the COM server support stuff in the Juno 
library, which allocates COM objects outside the GC heap so the 
GC will never collect them.


See 
https://github.com/JesseKPhillips/Juno-Windows-Class-Library/blob/master/juno/com/core.d#L3147 
for an example.


Re: Associative multidimensional Arrays

2013-06-12 Thread gedaiu

Hi,

Please look at this thread. You might find your answer there:

http://forum.dlang.org/thread/vphniyxyvgsiazutt...@forum.dlang.org

Bogdan

On Wednesday, 12 June 2013 at 20:20:09 UTC, MaB wrote:

Hi!

I want to bulid up a IndexArray with a structure like this (PHP 
code):


$arrIndex = array(
 "A" => array(
  "B" => array()
),
 "B" => array(
  "B" => array("C" => array())
)

);

The Keys are of Type string and the values can be arrays with 
the same structure.

The Array-Depth has to be variable..
Is there a way in D to make it possible? I am trying it now 
since hours :(


Greetings




Associative multidimensional Arrays

2013-06-12 Thread MaB

Hi!

I want to bulid up a IndexArray with a structure like this (PHP 
code):


$arrIndex = array(
 "A" => array(
  "B" => array()
),
 "B" => array(
  "B" => array("C" => array())
)

);

The Keys are of Type string and the values can be arrays with the 
same structure.

The Array-Depth has to be variable..
Is there a way in D to make it possible? I am trying it now since 
hours :(


Greetings





Strange seg fault

2013-06-12 Thread gedaiu

Hi,

Can anyone help me why this code goes wrong? I get exit code 139 
with DMD 2.063



import std.stdio;



struct Test {
string val = "";
string[] key;

/**
 * operator "=" overload
 */
Test opAssign(string val){
this.val = val;

string[1] t;
t = val;

key ~= t;

return this;
}

Test opAssign(Test val){
this.val = val.val;

key ~= val.key;

return this;
}

void append(Test t) {
val ~= t.val;
key ~= t.key;
};
}

void main() {

Test val;
val = "test";

Test[string] data;
Test v;
v = "asd";
data["test"] = v;

writeln(v);
writeln(data["test"]);

data["test"].append(val);


writeln(data["test"].key);
writeln("done");
}

Thanks,
Bogdan


Re: Friend class and methods

2013-06-12 Thread Maxime Chevalier-Boisvert
The closest is to put both classes and the function in the same 
module. Things all in the same module can see the private 
members of each other.


Good enough for my needs. Thanks.


Friend class and methods

2013-06-12 Thread Maxime Chevalier-Boisvert
Does D have something like the concept of friend classes and 
functions in C++? I'd like to have a function that can access 
private members of two classes at the same time.


Re: Friend class and methods

2013-06-12 Thread Adam D. Ruppe
On Wednesday, 12 June 2013 at 19:01:24 UTC, Maxime 
Chevalier-Boisvert wrote:
Does D have something like the concept of friend classes and 
functions in C++?


The closest is to put both classes and the function in the same 
module. Things all in the same module can see the private members 
of each other.




Re: Friend class and methods

2013-06-12 Thread David Nadlinger
On Wednesday, 12 June 2013 at 19:01:24 UTC, Maxime 
Chevalier-Boisvert wrote:
Does D have something like the concept of friend classes and 
functions in C++? I'd like to have a function that can access 
private members of two classes at the same time.


No (t really), you have to stick the two classes into the same 
module.


David


Re: Strange error when importing std.regex

2013-06-12 Thread bearophile

Dmitry Olshansky:

There is no ambiguity, 1 is not a Regex object but it seems 
like template constraint in std.regex blows up.


@Temtaime please file a bug on this.
http://d.puremagic.com/issues/

std.algorithm.splitter(arr, 1);


I think this bug already surfaced some time ago... Maybe it's 
already in Bugzilla.


Bye,
bearophile


Re: Strange error when importing std.regex

2013-06-12 Thread Dmitry Olshansky

12-Jun-2013 17:28, bearophile пишет:

Temtaime:


How i can avoid this?


You have to qualify where the function comes from. One way to do it is
to use:



There is no ambiguity, 1 is not a Regex object but it seems like 
template constraint in std.regex blows up.


@Temtaime please file a bug on this.
http://d.puremagic.com/issues/

std.algorithm.splitter(arr, 1);

Bye,
bearophile



--
Dmitry Olshansky


Re: Why there is too many uneccessary casts?

2013-06-12 Thread captaindet

On 2013-06-11 19:48, captaindet wrote:

On 2013-06-11 07:35, Adam D. Ruppe wrote:

On Tuesday, 11 June 2013 at 10:12:27 UTC, Temtaime wrote:

ubyte k = 10;
ubyte c = k + 1;

This code fails to compile because of: Error: cannot implicitly
convert expression (cast(int)k + 1) of type int to ubyte


The reason is arithmetic operations transform the operands into ints,
that's why the error says cast(int)k. Then it thinks int is too big
for ubyte. It really isn't about overflow, it is about truncation.

That's why uint + 1 is fine. The result there is still 32 bits so
assigning it to a 32 bit number is no problem, even if it does
overflow. But k + 1 is promoted to int first, so it is a 32 bit
number and now the compiler complains that you are trying to shove it
into an 8 bit variable. Unless it can prove the result still fits in
8 bits, it complains, and it doesn't look outside the immediate line
of code to try to prove it. So it thinks k can be 255, and 255 + 1 =
256, which doesn't fit in 8 bits.

The promotion to int is something D inherited from C and probably
isn't going anywhere.


i think part of the problem is that '1' is an int. so the calculation must be 
promoted to integer.
if we had byte and ubyte integer literals (suffix b/B and ub/UB?), then if all 
RHS arguments are (unsigned) bytes the compiler could infer that we are serious 
with sticking to bytes...

ubyte k = 10; // or optional 10ub
ubyte c = k + 1ub;


clarification: i was only half serious. i understand that indicating a byte 
literal is not enough and that the promotion rules would have to be altered 
too. however, i think it would be backwards compatible. then the question is if 
enough ppl want this feature badly enough to push the issue. i don't think this 
is the case meaning we will have to accept this little quirk.


Re: Redirecting C++ ostreams

2013-06-12 Thread Craig Dillabaugh

On Wednesday, 12 June 2013 at 04:05:22 UTC, Jeremy DeHaan wrote:

Hey guys,

I have something I am curious about, but haven't had much luck 
with when doing research and experimenting.


Basically, I am working with a library that uses ostreams 
internally and I want to somehow redirect that to what ever 
stderr is pointing to.


The reason I am trying to do this is because I was considering 
the user might want to have some sort of log they want error 
messages written to, and if they redirect stderr to a file I'd 
like the underlying ostreams to be directed to the same 
location.


Any ideas or suggestions would be welcome!

Thanks in advance


Do you have access to the source code of the library? Or are you
just linking to it?


Re: what keeps a COM object alive?

2013-06-12 Thread finalpatch
"Richard Webb"  writes:

> On Wednesday, 12 June 2013 at 14:41:05 UTC, finalpatch wrote:
>
>>
>> This feels even more cumbersome than in C++ because in C++ we can
>> simply
>> delete this in the Release() method, there's no need to store a
>> reference in a global place.
>>
>
>
> Juno does this by allocating the object on the non-gc heap, adding it
> as a GC root, and then deleting it when the refcount reaches 0.
> It works ok, though I think the current implementation has some
> issues.

I was reading the CHello sample that ships with DMD. I don't see
anything that can prevent the GC from killing a running object, this
makes me wonder whether it is actually a correct sample. 

-- 
finalpatch


Re: what keeps a COM object alive?

2013-06-12 Thread Richard Webb

On Wednesday, 12 June 2013 at 14:41:05 UTC, finalpatch wrote:



This feels even more cumbersome than in C++ because in C++ we 
can simply

delete this in the Release() method, there's no need to store a
reference in a global place.




Juno does this by allocating the object on the non-gc heap, 
adding it as a GC root, and then deleting it when the refcount 
reaches 0.
It works ok, though I think the current implementation has some 
issues.


Re: Getting object members

2013-06-12 Thread Adam D. Ruppe

On Wednesday, 12 June 2013 at 10:59:42 UTC, Szymon Gatner wrote:
I know how to iterate over members when type is known at 
compile time (with __traits) but I can't find a documentation 
of how to get them polymorphically, I mean:


It hasn't been implemented in the runtime yet (though all the 
pieces are there), so unless each class makes the info available 
itself (or you want to modify your runtime) you can't.


For example:

claas Foo {
   int x, y;
   mixin ReflectionInfo!Foo;
}


Where ReflectionInfo is something you'd write. Jacob Carlborg has 
written a serialization library that works like this: 
https://github.com/jacob-carlborg/orange


But even so it wouldn't work through Object or classinfo unless 
you modify druntime. I'd like to see some functionality for this 
in for the next release (or maybe the one after it) though.


Also: Is there any article / tutorial on D's introspection 
capabilities. I heard it is pretty powerful and I would really 
like to try it.


I don't think so. The basic idea though is:

foreach(member; __traits(allMembers, SomeClass)) {
   // member is a string, the name of the member
   // get the thing like this:
   __traits(getMember, SomeClass, member)
}


Then you look into it with the other traits and use std.traits 
from the stdlib for helper functions.


Re: Strange error when importing std.regex

2013-06-12 Thread Temtaime

Oh, thanks very much.


Re: what keeps a COM object alive?

2013-06-12 Thread finalpatch
Hi Sean,

Thanks for your reply. I have no problem with client side COM
programming in D. What I'm asking is if I write a COM object in D (in
this case the D code is a COM server), then it looks like I have to
store a reference to that COM object in some globally reachable place
(eg. add it to a global array, so that it won't be destroyed by the GC
while the C++/.NET client is still using it), and then manually remove
that reference when the object's ref count drops to zero for the GC to
collect it.

This feels even more cumbersome than in C++ because in C++ we can simply
delete this in the Release() method, there's no need to store a
reference in a global place.

Sean Cavanaugh  writes:
> COM is by definition ref counted.  In D you generally need to store a
> COM pointer in a struct and maintain the refcount with that struct
> (increment on copy, decrement in destructor, etc).  Its not too hard,
> as 'alias this' usage can wrap the pointer's methods easily enough.
>
>

-- 
finalpatch


Re: Stack trace

2013-06-12 Thread New guy
Ahh, looks like the stack trace is supported only in debug mode. 
-g option made the exception to spit out the function names.



$ rdmd -g t.d
object.Exception@t.d(5): in f1

0x0040CC9C in char[][] 
core.sys.windows.stacktrace.StackTrace.trace()
0x0040CB27 in core.sys.windows.stacktrace.StackTrace 
core.sys.windows.stacktrace.StackTrace.__ctor()

0x0040204C in void t.f1() at C:\test\t.d(6)
0x00402058 in void t.f2() at C:\test\t.d(10)
0x00402064 in void t.f3() at C:\test\t.d(14)
0x00402070 in _Dmain at C:\test\t.d(17)
0x00402BD8 in extern (C) int rt.dmain2._d_run_main(int, char**, 
extern (C) int function(char[][])*).void runMain()
0x00402C0E in extern (C) int rt.dmain2._d_run_main(int, char**, 
extern (C) int function(char[][])*).void runAll()

0x00402811 in _d_run_main
0x00402128 in main
0x754D33AA in BaseThreadInitThunk
0x76F09EF2 in RtlInitializeExceptionChain
0x76F09EC5 in RtlInitializeExceptionChain





Stack trace

2013-06-12 Thread New guy
Could someone please tell me how can I get the stack trace when 
an exception is thrown? My test code:


Code: -

void f1() {
throw new Exception("in f1");
}

void f2() {
f1();
}

void f3() {
f2();
}

void main() {
f3();
}

Output: -

$ rdmd t.d
object.Exception@t.d(5): in f1

0x0040BC6C
0x0040BAF7
0x00402045
0x0040264E
0x00402251
0x00402074
0x754D33AA in BaseThreadInitThunk
0x76F09EF2 in RtlInitializeExceptionChain
0x76F09EC5 in RtlInitializeExceptionChain



I am expecting function names instead of some hex codes.

Thank you in advance.



Re: what keeps a COM object alive?

2013-06-12 Thread Sean Cavanaugh

On 6/11/2013 10:38 PM, finalpatch wrote:

A typical COM server would create a new object (derived from IUnknown),
return it to the caller (potentially written in other languages).
Because the object pointer now resides outside of D's managed heap, does
that mean the object will be destroyed when the GC runs? A normal COM
object written in C++ relies on reference counting to manage its life
cycle but in D the ref counting seems not serving any purpose. The
AddRef()/Release() of std.c.windows.com.ComObject maintains a ref count,
but doesn't really use it for any thing. There's a comment in Release()
says "let the GC reap it", but how does the GC know the object is okay
to destroy?


COM is by definition ref counted.  In D you generally need to store a 
COM pointer in a struct and maintain the refcount with that struct 
(increment on copy, decrement in destructor, etc).  Its not too hard, as 
'alias this' usage can wrap the pointer's methods easily enough.





Re: Strange error when importing std.regex

2013-06-12 Thread bearophile

Temtaime:


How i can avoid this?


You have to qualify where the function comes from. One way to do 
it is to use:


std.algorithm.splitter(arr, 1);

Bye,
bearophile


Clarification of @trusted attribute?

2013-06-12 Thread Gary Willoughby

I know the reason to mark a method as trusted from the docs:

Trusted functions are guaranteed by the programmer to not 
exhibit any undefined
behavior if called by a safe function. Generally, trusted 
functions should be kept

small so that they are easier to manually verify.


Undefined behavior happens when an illegal code construct is 
executed.
Undefined behavior can include random, erratic results, 
crashes, faulting, etc.

A buffer overflow is an example of undefined behavior.


So would you mark the following with @trusted? The format() 
function is not @safe but what is @trusted really trying to say? 
This method is @safe as far as i'm concerned? The arguments make 
format() @safe? I'm confused.


/**
 * Get the current timestamp for the log.
 *
 * Returns:
 * The current timestamp.
 */
private string getTimestamp() const
{
auto time = Clock.currTime();
		return format("%d/%02d/%02d %d:%02d:%02d", time.year, 
time.month, time.day, time.hour, time.minute, time.second);

}


Strange error when importing std.regex

2013-06-12 Thread Temtaime

Hello guys!

http://dpaste.1azy.net/9c4c3eb8
http://dpaste.1azy.net/afd8d20b

How i can avoid this?


Re: Variable shadowing bug?

2013-06-12 Thread bearophile

denizzz:


says what sfTime* can not be implictly converted to sfTime*


Maybe it's a diagnostic bug. Please create a minimal example.

Bye,
bearophile


Re: UFCS and "with" statement

2013-06-12 Thread bearophile

Rob T:

Rather disappointing that this fails. Anyone know if this is an 
expected limitation of UFCS or a bug?


Probably no one thought on this case. Why do you think it's 
useful?


Bye,
bearophile


Getting object members

2013-06-12 Thread Szymon Gatner

Hi,

I am trying to get members of a class via pointer to Object.

I know how to iterate over members when type is known at compile 
time (with __traits) but I can't find a documentation of how to 
get them polymorphically, I mean:


class Foo
{
  int x, y;
}

Object o = new Foo();
auto ci = o.classinfo;

// what to do now to get members?

I found a solution that called getMembers() on ci object but I am 
getting compilation error when trying to call it. I would also 
like to be able to check access level of members as with 
__traits().


Also: Is there any article / tutorial on D's introspection 
capabilities. I heard it is pretty powerful and I would really 
like to try it.


Regards,
Szymon


Re: Why TypeTuple can be assigned to a variable

2013-06-12 Thread Zhenya

On Wednesday, 12 June 2013 at 10:09:07 UTC, Simen Kjaeraas wrote:
On Wed, 12 Jun 2013 11:44:19 +0200, Zhenya  
wrote:



OK,you say that TypeTuple!("foo","bar") is a cool value of type
TypeTuple!(string,string),right?


Well, yes and no, not really. It's a bit magical. In your case,
it's assigned to an auto variable, and that variable gets that 
type.
There are other ways to use a TypeTuple where it has other 
semantics,

as you write yourself.

As explained below, a TypeTuple is just a bag of template 
parameters,

and thus obeys the rules for a bag of template parameters.



This behaviour confuses me a bit.


Understandable. It's not entirely straightforward, because the
concerns of usability weigh heavier than those of consistency.



And I just don't understand why do we need TypeTuple's value
semantic
to implement std.Tuple,because AFAIK it use variadic template
parameter pack
  != TypeTuple.


The definition od std.typetuple.TypeTuple is:

template TypeTuple(T...){
alias TypeTuple = T;
}

So a TypeTuple is exactly the same as a variadic template
parameter pack.



Sorry for my english.


No need to be, your English is great.


Thank you for your explaination,I understand.



Re: Why TypeTuple can be assigned to a variable

2013-06-12 Thread Simen Kjaeraas

On Wed, 12 Jun 2013 11:44:19 +0200, Zhenya  wrote:


OK,you say that TypeTuple!("foo","bar") is a cool value of type
TypeTuple!(string,string),right?


Well, yes and no, not really. It's a bit magical. In your case,
it's assigned to an auto variable, and that variable gets that type.
There are other ways to use a TypeTuple where it has other semantics,
as you write yourself.

As explained below, a TypeTuple is just a bag of template parameters,
and thus obeys the rules for a bag of template parameters.



This behaviour confuses me a bit.


Understandable. It's not entirely straightforward, because the
concerns of usability weigh heavier than those of consistency.



And I just don't understand why do we need TypeTuple's value
semantic
to implement std.Tuple,because AFAIK it use variadic template
parameter pack
   != TypeTuple.


The definition od std.typetuple.TypeTuple is:

template TypeTuple(T...){
alias TypeTuple = T;
}

So a TypeTuple is exactly the same as a variadic template
parameter pack.



Sorry for my english.


No need to be, your English is great.

--
Simen


Re: Why TypeTuple can be assigned to a variable

2013-06-12 Thread Zhenya

On Wednesday, 12 June 2013 at 08:14:06 UTC, Simen Kjaeraas wrote:
On Wed, 12 Jun 2013 10:01:59 +0200, Zhenya  
wrote:



Hi!

I was just surprised when realized, that this code compiles 
and runs:


import std.typetuple;
import std.stdio;

void main()
{
 auto foo = TypeTuple!("foo","bar");
writeln(typeid(typeof(foo)));
 writeln(foo);
}

If I were compiler expert,I'd say that it's a bug.But I am not)
So, can anybody explain why it's work?


It is the equivalent of:

  TypeTuple!(string, string) foo;
  foo[0] = "foo";
  foo[1] = "bar";

The ability to have a tupetuple as a variable is very useful - 
if that

had not been possible one would need something akin to this:

struct Tuple(T...) {
T[0] head;
static if (T.length) {
Tuple!(T[1..$]) tail;
}
}

And to access the third element of a tuple one'd need to write:

myTuple.tail.tail.head = "foo";

Clearly this is suboptimal, so D has better ways of doing such 
things.



OK,you say that TypeTuple!("foo","bar") is a cool value of type
TypeTuple!(string,string),right?

Then could you tell me what type has [TypeTuple!("foo","bar")]?

Currently it is evaluated to string[].And I think if TypeTuple

had value semantic, this type would be TypeTuple!(...)[].

This behaviour confuses me a bit.

And I just don't understand why do we need TypeTuple's value
semantic
to implement std.Tuple,because AFAIK it use variadic template
parameter pack
  != TypeTuple.
Sorry for my english.


Re: Why TypeTuple can be assigned to a variable

2013-06-12 Thread Simen Kjaeraas

On Wed, 12 Jun 2013 10:01:59 +0200, Zhenya  wrote:


Hi!

I was just surprised when realized, that this code compiles and runs:

import std.typetuple;
import std.stdio;

void main()
{
  auto foo = TypeTuple!("foo","bar");
writeln(typeid(typeof(foo)));
  writeln(foo);
}

If I were compiler expert,I'd say that it's a bug.But I am not)
So, can anybody explain why it's work?


It is the equivalent of:

  TypeTuple!(string, string) foo;
  foo[0] = "foo";
  foo[1] = "bar";

The ability to have a tupetuple as a variable is very useful - if that
had not been possible one would need something akin to this:

struct Tuple(T...) {
T[0] head;
static if (T.length) {
Tuple!(T[1..$]) tail;
}
}

And to access the third element of a tuple one'd need to write:

myTuple.tail.tail.head = "foo";

Clearly this is suboptimal, so D has better ways of doing such things.

--
Simen


Why TypeTuple can be assigned to a variable

2013-06-12 Thread Zhenya

Hi!

I was just surprised when realized, that this code compiles and 
runs:


import std.typetuple;
import std.stdio;

void main()
{
 auto foo = TypeTuple!("foo","bar");
writeln(typeid(typeof(foo)));
 writeln(foo);
}

If I were compiler expert,I'd say that it's a bug.But I am not)
So, can anybody explain why it's work?

Thank you.