Named tuple from struct

2011-01-08 Thread Guilherme Vieira
Is is possible to get a named tuple from a struct type?

E.g.:

struct S { int foo; string bar; }

S s;

S.tupleof t; // S.tupleof is a tuple type, as opposed to s.tupleof,
 // which yields a tuple instance

t[0] = 1;
t.bar = 2;


If not, I think it would be quite useful.

Even still, a way to describe tuple types as if it was a struct would also
be useful:

tuple StructLike
{
int foo;
string bar;
}

StructLike t;

t[0] = 1;
t.bar = 2;


-- 
Atenciosamente / Sincerely,
Guilherme (n2liquid) Vieira


Re: auto declarations

2011-01-08 Thread bearophile
Ellery Newcomer:

 int a = 1, *b = null;

Walter has disallowed code like this in D because in C it is a well known 
source of bugs (so much that C style guides strongly suggest to declare only 
each variable in a distinct statement and line of code).


 auto a = 1, b = null;

I have discussed this with other people some time ago. At first I don't like 
this, it's against the D rule of not allowing different types to be initialized 
in the same statement. It may cause some bugs.

Example: if you write a line of code like, meaning it to initialize six double 
variables both you have a bug:
auto x1=1., x2=2., x3=3., x4=4., x5=5, x6=6.;

But you are playing with fire. Better to be safer and write:
double x1=1., x2=2., x3=3., x4=4., x5=5, x6=6.;

Or even:
double x1, x2, x3, x4, x5, x6;
x1 = 1.0;
x2 = 2.0;
x3 = 3.0;
x4 = 4.0;
x5 = 5.0;
x6 = 6.0;

Or:
double[6] x;
foreach (i, ref xi; x)
xi = i + 1;

Or:
double[6] x = array(iota(1.0, 7.0));


 The first is accepted by dmd, and it should result in typeof(a) == int 
 and typeof(b) == void*. It is somewhat contradictory to the error 
 message resulting from the second:
 
 multiple declarations must have the same type, not int and int*

Error messages in DMD are a draft :-) I have opened many bug reports that 
suggest to improve them.

Here the error message is not fully correct, but I think it's acceptable. To 
improve it the D compiler may also give a number of the error, and a manual of 
the errors may explain that indeed the auto is allowed to instantiate 
different types, etc. I don't know why DMD errors are not numbered ad in C#.

Bye,
bearophile


Re: Named tuple from struct

2011-01-08 Thread Jacob Carlborg

On 2011-01-08 09:15, Guilherme Vieira wrote:

Is is possible to get a named tuple from a struct type?

E.g.:

struct S { int foo; string bar; }

S s;

S.tupleof t; // S.tupleof is a tuple type, as opposed to s.tupleof,
  // which yields a tuple instance

t[0] = 1;
t.bar = 2;


If not, I think it would be quite useful.


You cannot get only the names as a tuple but you can implement something 
similar:


1. Loop through the tuple
2. Get the name of a field in the struct using the stringof property 
on the tuple

3. Slice of the name of the struct and some other unwanted characters

You can have a look at this method as well: 
http://dsource.org/projects/dstep/browser/dstep/objc/bridge/ClassInitializer.d#L84



Even still, a way to describe tuple types as if it was a struct would
also be useful:

tuple StructLike
{
 int foo;
 string bar;
}

StructLike t;

t[0] = 1;
t.bar = 2;


--
Atenciosamente / Sincerely,
Guilherme (n2liquid) Vieira



--
/Jacob Carlborg


Calling anonymous delegate recursively ?

2011-01-08 Thread tsukikage

eg. to return a fibonacci delegate:

return (ulong m) {
if(m  2) return m ;
return _self_ref(m-1)+_self_ref(m-2) ;
} ;

Is it possible? Thank you!


Re: Calling anonymous delegate recursively ?

2011-01-08 Thread Pelle

On 01/08/2011 04:45 PM, tsukikage wrote:

eg. to return a fibonacci delegate:

return (ulong m) {
if(m  2) return m ;
return _self_ref(m-1)+_self_ref(m-2) ;
} ;

Is it possible? Thank you!


http://en.wikipedia.org/wiki/Fixed_point_combinator#Y_combinator

I don't think there's a built in way to self-recurse.


Re: Calling anonymous delegate recursively ?

2011-01-08 Thread Andrej Mitrovic
As a workaround you can do this for now:

import std.stdio;

enum deleg = returnFib();
ulong delegate(ulong m) returnFib()
{
return (ulong m)
{
if(m  2)
return m;
return deleg(m-1)+deleg(m-2);
};
}

void main()
{
writeln(returnFib()(10));
}

Otherwise I'd really like the ability for a lambda to call itself.
Perhaps a feature request is in order.


Re: Calling anonymous delegate recursively ?

2011-01-08 Thread Stewart Gordon

On 08/01/2011 16:00, Pelle wrote:
snip

