Re: is this a poly Polymorphism?

2011-08-29 Thread Michel Fortin

On 2011-08-29 01:58:30 +, Jonathan M Davis jmdavisp...@gmx.com said:


On Monday, August 29, 2011 04:50:09 hhaammaadd wrote:

import std.stdio;
class employee {
void work() {
writeln(I am employee);
}
}

class manager: employee {
void work() {
writeln(I am manager);
}
}


void main() {
employee e1 = new manager;//here
e1.work();

manager m3 = new manager;//--|here
employee *e2 = m3;//|
e2.work();


}


Pointers are not polymorphic in D. Class references are, but if you have a
pointer to a class, it assumes that the type is exactly that type and
determines its function calls at compile time rather than at runtime.


Actually, a pointer to a class is really a pointer to a reference to an 
object, since the reference is always with a class type. So employee 
* is in reality a pointer to a reference to an employee object.


--
Michel Fortin
michel.for...@michelf.com
http://michelf.com/



Re: d2 file input performance

2011-08-29 Thread Christian Köstlin

On 8/26/11 23:56 , bearophile wrote:

Steven Schveighoffer:


In fact, it would probably be faster.


I suggest the OP to keep us updated on this matter. And later after some time, 
if no solutions are found, to bring the issue to the main D newsgroup and to 
Bugzilla too. This is a significant issue.

Bye,
bearophile

Small update:
I added some more example implementations as a reaction to Mehrdad's 
suggestion to make sure to use the same file-read api. So the c++ and 
the d version both load libc dynamically and from that the symbol fread.

respective times from c++ and d: 115ms vs. 504ms.

the only thing i could also try is to use ldc or gdc (but i first have 
to install those).



regards
christian



Re: Memory leak with BufferedFile?

2011-08-29 Thread Christian Köstlin

On 8/28/11 1:00 , Leon wrote:

I'm writing a simple program which uses merge sort to sort very large text
files. I split the text file into several temporary files which are sorted and
merged together.

The trouble I'm having is that the BufferedFile class seems to have a memory
leak. The memory usage of the process slowly goes up until it throws an out of
memory exception. I can fix the memory leak by switching to the File class,
but that makes the program much slower.

Calling GC.collect() didn't help, and neither did explicitly deleting the
BufferedFile object.


Thats very interesting!
Perhaps you can have a look at: https://github.com/gizmomogwai/performance
There I ran into performanceproblems using BufferedFile.
One of the best solutions I found till now is using mmfile as in 
https://github.com/gizmomogwai/performance/blob/master/src/d/readbytes5.d. 
Perhaps this could be of help for you.


I dont know how you have to access the data in the files, but perhaps 
the interface of mmfile is good enough for you!



christian


Re: implicit casting from primitive type

2011-08-29 Thread Ali Çehreli
On Mon, 29 Aug 2011 21:33:13 +0200, Mariusz Gliwiński wrote:

 Hello,
 this will be easy question. I defined attributes, that are taking my
 custom structures as attributes. Then, I'd like to add implicit
 conversion to this structures from primitive types, such as: code
 some.attribute = [1, 2, 3];   // i'd like to do that some.attribute =
 MyStruct(1, 2, 3); // now it's like that /code
 opCast can be used only to casting FROM my type, but not TO... Was that
 in a book?
 
 Thanks,
 Mariusz Gliwiński

To solve it for this specific case, you can overload attribute() to take 
int[]:

import std.exception;

struct MyStruct
{
int i;
int j;
int k;
}

struct CustomStruct
{
MyStruct ms;

@property void attribute(int[] args)
{
enforce(args.length == 3);
ms = MyStruct(args[0], args[1], args[2]);
}
}

void main()
{
auto some = CustomStruct();
some.attribute = [ 1, 2, 3 ];
}

Ali


Re: implicit casting from primitive type

2011-08-29 Thread Mariusz Gliwiński
Ali Çehreli wrote:
 To solve it for this specific case, you can overload attribute() to take
 int[]:

I know, but i don't really want to. Too many overrides or templates, but 
i'll consider it if wont find any better solution.

Thanks,
Mariusz Gliwiński


struct opEquals does not work with parameter of same type - bug or feature?

2011-08-29 Thread Sean Eskapp
I am trying to build a struct with equality testing, using this code:

struct Foo
{
const bool opEquals(Foo f)
{
return true;
}
}

This gives me the error that the parameter should be of type ref const Foo.
Fine.

struct Foo
{
const bool opEquals(ref const Foo f)
{
return true;
}
}

This, however, does not work with code like:

Foo bar()
{
return Foo();
}

assert(Foo() == bar());

function Foo.opEquals(ref const const(Foo) f) const is not callable using
argument types (Foo) and bar() is not an lvalue.

How can I do this?


How do I pass multidimensional static arrays to functions expecting dynamic arrays?

2011-08-29 Thread Andrej Mitrovic
Take a look:

void main()
{
int[2] single;
// foo(single);  // no
foo(single[]);   // int[2][] slice, ok

int[2][2] multi;
// bar(multi); // int[2][2] no
// bar(multi[]);   // int[2][] slice, no
// bar(multi[][]); // int[2][] slice, no
}

void foo(int[] value) {}
void bar(int[][] value) {}

I can easily slice a one-dimensional static array, but I can only
slice a single dimension. So how do I pass a multidimensional static
array to a function expecting a multidimensional dynamic array?


Re: How do I pass multidimensional static arrays to functions expecting dynamic arrays?

2011-08-29 Thread Timon Gehr

On 08/30/2011 01:29 AM, Andrej Mitrovic wrote:

Take a look:

void main()
{
 int[2] single;
 // foo(single);  // no
 foo(single[]);   // int[2][] slice, ok

 int[2][2] multi;
 // bar(multi); // int[2][2] no
 // bar(multi[]);   // int[2][] slice, no
 // bar(multi[][]); // int[2][] slice, no
}

void foo(int[] value) {}
void bar(int[][] value) {}

I can easily slice a one-dimensional static array, but I can only
slice a single dimension. So how do I pass a multidimensional static
array to a function expecting a multidimensional dynamic array?


D does not have multidimensional dynamic arrays.

To solve your problem, you have to manually create an array of 1D-slices:

Like this:

bar(array(map!((int[] a){return a;})(multi[])));

Or like this:

int[][] arg=new int[][](2);
foreach(i,ref x;arg) x=multi[i][];
bar(arg);







Re: struct opEquals does not work with parameter of same type - bug or feature?

2011-08-29 Thread Jonathan M Davis
On Monday, August 29, 2011 22:41:26 Sean Eskapp wrote:
 I am trying to build a struct with equality testing, using this code:
 
 struct Foo
 {
 const bool opEquals(Foo f)
 {
 return true;
 }
 }
 
 This gives me the error that the parameter should be of type ref const
 Foo. Fine.
 
 struct Foo
 {
 const bool opEquals(ref const Foo f)
 {
 return true;
 }
 }
 
 This, however, does not work with code like:
 
 Foo bar()
 {
 return Foo();
 }
 
 assert(Foo() == bar());
 
 function Foo.opEquals(ref const const(Foo) f) const is not callable using
 argument types (Foo) and bar() is not an lvalue.
 
 How can I do this?

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

http://stackoverflow.com/questions/6986175/const-ref-and-rvalue-in-d

- Jonathan M Davis


Re: struct opEquals does not work with parameter of same type - bug or feature?

2011-08-29 Thread Sean Eskapp
== Quote from Jonathan M Davis (jmdavisp...@gmx.com)'s article
 On Monday, August 29, 2011 22:41:26 Sean Eskapp wrote:
  I am trying to build a struct with equality testing, using this code:
 
  struct Foo
  {
  const bool opEquals(Foo f)
  {
  return true;
  }
  }
 
  This gives me the error that the parameter should be of type ref const
  Foo. Fine.
 
  struct Foo
  {
  const bool opEquals(ref const Foo f)
  {
  return true;
  }
  }
 
  This, however, does not work with code like:
 
  Foo bar()
  {
  return Foo();
  }
 
  assert(Foo() == bar());
 
  function Foo.opEquals(ref const const(Foo) f) const is not callable using
  argument types (Foo) and bar() is not an lvalue.
 
  How can I do this?
 http://d.puremagic.com/issues/show_bug.cgi?id=3659
 http://stackoverflow.com/questions/6986175/const-ref-and-rvalue-in-d
 - Jonathan M Davis

Ah, thanks!


Re: How do I pass multidimensional static arrays to functions expecting

2011-08-29 Thread bearophile
Timon Gehr:

 bar(array(map!((int[] a){return a;})(multi[])));

Simpler:
bar( array(map!q{ a[] }(multi[])) );

Simpler still when we'll get amap:
bar( amap!q{ a[] }(multi[]) );

Bye,
bearophile


Re: How do I pass multidimensional static arrays to functions expecting dynamic arrays?

2011-08-29 Thread Andrej Mitrovic
Right, but I was just trying to temporarily avoid GC allocation so
I've used a static array instead of a dynamic ones. Also, I don't know
of another term that is used to describe a int[][] array, other than
multidimensional.

Anyway this works fine for me (int wasn't a requirement in this case):

void foo(T)(T val) if (isArray!T)
void bar(T)(T val) if (isArray!T  isArray!(ElementType!T))


Re: How do I pass multidimensional static arrays to functions expecting dynamic arrays?

2011-08-29 Thread Andrej Mitrovic
Hey btw, do you think we could use this in Phobos?

template BaseElementType(R)
{
static if (isArray!(ElementType!R))
alias BaseElementType!(ElementType!R) BaseElementType;
else static if (is(typeof({return R.init.front();}()) T))
alias T BaseElementType;
else
alias void BaseElementType;
}

assert(is(BaseElementType!(int[][]) == int));

Maybe a better name is RootElementType. Not too sure.


Re: How do I pass multidimensional static arrays to functions expecting dynamic arrays?

2011-08-29 Thread Jonathan M Davis
On Tuesday, August 30, 2011 03:43:39 Andrej Mitrovic wrote:
 Right, but I was just trying to temporarily avoid GC allocation so
 I've used a static array instead of a dynamic ones. Also, I don't know
 of another term that is used to describe a int[][] array, other than
 multidimensional.

It _is_ multi-dimensional. I'm not quite sure why Timon is saying that it 
isn't. Ultimately though, a multi-dimensional dynamic array is an array of 
arrays (or array of arrays of arrays of ...). When slicing it, you get the a 
portion of the outermost array. If you index that, you can get at the inner 
arrays to slice those if you want to, but the inner arrays know nothing about 
the outer arrays, and actually taking a slice of the whole where you get the 
outer array and some portion of the inner arrays would require creating a new 
array, so you can't really do it.

I can't really think of a clean way of explaining it without diagrams, and 
even then it's a bit of a pain, but with a slice, it's only a matter of 
adjusting its ptr and length properties. If you want a multi-dimensional 
slice, you'd need to adjust the ptr and length properties of the arrays that 
the slice contained, and you can't do that without affecting the original 
arrays unless you copy them. So, ultimately, you need to construct a new 
multi-dimensional array with the pieces that you want if you want a multi-
dimensional slice.

- Jonathan M Davis