http://en.wikipedia.org/wiki/Fixed_point_combinator#Y_combinator

snip

How are you getting around D not supporting recursively defined types?

Stewart.


Re: Calling anonymous delegate recursively ?

2011-01-08 Thread Stewart Gordon

On 08/01/2011 17:40, Andrej Mitrovic wrote:
snip

Otherwise I'd really like the ability for a lambda to call itself.
Perhaps a feature request is in order.


I'm not sure what D would gain in practice.  If you want a function that 
calls itself, why not just name the function?


Stewart.


Re: Calling anonymous delegate recursively ?

2011-01-08 Thread Andrej Mitrovic
On 1/8/11, Stewart Gordon smjg_1...@yahoo.com wrote:
 On 08/01/2011 17:40, Andrej Mitrovic wrote:
 snip
 Otherwise I'd really like the ability for a lambda to call itself.
 Perhaps a feature request is in order.

 I'm not sure what D would gain in practice.  If you want a function that
 calls itself, why not just name the function?

 Stewart.


I don't know. Perhaps it would be useful in generics..


Re: Calling anonymous delegate recursively ?

2011-01-08 Thread tsukikage

Thank Pelle , and others.

I'm thinking ways to do this task :
http://rosettacode.org/wiki/Anonymous_recursion

With this last version of Y-combinator
http://rosettacode.org/wiki/Y_combinator#D ,
it look like this:
ulong fib(long n) {
  if(n  0) throw new Exception(No negative) ;
  return Y((ulong delegate(ulong) self) {
return (ulong m) {
  return (m = 1) ? m : self(m-1) + self(m-2) ;
} ;
  })(n) ;
}
and works. Only that this Y-combinator seems induced a lot of 
overhead(four return statements in the Y's definition).


From D document, if i not misunderstood, a delegate has 2 property .ptr 
(frame pointer) and .funcptr (address of function), so the delegate 
should be a structure? Would there be a hack to get access to this 
structure? It seems not now.


Thanks again.

Pelle wrote:

On 01/08/2011 04:45 PM, tsukikage wrote:

eg. to return a fibonacci delegate:

return (ulong m) {
if(m  2) return m ;
return _self_ref(m-1)+_self_ref(m-2) ;
} ;

Is it possible? Thank you!


http://en.wikipedia.org/wiki/Fixed_point_combinator#Y_combinator

I don't think there's a built in way to self-recurse.


Re: What's wrong with this code?

2011-01-08 Thread Michal Minich
On Sat, 08 Jan 2011 20:34:39 +, Sean Eskapp wrote:

   if(left == null)

1) write if (left is null) instead if checking for null. Equality 
operator is rewritten to a.opEquals(b), which you don't want if you 
checking for null.

 this()
 {
 left = right = null;
 }
2) default constructor as you written it is not needed, fields are always 
initialized to default value of their type, which is in this case null.

 private const Fib* left, right;
3) Classes in D are reference types. It should be 
 private const Fib left, right;

4) second constructor - Classes in D are reference types,  operator is 
not needed (see point 3), parameter left and right are already references 
to class.


Re: What's wrong with this code?

2011-01-08 Thread Tomek Sowiński

Sean Eskapp napisał(a):


I had some code that was segfaulting, so I rewrote the basic idea as a
fibonacci function, and lo and behold, it still segfaults. Why, and how  
to fix?


This looks fishy:

class Fib
{
private const Fib* left, right;
...
this(in Fib left, in Fib right)
{
this.left = left;
this.right = right;
}


Are you sure you want a pointer to class? Classes have reference semantics  
in D (like Java). Structs are value types, though. Anyway, it looks like  
the ctor takes the address of the reference placed on the stack.


--
Tomek


Implicit type conversion

2011-01-08 Thread Michal Minich
Use case:

import std.variant;

void foo (Variant v) {}

void main () {
Variant v = 3; // ok, this () called
v = 3; // ok, opAssing  called
foo (v);   // ok, struct copy, this(this) called
foo (3);   // error
}

I'm trying to understand what is needed to make this work from language 
design perspective. Is there already known/proposed solution to make this 
work? What comes to my mind as first - Variant constructor should be 
called on last line, the same way as on the first. But maybe to solve 
this, another operator is needed, opImplicitConvertFrom ...

Second question. Why is needed to have both - strut constructor (this) 
and opAssing. In variant case, constructor just forwards to opAssing. 
From high level point of view, I don't see any reason both should behave 
differently...


Re: std.container.Array/RefCounted(T) leaking memory?

2011-01-08 Thread %u
 What method are you using to test the memory?
 I'm puzzled that you've put a comment there rather than the code you're 
 actually
using.

I'm not using code, I'm checking the working set of my process in Task Manager,
and through every iteration, it adds 128 MB.


 If you run this code twice, does the memory usage double?

Yes. I ran this code:
{
auto b = Array!(bool)();
b.length = 1024 * 1024 * 128 * 8;
}
{
auto b = Array!(bool)();
b.length = 1024 * 1024 * 128 * 8;
}
and Task Manager showed two increases of 128-MB.


Thank you!


druntime

2011-01-08 Thread Ellery Newcomer

where did libdruntime.a go in dmd.2.051.zip:/linux/lib ?


writef vs writeln and printing to the console

2011-01-08 Thread Andrej Mitrovic
Unfortunately I can't provide a simple test case, but I have a case where using:

writef(..\n);

inside a loop that runs a dozen times does not print out each line as the 
statement is reached, instead it prints out everything at once when the 
application is done. If I use this:

writeln(.);

then I get each line printed out at the exact moment as this statement is 
reached. Is this normal behavior? 


Re: What's wrong with this code? (OP)

2011-01-08 Thread Sean Eskapp
Tomek got it right. Fixed by copying the objects, rather than using pointers. 
Thanks!


Re: druntime

2011-01-08 Thread Jonathan M Davis
On Saturday 08 January 2011 13:32:19 Ellery Newcomer wrote:
 where did libdruntime.a go in dmd.2.051.zip:/linux/lib ?

I think that it's included inside of libphobos.a now, and has been for a few 
releases. The libraries are still separate, and you can build them separately, 
but from what I can tell, it looks like phobos ends up including the druntime 
stuff in itself when you build it. So, having a separate libdruntime.a would be 
kind of pointless unless you were specifically trying to avoid linking in any 
Phobos stuff, which would be highly abnormal, and in which case you could build 
druntime yourself.

But Sean or someone else who's been involved in that build process may have a 
better answer as to what exactly is going on now and why.

- Jonathan M Davis


Re: druntime

2011-01-08 Thread Ellery Newcomer

On 01/08/2011 09:02 PM, Jonathan M Davis wrote:

On Saturday 08 January 2011 13:32:19 Ellery Newcomer wrote:

where did libdruntime.a go in dmd.2.051.zip:/linux/lib ?


I think that it's included inside of libphobos.a now, and has been for a few
releases. The libraries are still separate, and you can build them separately,
but from what I can tell, it looks like phobos ends up including the druntime
stuff in itself when you build it. So, having a separate libdruntime.a would be
kind of pointless unless you were specifically trying to avoid linking in any
Phobos stuff, which would be highly abnormal, and in which case you could build
druntime yourself.


Figures, but I thought I looked in libphobos and didn't see druntime 
symbols.. oh well.


And yeah, it's been a few releases since I've needed to build my own rpms


Re: druntime

2011-01-08 Thread Jonathan M Davis
On Saturday 08 January 2011 19:16:26 Ellery Newcomer wrote:
 On 01/08/2011 09:02 PM, Jonathan M Davis wrote:
  On Saturday 08 January 2011 13:32:19 Ellery Newcomer wrote:
  where did libdruntime.a go in dmd.2.051.zip:/linux/lib ?
  
  I think that it's included inside of libphobos.a now, and has been for a
  few releases. The libraries are still separate, and you can build them
  separately, but from what I can tell, it looks like phobos ends up
  including the druntime stuff in itself when you build it. So, having a
  separate libdruntime.a would be kind of pointless unless you were
  specifically trying to avoid linking in any Phobos stuff, which would be
  highly abnormal, and in which case you could build druntime yourself.
 
 Figures, but I thought I looked in libphobos and didn't see druntime
 symbols.. oh well.
 
 And yeah, it's been a few releases since I've needed to build my own rpms

Well, I haven't actually gone so far as to look at the symbols in the library, 
but there is no libdruntime.a - only a libphobos.a - and it works. And if you 
build phobos, you still need to build druntime first. So, really, the only way 
that it could really work from what I can see is that libphobos.a includes the 
stuff from libdruntime.a in it. Why or how, I don't know.

- Jonathan M Davis


Re: interface function overloading

2011-01-08 Thread %u
== Quote from Jonathan M Davis (jmdavisp...@gmx.com)'s article
 On Saturday 08 January 2011 22:01:11 %u wrote:
  Isn't it possible to have a hierarchy in interface definitions such that it
  is possible to overload according to best interface match?
 
  This now won't compile due to multiple matches.
 
  
  module main;
 
  interface I1{}
  interface I2 : I1{}
 
  class C : I2{
this(){}
  }
 
  void func(I1 i){}
  void func(I2 i){}
 
  void main(){
func(new C());
  }
  
 Very little - if anything - in the way of overloading in D works by best
 match. It pretty much always has to be exact or implicitly convertible and be
 the _only_ option which is implicitly convertible. Sometimes, it can be pretty
 annoying, but it's done to avoid cases where you accidentally call one 
 function
 when you mean another, like you can get in C++ fairly easily.
 Take a look at http://www.digitalmars.com/d/2.0/hijack.html
 - Jonathan M Davis

I see, cast(ugly:) seems to work.

  func(cast(I2)(new C()));

Thanks.