Re: Classical bug

2015-01-27 Thread Fyodor Ustinov via Digitalmars-d-learn
On Tuesday, 27 January 2015 at 12:02:59 UTC, Vladimir Panteleev 
wrote:


Always. But the check seems very simple, and is easily 
circumvented. This compiles:


byte[] func() {
 byte[1024] buffer;
 auto p = buffer[0..3];
 return p;
}


I think this is the first step of a long and difficult way.

byte[] func(byte[] a) {
 return a[0..1];
}

byte[] func1() {
 byte[1024] buffer;
 return func(buffer[1...$]);
}

byte[] func2() {
 static byte[1024] buffer;
 return func(buffer[1..$]);
}



Re: Virtual functions and inheritance

2015-01-27 Thread Daniel Kozák via Digitalmars-d-learn
V Tue, 27 Jan 2015 04:38:57 +
David Monagle via Digitalmars-d-learn
digitalmars-d-learn@puremagic.com napsáno:

 Hi guys,
 
 I'm a former C++ developer and really enjoying working with D 
 now. I have a question that I hope some of you may be able to 
 answer.
 
 class Parent {
@property string typeName() {
  return typeof(this).stringof;
}
 }
 
 class Child : Parent {
 }
 
 void main() {
auto p = new Parent;
auto c = new Child;
assert(p.typeName == Parent);
assert(p.typeName == Child);
 }
 
 
 I'm looking for an explanation as to why this doesn't work, then 
 a suggestion for how I may achieve child classes being able to 
 generate a string description of their own type, without 
 redefining the typeName property on each child. (I'm currently 
 solving this with a mixin, but I was hoping for a better solution.
 
 I'm assuming it doesn't work because either typeof(this) or 
 .stringof is evaluated at compile time?

You can use this T:

class Parent {
@property string typeName(this T)() {
return T.stringof;
}
}

class Child : Parent {
}

void main() {
auto p = new Parent;
auto c = new Child;

assert(p.typeName == Parent);
assert(c.typeName == Child);
}



Re: static class vs. static struct

2015-01-27 Thread Ali Çehreli via Digitalmars-d-learn

On 01/27/2015 08:58 AM, Piotrek wrote:

Nice list. :)

 1. static variable

 struct A{int a} // no static before declaration
 static A s; //note that static is used for struct variable storage class
 (lifetime)

 static int b;
 etc.

 2. static declaration

 static struct A{int a}; //static used for context unnesting
 static int fun(){}; // static used also for removing scope context

Of course that includes static member functions, where the 'this' 
pointer is removed.


Actually, static opCall is kind of different because it makes the type 
itself callable.



 etc.

 3. static if

 static if(compile_time_cond)
 {
//this section of code will be taken into the binary, used for meta
 programming
 }

Another use of 'static' that means at compile time:

static assert

4. Module initialization and deinitialization:

static this
shared static this

static ~this
shared static ~this

5. Module import:

static import std.stdio;

Ali



Re: I left my program open for 9 hours and it used up 700mb of ram, could someone review it?

2015-01-27 Thread Gan via Digitalmars-d-learn

On Tuesday, 27 January 2015 at 15:45:47 UTC, bearophile wrote:

Gan:


How can I make it use less CPU/RAM?


Most tiny classes probably should be structs. More generally, 
use a struct every time you don't need a class.


You can start with those two:

struct SBRange {
double left = 0.0, right = 0.0, top = 0.0, bottom = 0.0;
}

struct Point(T) {
T x, y;
}

This probably isn't enough to solve your problems, but it's a 
start.


Bye,
bearophile


Is there some special stuff I gotta do extra with structs? Do 
they need manually allocated and released?


On a second question, do I ever need to manually release objects 
I create with new?


Re: I left my program open for 9 hours and it used up 700mb of ram, could someone review it?

2015-01-27 Thread bearophile via Digitalmars-d-learn

Gan:

Is there some special stuff I gotta do extra with structs? Do 
they need manually allocated and released?


Most of your usages of tiny structs should be by value. So just 
keep in mind they are values. Even when you iterate with a 
foreach on a mutable array of them :-)



On a second question, do I ever need to manually release 
objects I create with new?


Usually not. How much advanced do you want to be? :-)

Bye,
bearophile


Re: I left my program open for 9 hours and it used up 700mb of ram, could someone review it?

2015-01-27 Thread Gan via Digitalmars-d-learn

On Tuesday, 27 January 2015 at 19:26:12 UTC, bearophile wrote:

Gan:

Is there some special stuff I gotta do extra with structs? Do 
they need manually allocated and released?


Most of your usages of tiny structs should be by value. So just 
keep in mind they are values. Even when you iterate with a 
foreach on a mutable array of them :-)



On a second question, do I ever need to manually release 
objects I create with new?


Usually not. How much advanced do you want to be? :-)

Bye,
bearophile


Thanks. I'll give structs a try.

When I start the program, it runs fine at 35mb of ram. It only 
keeps 15 objects stored in the arrays at a time so why do you 
think my ram usage increases to 700+ after many hours?


Re: static class vs. static struct

2015-01-27 Thread Ali Çehreli via Digitalmars-d-learn

On 01/27/2015 08:33 AM, Piotrek wrote:

 Non-static means nested.

 Hmm,this can be misleading. Nesting in structs doesn't introduce context
 pointer.

You must be thinking of structs nested inside user-defined types. 
Structs that are nested inside functions do have the context pointer.


Ali



Re: Virtual functions and inheritance

2015-01-27 Thread via Digitalmars-d-learn

On Tuesday, 27 January 2015 at 08:19:46 UTC, Daniel Kozák wrote:

You can use this T:

class Parent {
@property string typeName(this T)() {
return T.stringof;
}
}

class Child : Parent {
}

void main() {
auto p = new Parent;
auto c = new Child;

assert(p.typeName == Parent);
assert(c.typeName == Child);
}


OTOH:

void main() {
auto p = new Parent;
auto c = new Child;

assert(p.typeName == Parent);
assert(c.typeName == Child);

p = c;
assert(p.typeName == Parent);
}

It will still use the static type of the object that it's called 
on, not the dynamic type.


Re: static class vs. static struct

2015-01-27 Thread ref2401 via Digitalmars-d-learn
For several times I've met struct(or static struct) usage in 
Phobos for singleton pattern implementation. Unfortunately now i 
can remember only core.runtime.Runtime.
So I've got a question. Why do Phobos guys use struct or static 
struct for or singleton pattern implementation? Why don't use 
static final class for this purpose?


Re: Array List object?

2015-01-27 Thread bearophile via Digitalmars-d-learn

Gan:


//Initializing the array
tiles = new SBTile[](0);


This is often useless.



//Clearing the array
tiles = [];


This doesn't clear the array, it rebinds it to a null pointer.

Bye,
bearophile


Re: static class vs. static struct

2015-01-27 Thread Daniel Kozak via Digitalmars-d-learn

On Tuesday, 27 January 2015 at 09:01:39 UTC, ref2401 wrote:
For several times I've met struct(or static struct) usage in 
Phobos for singleton pattern implementation. Unfortunately now 
i can remember only core.runtime.Runtime.
So I've got a question. Why do Phobos guys use struct or static 
struct for or singleton pattern implementation? Why don't use 
static final class for this purpose?


I do not think this is a singleton pattern (no instance). I see 
it much more like namespace in case of core.runtime.Runtime. And 
yes static final class could do that too but struct looks better 
than final class and you can disable this on structs


Re: Using dub

2015-01-27 Thread Joel via Digitalmars-d-learn

Oope, yeah, and it ran.


Re: static class vs. static struct

2015-01-27 Thread ketmar via Digitalmars-d-learn
On Tue, 27 Jan 2015 09:40:08 +, Daniel Kozak wrote:

 import std.stdio;
 import std.conv;
 
 struct S {
  @disable this();
 }
 
 final class C {
 }
 
 void main() {
  writeln(C.sizeof);
  writeln(S.sizeof);
 }

blind guess: vmt with toString() from Object? ;-)

signature.asc
Description: PGP signature


Re: Using dub

2015-01-27 Thread Joel via Digitalmars-d-learn

On Tuesday, 27 January 2015 at 07:44:12 UTC, Rikki Cattermole
wrote:

On 27/01/2015 8:40 p.m., Joel wrote:

On Tuesday, 27 January 2015 at 07:25:18 UTC, Rikki Cattermole
wrote:

On 27/01/2015 8:03 p.m., Joel wrote:
I'm having trouble using dub. Nothing seems to work (-h 
works though). I
would like an example or two of how to get an app going 
(stand alone for

now). I'm using the Mac OS.


Lets use Devisualization.Window as an example.
Assuming in a safe directory and dmd/dub on PATH variable:

$ git clone https://github.com/Devisualization/window.git
$ cd window
$ dub build de_window:test
$ ./de_window_test

For simple test app:

$ mkdir myapp
$ cd myapp
$ dub init
$ nano source/app.d
// edit
// ctrl + x, y

$ dub build
$ ./myapp
// or
$ dub run


I get this (dub isn't in the path):

Joels-MacBook-Pro:window joelcnz$ ../dub build de_window:test
Failed to parse package description in
/Users/joelcnz/.dub/packages/dil-master
Failed to load package in
/Users/joelcnz/.dub/packages/dil-master: Got 
.excludedSourceFiles

of type string - expected array.
Failed to parse package description in
/Users/joelcnz/.dub/packages/dplug-0.0.2
Failed to load package in
/Users/joelcnz/.dub/packages/dplug-0.0.2: Expected version 
number

in version spec: *
Failed to parse package description in
/Users/joelcnz/.dub/packages/gfm-1.1.6
Failed to load package in 
/Users/joelcnz/.dub/packages/gfm-1.1.6:

Expected version number in version spec: *
Failed to parse package description in
/Users/joelcnz/.dub/packages/gfm-1.3.3
Failed to load package in 
/Users/joelcnz/.dub/packages/gfm-1.3.3:

Expected version number in version spec: *
Failed to parse package description in
/Users/joelcnz/.dub/packages/dil-master
Failed to load package in
/Users/joelcnz/.dub/packages/dil-master: Got 
.excludedSourceFiles

of type string - expected array.
Failed to parse package description in
/Users/joelcnz/.dub/packages/dplug-0.0.2
Failed to load package in
/Users/joelcnz/.dub/packages/dplug-0.0.2: Expected version 
number

in version spec: *
Failed to parse package description in
/Users/joelcnz/.dub/packages/gfm-1.1.6
Failed to load package in 
/Users/joelcnz/.dub/packages/gfm-1.1.6:

Expected version number in version spec: *
Failed to parse package description in
/Users/joelcnz/.dub/packages/gfm-1.3.3
Failed to load package in 
/Users/joelcnz/.dub/packages/gfm-1.3.3:

Expected version number in version spec: *
Failed to parse package description in
/Users/joelcnz/jpro/dpro2/OtherPeoples/window
Failed to parse package description in
/Users/joelcnz/.dub/packages/dil-master
Failed to load package in
/Users/joelcnz/.dub/packages/dil-master: Got 
.excludedSourceFiles

of type string - expected array.
Failed to parse package description in
/Users/joelcnz/.dub/packages/dplug-0.0.2
Failed to load package in
/Users/joelcnz/.dub/packages/dplug-0.0.2: Expected version 
number

in version spec: *
Failed to parse package description in
/Users/joelcnz/.dub/packages/gfm-1.1.6
Failed to load package in 
/Users/joelcnz/.dub/packages/gfm-1.1.6:

Expected version number in version spec: *
Failed to parse package description in
/Users/joelcnz/.dub/packages/gfm-1.3.3
Failed to load package in 
/Users/joelcnz/.dub/packages/gfm-1.3.3:

Expected version number in version spec: *
Failed to parse package description in
/Users/joelcnz/.dub/packages/dil-master
Failed to load package in
/Users/joelcnz/.dub/packages/dil-master: Got 
.excludedSourceFiles

of type string - expected array.
Failed to parse package description in
/Users/joelcnz/.dub/packages/dplug-0.0.2
Failed to load package in
/Users/joelcnz/.dub/packages/dplug-0.0.2: Expected version 
number

in version spec: *
Failed to parse package description in
/Users/joelcnz/.dub/packages/gfm-1.1.6
Failed to load package in 
/Users/joelcnz/.dub/packages/gfm-1.1.6:

Expected version number in version spec: *
Failed to parse package description in
/Users/joelcnz/.dub/packages/gfm-1.3.3
Failed to load package in 
/Users/joelcnz/.dub/packages/gfm-1.3.3:

Expected version number in version spec: *
Failed to parse package description in
/Users/joelcnz/jpro/dpro2/OtherPeoples/window
Error executing command build: Expected version number in 
version

spec: *


Try again after doing:
$ rm -rf ~/.dub

Something seems ugh, weird.
If that doesn't, than its time for dub bug reporting.


I've tried an earlier version of dub, similar problem. It was
better another time I tried it.

I got this now (I didn't do any thing to the dub file):
Joels-MacBook-Pro:window joelcnz$ ../../dub build de_window:test
Failed to parse package description for dil  in
/Users/joelcnz/.dub/packages/dil-master/.
Failed to load package in
/Users/joelcnz/.dub/packages/dil-master/: Got
.excludedSourceFiles of type string - expected array.
Failed to parse package description for dil  in
/Users/joelcnz/.dub/packages/dil-master/.
Failed to load package in
/Users/joelcnz/.dub/packages/dil-master/: Got
.excludedSourceFiles of type string - expected array.
Building 

Re: core.exception.InvalidMemoryOperationError@(0)

2015-01-27 Thread ketmar via Digitalmars-d-learn
On Tue, 27 Jan 2015 06:46:20 +, Bayan Rafeh wrote:

 This is the first serious project I do with D

and now you're lost to other C-like languages, methinks. ;-)

signature.asc
Description: PGP signature


Re: static class vs. static struct

2015-01-27 Thread Daniel Kozak via Digitalmars-d-learn

On Tuesday, 27 January 2015 at 09:36:49 UTC, Daniel Kozak wrote:

On Tuesday, 27 January 2015 at 09:01:39 UTC, ref2401 wrote:
For several times I've met struct(or static struct) usage in 
Phobos for singleton pattern implementation. Unfortunately now 
i can remember only core.runtime.Runtime.
So I've got a question. Why do Phobos guys use struct or 
static struct for or singleton pattern implementation? Why 
don't use static final class for this purpose?


I do not think this is a singleton pattern (no instance). I see 
it much more like namespace in case of core.runtime.Runtime. 
And yes static final class could do that too but struct looks 
better than final class and you can disable this on structs


import std.stdio;
import std.conv;

struct S
{
@disable this();
}

final class C
{
}

void main() {
writeln(C.sizeof);
writeln(S.sizeof);
}


Re: Array List object?

2015-01-27 Thread bearophile via Digitalmars-d-learn
And it's named dynamic array, instead of Array List object, 
it's not a class instance.


Bye,
bearophile


Re: About variant

2015-01-27 Thread bioinfornatics via Digitalmars-d-learn

I can do this
import std.variant;

struct Alpha {
Variant something;

this(Variant v){
something = v;
}

static Alpha build(T)(T v){
return Alpha( cast(Variant)v );
}

}

void main(){
auto a = Alpha.build!(int)( 6);
auto b = Alpha.build!(string)( hello);
auto l = new Alpha[](2);
l[0] = a;
l[1] = b;
}

If someone has better


Re: About variant

2015-01-27 Thread bioinfornatics via Digitalmars-d-learn

On Tuesday, 27 January 2015 at 21:00:16 UTC, Justin Whear wrote:

On Tue, 27 Jan 2015 20:46:59 +, bioinfornatics wrote:


void main(){
auto a = Alpha!(int)( 6);
auto b = Alpha!(string)( hello);


The Alpha struct is not a template, only the constructor is.  
Remove the

explicit instantiations and IFTI does the work:

void main(){
auto a = Alpha( 6);
auto b = Alpha( hello);


Oh really cool


Re: I left my program open for 9 hours and it used up 700mb of ram, could someone review it?

2015-01-27 Thread Gan via Digitalmars-d-learn

On Tuesday, 27 January 2015 at 19:59:08 UTC, Gan wrote:

On Tuesday, 27 January 2015 at 19:26:12 UTC, bearophile wrote:

Gan:

Is there some special stuff I gotta do extra with structs? Do 
they need manually allocated and released?


Most of your usages of tiny structs should be by value. So 
just keep in mind they are values. Even when you iterate with 
a foreach on a mutable array of them :-)



On a second question, do I ever need to manually release 
objects I create with new?


Usually not. How much advanced do you want to be? :-)

Bye,
bearophile


Thanks. I'll give structs a try.

When I start the program, it runs fine at 35mb of ram. It only 
keeps 15 objects stored in the arrays at a time so why do you 
think my ram usage increases to 700+ after many hours?


Curiously, my CPU usage went from 10% to 5% after I changed to 
structs on Point and Range. Though my memory still climbs high.


Re: About variant

2015-01-27 Thread Justin Whear via Digitalmars-d-learn
On Tue, 27 Jan 2015 20:46:59 +, bioinfornatics wrote:

 void main(){
   auto a = Alpha!(int)( 6);
   auto b = Alpha!(string)( hello);

The Alpha struct is not a template, only the constructor is.  Remove the 
explicit instantiations and IFTI does the work:
 void main(){
   auto a = Alpha( 6);
   auto b = Alpha( hello);



Re: static class vs. static struct

2015-01-27 Thread Piotrek via Digitalmars-d-learn

On Tuesday, 27 January 2015 at 18:24:29 UTC, Ali Çehreli wrote:

On 01/27/2015 08:33 AM, Piotrek wrote:

 Non-static means nested.

 Hmm,this can be misleading. Nesting in structs doesn't
introduce context
 pointer.

You must be thinking of structs nested inside user-defined 
types. Structs that are nested inside functions do have the 
context pointer.


Ali


What you wrote about the structs is true. However I was referring 
to other thing. I just wanted to emphasize (with my poor English) 
that also classes and structs *nested in struct* doesn't contain 
the additional context pointer. As opposed to class nested in 
class.


Then I think we'd better not say that non-static means nested.

Piotrek


Print to Win Printer

2015-01-27 Thread Paul via Digitalmars-d-learn

How do I print to a Windows printer from a console program?
Thanks for your assistance.


Re: static class vs. static struct

2015-01-27 Thread Piotrek via Digitalmars-d-learn

On Tuesday, 27 January 2015 at 18:18:02 UTC, Ali Çehreli wrote:

On 01/27/2015 08:58 AM, Piotrek wrote:

Nice list. :)

 1. static variable

 struct A{int a} // no static before declaration
 static A s; //note that static is used for struct variable
storage class
 (lifetime)

 static int b;
 etc.

 2. static declaration

 static struct A{int a}; //static used for context unnesting
 static int fun(){}; // static used also for removing scope
context

Of course that includes static member functions, where the 
'this' pointer is removed.


Actually, static opCall is kind of different because it makes 
the type itself callable.



 etc.

 3. static if

 static if(compile_time_cond)
 {
//this section of code will be taken into the binary, used
for meta
 programming
 }

Another use of 'static' that means at compile time:

static assert

4. Module initialization and deinitialization:

static this
shared static this

static ~this
shared static ~this

5. Module import:

static import std.stdio;

Ali


Thanks for comments, Mr. Professor. On duty as usual ;)
Let me here thank for your book which I've been reading for some 
time.


Piotrek


About variant

2015-01-27 Thread bioinfornatics via Digitalmars-d-learn
Dear that do a lot time wehere I not used std.variant. i would 
like to hide extra cast from user by using a generic ctor


import std.variant;

struct Alpha {
Variant something;

this(T)(T v){
something = cast(Variant)v;
}

}

void main(){
auto a = Alpha!(int)( 6);
auto b = Alpha!(string)( hello);
auto l = new Alpha[](2);
l[0] = a;
l[1] = b;
}

but that do not works.

Someone know a trick?

thanks


Re: I left my program open for 9 hours and it used up 700mb of ram, could someone review it?

2015-01-27 Thread Rikki Cattermole via Digitalmars-d-learn

On 28/01/2015 9:59 a.m., Gan wrote:

On Tuesday, 27 January 2015 at 19:59:08 UTC, Gan wrote:

On Tuesday, 27 January 2015 at 19:26:12 UTC, bearophile wrote:

Gan:


Is there some special stuff I gotta do extra with structs? Do they
need manually allocated and released?


Most of your usages of tiny structs should be by value. So just keep
in mind they are values. Even when you iterate with a foreach on a
mutable array of them :-)



On a second question, do I ever need to manually release objects I
create with new?


Usually not. How much advanced do you want to be? :-)

Bye,
bearophile


Thanks. I'll give structs a try.

When I start the program, it runs fine at 35mb of ram. It only keeps
15 objects stored in the arrays at a time so why do you think my ram
usage increases to 700+ after many hours?


Curiously, my CPU usage went from 10% to 5% after I changed to structs
on Point and Range. Though my memory still climbs high.


Force a GC.collect() now and again. Disable it at the beginning too.


Re: I left my program open for 9 hours and it used up 700mb of ram, could someone review it?

2015-01-27 Thread Rikki Cattermole via Digitalmars-d-learn

On 28/01/2015 11:30 a.m., Gan wrote:

On Tuesday, 27 January 2015 at 21:36:51 UTC, Rikki Cattermole wrote:

On 28/01/2015 9:59 a.m., Gan wrote:

On Tuesday, 27 January 2015 at 19:59:08 UTC, Gan wrote:

On Tuesday, 27 January 2015 at 19:26:12 UTC, bearophile wrote:

Gan:


Is there some special stuff I gotta do extra with structs? Do they
need manually allocated and released?


Most of your usages of tiny structs should be by value. So just keep
in mind they are values. Even when you iterate with a foreach on a
mutable array of them :-)



On a second question, do I ever need to manually release objects I
create with new?


Usually not. How much advanced do you want to be? :-)

Bye,
bearophile


Thanks. I'll give structs a try.

When I start the program, it runs fine at 35mb of ram. It only keeps
15 objects stored in the arrays at a time so why do you think my ram
usage increases to 700+ after many hours?


Curiously, my CPU usage went from 10% to 5% after I changed to structs
on Point and Range. Though my memory still climbs high.


Force a GC.collect() now and again. Disable it at the beginning too.


I did a test and ran GC.collect() every loop but my memory usage
continues to rise. I can see how you'd be able to lower CPU usage by
running GC.collect() every now and then but right now I'm stuck on the
memory issue.

Perhaps my problem lies in the C++ library SFML?


I had a quick look at your code.
I think its safe to assume the SFML isn't the problem.
Whats happening is on every iteration of the event loop, you are 
allocating, holding a reference somewhere and continuing on.

The GC even if it does run cannot release that memory as it is still held.

At this point maybe strip out what happens on each loop iteration to 
find out what is holding references. Divide and conquer.


Re: I left my program open for 9 hours and it used up 700mb of ram, could someone review it?

2015-01-27 Thread Gan via Digitalmars-d-learn

On Tuesday, 27 January 2015 at 22:30:13 UTC, Gan wrote:
On Tuesday, 27 January 2015 at 21:36:51 UTC, Rikki Cattermole 
wrote:

On 28/01/2015 9:59 a.m., Gan wrote:

On Tuesday, 27 January 2015 at 19:59:08 UTC, Gan wrote:
On Tuesday, 27 January 2015 at 19:26:12 UTC, bearophile 
wrote:

Gan:

Is there some special stuff I gotta do extra with structs? 
Do they

need manually allocated and released?


Most of your usages of tiny structs should be by value. So 
just keep
in mind they are values. Even when you iterate with a 
foreach on a

mutable array of them :-)


On a second question, do I ever need to manually release 
objects I

create with new?


Usually not. How much advanced do you want to be? :-)

Bye,
bearophile


Thanks. I'll give structs a try.

When I start the program, it runs fine at 35mb of ram. It 
only keeps
15 objects stored in the arrays at a time so why do you 
think my ram

usage increases to 700+ after many hours?


Curiously, my CPU usage went from 10% to 5% after I changed 
to structs

on Point and Range. Though my memory still climbs high.


Force a GC.collect() now and again. Disable it at the 
beginning too.


I did a test and ran GC.collect() every loop but my memory 
usage continues to rise. I can see how you'd be able to lower 
CPU usage by running GC.collect() every now and then but right 
now I'm stuck on the memory issue.


Perhaps my problem lies in the C++ library SFML?


I commented out some stuff and it appears my massive memory 
consumption comes from my tile.redraw function:


void redraw() {
undrawn = false;
canvas.clear();

//Add stars
for (int i = 0; i  controller.starsPerTile; i++) {
Random gen;
gen.seed(unpredictableSeed);
int x = uniform(0, controller.tileSize, gen);
int y = uniform(0, controller.tileSize, gen);
double s = uniform(0, 1, gen) / 1.0;
			double size = (s * (controller.starSizeMax - 
controller.starSizeMin)) + controller.starSizeMin;

if (x - size / 2  0) {
x += size / 2;
}
if (y - size / 2  0) {
y += size / 2;
}
if (x + size / 2  controller.tileSize) {
x -= size / 2;
}
if (y + size / 2  controller.tileSize) {
y -= size / 2;
}
drawStar(canvas, x, y, size, size);
}

canvas.display();
}
	void drawStar(RenderTarget target, int centerX, int centerY, 
double width, double height) {

CircleShape star;
if (controller.multiColor == false) {
star = controller.stars[0];
} else {
Random gen;
gen.seed(unpredictableSeed);
			star = controller.stars[uniform(0, controller.stars.length - 
1, gen)];

}
star.position = Vector2f(centerX, centerY);
star.origin = Vector2f(0.5, 0.5);
star.scale = Vector2f(width / 100.0, height / 100.0);
target.draw(star);
}


Would you know why this is using hundreds of mb of rams?


Re: vibe.d error

2015-01-27 Thread Phil via Digitalmars-d-learn

Bumping as it's still not possible to install lib event via dub.

On Sunday, 25 January 2015 at 17:41:38 UTC, Phil wrote:

dub init name vibe.d
cd name
dub

Results in

Fetching libevent 2.0.1+2.0.16...
Error executing command upgrade: Failed to download 
http://code.dlang.org/packages/libevent/2.0.1%252B2.0.16.zip: 
404 Not Found


I'm not sure if the error is the file not being there or dub 
looking there. Is there a workaround for this? Thanks


Re: About variant

2015-01-27 Thread ketmar via Digitalmars-d-learn
On Tue, 27 Jan 2015 21:55:37 +, bioinfornatics wrote:

 On Tuesday, 27 January 2015 at 21:00:16 UTC, Justin Whear wrote:
 On Tue, 27 Jan 2015 20:46:59 +, bioinfornatics wrote:

 void main(){
 auto a = Alpha!(int)( 6);
 auto b = Alpha!(string)( hello);

 The Alpha struct is not a template, only the constructor is.
 Remove the explicit instantiations and IFTI does the work:
 void main(){
 auto a = Alpha( 6);
 auto b = Alpha( hello);
 
 Oh really cool

or this:

  import std.variant;

  struct Alpha {
Variant something;

this(T) (T v) {
  something = cast(Variant)v;
}
  }

  void main () {
Alpha a = 6;
Alpha b = hello;
  }


signature.asc
Description: PGP signature


Re: static class vs. static struct

2015-01-27 Thread Ali Çehreli via Digitalmars-d-learn

On 01/27/2015 01:33 PM, Piotrek wrote:

 On Tuesday, 27 January 2015 at 18:24:29 UTC, Ali Çehreli wrote:
 On 01/27/2015 08:33 AM, Piotrek wrote:

  Non-static means nested.
 
  Hmm,this can be misleading. Nesting in structs doesn't
 introduce context
  pointer.

Oh, I misread what you wrote. Sorry...

 classes and structs *nested in struct* doesn't contain the additional
 context pointer. As opposed to class nested in class.

 Then I think we'd better not say that non-static means nested.

 Piotrek

Makes sense.

Ali



Re: Using dub

2015-01-27 Thread Joel via Digitalmars-d-learn

On Tuesday, 27 January 2015 at 08:08:19 UTC, Joel wrote:

Oope, yeah, and it ran.


Thanks Rikki, I wiped off the dub installation. Now, no errors. 
The small program worked too.


I don't now how to set up the dub executable to work with out 
doing stuff like this - '../dub' (Mac OS 10.10.1)


Joels-MacBook-Pro:window joelcnz$ ../../dub build de_window:test
Building package de_window:test in 
/Users/joelcnz/jpro/dpro2/OtherPeoples/window/

Fetching de_util 0.0.4 (getting selected version)...
Placing de_util 0.0.4 to /Users/joelcnz/.dub/packages/...
Fetching x11 1.0.5 (getting selected version)...
Placing x11 1.0.5 to /Users/joelcnz/.dub/packages/...
Fetching de_image 0.3.4 (getting selected version)...
Placing de_image 0.3.4 to /Users/joelcnz/.dub/packages/...
Fetching derelict-util 1.9.0 (getting selected version)...
Placing derelict-util 1.9.0 to /Users/joelcnz/.dub/packages/...
Fetching derelict-gl3 1.0.12 (getting selected version)...
Placing derelict-gl3 1.0.12 to /Users/joelcnz/.dub/packages/...
Building de_util:core 0.0.4 configuration library, build type 
debug.

Running dmd...
Building de_image:interfaces 0.3.4 configuration library, build 
type debug.

Running dmd...
Building de_image:mutable 0.3.4 configuration library, build 
type debug.

Running dmd...
Building x11 1.0.5 configuration library, build type debug.
Running dmd...
Building de_window:interfaces 0.0.8 configuration library, 
build type debug.

Running dmd...
Building derelict-util 1.9.0 configuration library, build type 
debug.

Running dmd...
Building derelict-gl3 1.0.12 configuration library, build type 
debug.

Running dmd...
Building de_window:test 0.0.8 configuration application, build 
type debug.

Compiling using dmd...
Linking...
Joels-MacBook-Pro:window joelcnz$ ls
LICENSE dub.json
README.md   dub.selections.json
WindowsAPI  interfaces
cocoa_library   libde_window_interfaces.a
de_window_test  platforms
dub test


Re: I left my program open for 9 hours and it used up 700mb of ram, could someone review it?

2015-01-27 Thread Vladimir Panteleev via Digitalmars-d-learn

On Tuesday, 27 January 2015 at 22:39:31 UTC, Gan wrote:

Would you know why this is using hundreds of mb of rams?


Hi,

What type is CircleShape?

If it is a class, or otherwise contains pointers, then this is 
probably the source of your problem.


You are storing high-entropy data (floating-point / random 
numbers) within the same type as one containing pointers. This is 
problematic with a non-precise GC, because the GC will consider 
the random numbers as possibly pointers, thus pinning random 
objects within the memory address space.


You should be able to work around this problem by:

- Making CircleShape a static struct without any pointers
- If you need to have pointers associated with CircleShape: 
splitting pointers and non-pointers into separate arrays of 
structs
- Manually managing memory allocations, and storing the 
CircleShape instances outside the managed D heap
- Building your program for x86_64 - 64 bits of address space 
will make fake pointer pinning very unlikely
- Using a precise GC - none are currently available for D, but 
one is under development: 
https://github.com/D-Programming-Language/druntime/pull/1057


Re: I left my program open for 9 hours and it used up 700mb of ram, could someone review it?

2015-01-27 Thread FG via Digitalmars-d-learn

On 2015-01-27 at 23:39, Gan wrote:

I commented out some stuff and it appears my massive memory consumption comes 
from my tile.redraw function:
...
Would you know why this is using hundreds of mb of rams?


Looks OK, so probably it is not the cause by itself.
I would add a piece of code to SpaceBackground.draw that for every few calls to 
this function would print out tiles.length and stack.length to the console. 
Just to make sure that those arrays don't grow out of control, before accusing 
anything else.


Re: Using dub

2015-01-27 Thread Joel via Digitalmars-d-learn

On Wednesday, 28 January 2015 at 00:34:13 UTC, Joel wrote:

On Tuesday, 27 January 2015 at 08:08:19 UTC, Joel wrote:

Oope, yeah, and it ran.


Thanks Rikki, I wiped off the dub installation. Now, no errors. 
The small program worked too.


Actually I got this with dlangui, (I followed the instructions on 
the announce post):


Joels-MacBook-Pro:dlangui joelcnz$ ../../dub run dlangui:example1
Failed to parse package description in 
/Users/joelcnz/jpro/dpro2/OtherPeoples/dlangui
Failed to parse package description in 
/Users/joelcnz/jpro/dpro2/OtherPeoples/dlangui
Error executing command run: Expected version number in version 
spec: *


Re: Print to Win Printer

2015-01-27 Thread Vladimir Panteleev via Digitalmars-d-learn

On Tuesday, 27 January 2015 at 21:32:34 UTC, Paul wrote:

How do I print to a Windows printer from a console program?
Thanks for your assistance.


You can save your document to a temporary file, then call 
ShellExecute with a path to the file and the print command.


Re: Cached Incremental Updates of DUB Builds

2015-01-27 Thread Nordlöw

On Monday, 26 January 2015 at 22:33:21 UTC, Nordlöw wrote:
Is there a way to tell DUB to compile all its sources as 
separate objects which are then fed to ld.


And I don't want DMD to recompile every line in my project if I 
change only of them...and yes most of my cross-module 
interfaces are un-templatized.


Ping. Why no answers? Should i post this as a DUB issue on github 
instead?


Re: Cached Incremental Updates of DUB Builds

2015-01-27 Thread data man via Digitalmars-d-learn

Try it: https://github.com/gecko0307/Cook2

On Monday, 26 January 2015 at 22:33:21 UTC, Nordlöw wrote:
Is there a way to tell DUB to compile all its sources as 
separate objects which are then fed to ld.


My project has grown beyound 10k lines of code.

And I don't want DMD to recompile every line in my project if I 
change only of them...and yes most of my cross-module 
interfaces are un-templatized.


I'm assuming caching a la SCons is supported by this logic 
aswell.


If not are there any plans to add this feature to DUB?

If not could somebody point out where I should add it ;)




Re: I left my program open for 9 hours and it used up 700mb of ram, could someone review it?

2015-01-27 Thread Rikki Cattermole via Digitalmars-d-learn

On 28/01/2015 11:39 a.m., Gan wrote:

On Tuesday, 27 January 2015 at 22:30:13 UTC, Gan wrote:

On Tuesday, 27 January 2015 at 21:36:51 UTC, Rikki Cattermole wrote:

On 28/01/2015 9:59 a.m., Gan wrote:

On Tuesday, 27 January 2015 at 19:59:08 UTC, Gan wrote:

On Tuesday, 27 January 2015 at 19:26:12 UTC, bearophile wrote:

Gan:


Is there some special stuff I gotta do extra with structs? Do they
need manually allocated and released?


Most of your usages of tiny structs should be by value. So just keep
in mind they are values. Even when you iterate with a foreach on a
mutable array of them :-)



On a second question, do I ever need to manually release objects I
create with new?


Usually not. How much advanced do you want to be? :-)

Bye,
bearophile


Thanks. I'll give structs a try.

When I start the program, it runs fine at 35mb of ram. It only keeps
15 objects stored in the arrays at a time so why do you think my ram
usage increases to 700+ after many hours?


Curiously, my CPU usage went from 10% to 5% after I changed to structs
on Point and Range. Though my memory still climbs high.


Force a GC.collect() now and again. Disable it at the beginning too.


I did a test and ran GC.collect() every loop but my memory usage
continues to rise. I can see how you'd be able to lower CPU usage by
running GC.collect() every now and then but right now I'm stuck on the
memory issue.

Perhaps my problem lies in the C++ library SFML?


I commented out some stuff and it appears my massive memory consumption
comes from my tile.redraw function:

void redraw() {
 undrawn = false;
 canvas.clear();

 //Add stars
 for (int i = 0; i  controller.starsPerTile; i++) {
 Random gen;
 gen.seed(unpredictableSeed);
 int x = uniform(0, controller.tileSize, gen);
 int y = uniform(0, controller.tileSize, gen);
 double s = uniform(0, 1, gen) / 1.0;
 double size = (s * (controller.starSizeMax -
controller.starSizeMin)) + controller.starSizeMin;
 if (x - size / 2  0) {
 x += size / 2;
 }
 if (y - size / 2  0) {
 y += size / 2;
 }
 if (x + size / 2  controller.tileSize) {
 x -= size / 2;
 }
 if (y + size / 2  controller.tileSize) {
 y -= size / 2;
 }
 drawStar(canvas, x, y, size, size);
 }

 canvas.display();
 }
 void drawStar(RenderTarget target, int centerX, int centerY, double
width, double height) {
 CircleShape star;
 if (controller.multiColor == false) {
 star = controller.stars[0];
 } else {
 Random gen;
 gen.seed(unpredictableSeed);
 star = controller.stars[uniform(0, controller.stars.length
- 1, gen)];
 }
 star.position = Vector2f(centerX, centerY);
 star.origin = Vector2f(0.5, 0.5);
 star.scale = Vector2f(width / 100.0, height / 100.0);
 target.draw(star);
 }


Would you know why this is using hundreds of mb of rams?


Try with only drawStart stripped.
If it still does it, then its redraw. Also try with just a hard coded 
call to drawStart.

Something smells wrong here.


Re: I left my program open for 9 hours and it used up 700mb of ram, could someone review it?

2015-01-27 Thread Gan via Digitalmars-d-learn
On Tuesday, 27 January 2015 at 21:36:51 UTC, Rikki Cattermole 
wrote:

On 28/01/2015 9:59 a.m., Gan wrote:

On Tuesday, 27 January 2015 at 19:59:08 UTC, Gan wrote:

On Tuesday, 27 January 2015 at 19:26:12 UTC, bearophile wrote:

Gan:

Is there some special stuff I gotta do extra with structs? 
Do they

need manually allocated and released?


Most of your usages of tiny structs should be by value. So 
just keep
in mind they are values. Even when you iterate with a 
foreach on a

mutable array of them :-)


On a second question, do I ever need to manually release 
objects I

create with new?


Usually not. How much advanced do you want to be? :-)

Bye,
bearophile


Thanks. I'll give structs a try.

When I start the program, it runs fine at 35mb of ram. It 
only keeps
15 objects stored in the arrays at a time so why do you think 
my ram

usage increases to 700+ after many hours?


Curiously, my CPU usage went from 10% to 5% after I changed to 
structs

on Point and Range. Though my memory still climbs high.


Force a GC.collect() now and again. Disable it at the beginning 
too.


I did a test and ran GC.collect() every loop but my memory usage 
continues to rise. I can see how you'd be able to lower CPU usage 
by running GC.collect() every now and then but right now I'm 
stuck on the memory issue.


Perhaps my problem lies in the C++ library SFML?


Re: I left my program open for 9 hours and it used up 700mb of ram, could someone review it?

2015-01-27 Thread Gan via Digitalmars-d-learn
On Tuesday, 27 January 2015 at 22:42:25 UTC, Rikki Cattermole 
wrote:

On 28/01/2015 11:39 a.m., Gan wrote:

On Tuesday, 27 January 2015 at 22:30:13 UTC, Gan wrote:
On Tuesday, 27 January 2015 at 21:36:51 UTC, Rikki Cattermole 
wrote:

On 28/01/2015 9:59 a.m., Gan wrote:

On Tuesday, 27 January 2015 at 19:59:08 UTC, Gan wrote:
On Tuesday, 27 January 2015 at 19:26:12 UTC, bearophile 
wrote:

Gan:

Is there some special stuff I gotta do extra with 
structs? Do they

need manually allocated and released?


Most of your usages of tiny structs should be by value. 
So just keep
in mind they are values. Even when you iterate with a 
foreach on a

mutable array of them :-)


On a second question, do I ever need to manually release 
objects I

create with new?


Usually not. How much advanced do you want to be? :-)

Bye,
bearophile


Thanks. I'll give structs a try.

When I start the program, it runs fine at 35mb of ram. It 
only keeps
15 objects stored in the arrays at a time so why do you 
think my ram

usage increases to 700+ after many hours?


Curiously, my CPU usage went from 10% to 5% after I changed 
to structs

on Point and Range. Though my memory still climbs high.


Force a GC.collect() now and again. Disable it at the 
beginning too.


I did a test and ran GC.collect() every loop but my memory 
usage
continues to rise. I can see how you'd be able to lower CPU 
usage by
running GC.collect() every now and then but right now I'm 
stuck on the

memory issue.

Perhaps my problem lies in the C++ library SFML?


I commented out some stuff and it appears my massive memory 
consumption

comes from my tile.redraw function:

void redraw() {
undrawn = false;
canvas.clear();

//Add stars
for (int i = 0; i  controller.starsPerTile; i++) {
Random gen;
gen.seed(unpredictableSeed);
int x = uniform(0, controller.tileSize, gen);
int y = uniform(0, controller.tileSize, gen);
double s = uniform(0, 1, gen) / 1.0;
double size = (s * (controller.starSizeMax -
controller.starSizeMin)) + controller.starSizeMin;
if (x - size / 2  0) {
x += size / 2;
}
if (y - size / 2  0) {
y += size / 2;
}
if (x + size / 2  controller.tileSize) {
x -= size / 2;
}
if (y + size / 2  controller.tileSize) {
y -= size / 2;
}
drawStar(canvas, x, y, size, size);
}

canvas.display();
}
void drawStar(RenderTarget target, int centerX, int 
centerY, double

width, double height) {
CircleShape star;
if (controller.multiColor == false) {
star = controller.stars[0];
} else {
Random gen;
gen.seed(unpredictableSeed);
star = controller.stars[uniform(0, 
controller.stars.length

- 1, gen)];
}
star.position = Vector2f(centerX, centerY);
star.origin = Vector2f(0.5, 0.5);
star.scale = Vector2f(width / 100.0, height / 100.0);
target.draw(star);
}


Would you know why this is using hundreds of mb of rams?


Try with only drawStart stripped.
If it still does it, then its redraw. Also try with just a hard 
coded call to drawStart.

Something smells wrong here.


Without drawStar, my program starts at 24mb and slowly increases. 
With drawStar my program starts at 70mb and increases at twice 
the rate.


Re: static class vs. static struct

2015-01-27 Thread Ali Çehreli via Digitalmars-d-learn

On 01/27/2015 01:44 PM, Piotrek wrote:

 Let me here thank for your book

I am glad that it is useful.

 which I've been reading for some time.

Me too! I browsed the index section to remember the other uses of 
'static'. :)


Ali



Re: Virtual functions and inheritance

2015-01-27 Thread bearophile via Digitalmars-d-learn

Baz:


doesn't work. And similarly to the the orginal post:


I suggest to read some D documentation first, and program later.

Bye,
bearophile


Classical bug

2015-01-27 Thread Fyodor Ustinov via Digitalmars-d-learn

Hi!

I thought at least in safe mode this code will not compile or I 
get warning:


byte[] func() @safe {
 byte[1024] buffer;
 return buffer[0..3];
}

void main() {
  auto b = func();
  b[0] = 1;
}

But no any error. Dlang do not catch this?

WBR,
Fyodor.


Re: Virtual functions and inheritance

2015-01-27 Thread Baz via Digitalmars-d-learn

On Tuesday, 27 January 2015 at 08:19:46 UTC, Daniel Kozák wrote:

You can use this T:

class Parent {
@property string typeName(this T)() {
return T.stringof;
}
}

class Child : Parent {
}

void main() {
auto p = new Parent;
auto c = new Child;

assert(p.typeName == Parent);
assert(c.typeName == Child);
}


Could 'this T' be used for a static constructor ?

-
class Bar
{
static T construct(this T, A...)(A a)
{
return new T(a);
}
}


doesn't work. And similarly to the the orginal post:

-
class Bar
{
static typeof(this) construct(A...)(A a)
{
return new typeof(this)(a);
}
}

class Foo: Bar{}
Foo foo= Foo.construct; // fail



construct() won't be redefined in the Bar descendants.


Re: Classical bug

2015-01-27 Thread Vladimir Panteleev via Digitalmars-d-learn

On Tuesday, 27 January 2015 at 11:41:21 UTC, Fyodor Ustinov wrote:

byte[] func() @safe {
 byte[1024] buffer;
 return buffer[0..3];
}

void main() {
  auto b = func();
  b[0] = 1;
}


In 2.067, this is an error:

test.d(4,9): Error: escaping reference to local variable buffer


Re: Classical bug

2015-01-27 Thread Fyodor Ustinov via Digitalmars-d-learn
On Tuesday, 27 January 2015 at 11:51:43 UTC, Vladimir Panteleev 
wrote:

In 2.067, this is an error:

test.d(4,9): Error: escaping reference to local variable buffer


Always or only in safe mode?


Re: Classical bug

2015-01-27 Thread Vladimir Panteleev via Digitalmars-d-learn

On Tuesday, 27 January 2015 at 12:01:11 UTC, Fyodor Ustinov wrote:
On Tuesday, 27 January 2015 at 11:51:43 UTC, Vladimir Panteleev 
wrote:

In 2.067, this is an error:

test.d(4,9): Error: escaping reference to local variable buffer


Always or only in safe mode?


Always. But the check seems very simple, and is easily 
circumvented. This compiles:


byte[] func() {
 byte[1024] buffer;
 auto p = buffer[0..3];
 return p;
}


Re: Virtual functions and inheritance

2015-01-27 Thread Tobias Pankrath via Digitalmars-d-learn

-
class Bar
{
static T construct(this T, A...)(A a)
{
return new T(a);
}
}


I think it's a bug that you can use a template this parameter 
(this T) on a static member function without a direct compilation 
error.



-
class Bar
{
static typeof(this) construct(A...)(A a)
{
return new typeof(this)(a);
}
}



Because it's exactly the same to write it as

static Bar construct(A...)(A a) { return new Bar(a); }

Of course this does not work. I don't know how to do it without 
one line of boilerplate per class or without supplying the type 
via template argument.


std.container.util : make takes the type to construct as an 
argument, but std.container do not form a class hierarchy.


Re: I left my program open for 9 hours and it used up 700mb of ram, could someone review it?

2015-01-27 Thread FG via Digitalmars-d-learn

On 2015-01-28 at 03:04, Vladimir Panteleev wrote:

What type is CircleShape?
If it is a class, or otherwise contains pointers, then this is probably the 
source of your problem.


class CircleShape : Shape is defined in dsfml.graphics.circleshape, so there's 
no going around this...


- Building your program for x86_64 - 64 bits of address space will make fake 
pointer pinning very unlikely


The binary included in the zip was 64-bit, so fake pointers shouldn't be that 
much of a problem.

Oh, and I take back what I said about suspecting that SpaceBackground.stack 
grows infinitely. It probably doesn't. I don't have DSFML installed, and 
therefore couldn't recreate the behaviour.


Re: vibe.d error

2015-01-27 Thread Phil via Digitalmars-d-learn
I was using 0.9.21. Upgrading has fixed this issue, thanks to you 
both.


Re: why there is no TBase in thrift for dlang?

2015-01-27 Thread zhmt via Digitalmars-d-learn

sorry , I am quite new to dlang.


Re: why there is no TBase in thrift for dlang?

2015-01-27 Thread zhmt via Digitalmars-d-learn

I resovled it by Generic programming :

private const (ubyte)[] serialObj(T) (T obj)
{
TMemoryBuffer trans = new TMemoryBuffer();
auto prot = new TCompactProtocol!TMemoryBuffer(trans);
obj.write(prot);
return trans.getContents();
}


Re: I left my program open for 9 hours and it used up 700mb of ram, could someone review it?

2015-01-27 Thread Gan via Digitalmars-d-learn

On Wednesday, 28 January 2015 at 02:50:11 UTC, FG wrote:

On 2015-01-28 at 03:04, Vladimir Panteleev wrote:

What type is CircleShape?
If it is a class, or otherwise contains pointers, then this is 
probably the source of your problem.


class CircleShape : Shape is defined in 
dsfml.graphics.circleshape, so there's no going around this...


- Building your program for x86_64 - 64 bits of address space 
will make fake pointer pinning very unlikely


The binary included in the zip was 64-bit, so fake pointers 
shouldn't be that much of a problem.


Oh, and I take back what I said about suspecting that 
SpaceBackground.stack grows infinitely. It probably doesn't. I 
don't have DSFML installed, and therefore couldn't recreate the 
behaviour.


Is there a way to set a variable to be cleared in the new GC 
collection or to forcibly release a variable?


I have an inkling that it might be the Sprite class or something 
with the RenderTextures.


Re: vibe.d error

2015-01-27 Thread Suliman via Digitalmars-d-learn
Try to update dub. There was issue in old version with path with 
whitespace.


Re: static class vs. static struct

2015-01-27 Thread Artur Skawina via Digitalmars-d-learn
On 01/27/15 10:40, Daniel Kozak via Digitalmars-d-learn wrote:
 On Tuesday, 27 January 2015 at 09:36:49 UTC, Daniel Kozak wrote:
 On Tuesday, 27 January 2015 at 09:01:39 UTC, ref2401 wrote:
 For several times I've met struct(or static struct) usage in Phobos for 
 singleton pattern implementation. Unfortunately now i can remember only 
 core.runtime.Runtime.
 So I've got a question. Why do Phobos guys use struct or static struct for 
 or singleton pattern implementation? Why don't use static final class for 
 this purpose?

 I do not think this is a singleton pattern (no instance). I see it much more 
 like namespace in case of core.runtime.Runtime. And yes static final class 
 could do that too but struct looks better than final class and you can 
 disable this on structs
 
 import std.stdio;
 import std.conv;
 
 struct S
 {
 @disable this();
 }
 
 final class C
 {
 }
 
 void main() {
 writeln(C.sizeof);
 writeln(S.sizeof);
 }

D's `class` magically adds a level of indirection, so C.sizeof
gives you just the size of the _reference_.

For the true (ie instance/payload) size you'd have to use 

   __traits(classInstanceSize, C)

artur


Re: static class vs. static struct

2015-01-27 Thread Piotrek via Digitalmars-d-learn

On Tuesday, 27 January 2015 at 09:01:39 UTC, ref2401 wrote:
For several times I've met struct(or static struct) usage in 
Phobos for singleton pattern implementation. Unfortunately now 
i can remember only core.runtime.Runtime.
So I've got a question. Why do Phobos guys use struct or static 
struct for or singleton pattern implementation? Why don't use 
static final class for this purpose?


You probably saw static member function.

Please take the following with a big grain of salt as I took it 
out of my head:

We can divide the D static keyword usage into 3 types:

1. static variable

struct A{int a} // no static before declaration
static A s; //note that static is used for struct variable 
storage class (lifetime)


static int b;
etc.

2. static declaration

static struct A{int a}; //static used for context unnesting
static int fun(){}; // static used also for removing scope context

etc.

3. static if

static if(compile_time_cond)
{
  //this section of code will be taken into the binary, used for 
meta programming

}

I don't think there is much (if any) use of static (type 1) for 
singleton.


Piotrek


Re: Array List object?

2015-01-27 Thread H. S. Teoh via Digitalmars-d-learn
On Tue, Jan 27, 2015 at 07:18:22AM +, Gan via Digitalmars-d-learn wrote:
[...]
 Okay now I'm very confused. When I have my program fully hidden behind
 another window, my ram usage goes up without going down. Which my
 program is partly visible it goes up a few mb then returns to the past
 amount of mb.
 
 Is this a bug?

It's hard to say without more information. One possibility is that when
the program is partly visible the GC gets triggered more often, so more
memory gets returned to the OS, whereas when it's not, the GC may be
triggering less often so memory usage increases. But that's just my
guess, it's hard to say unless we know more about what your program
does.


T

-- 
People say I'm indecisive, but I'm not sure about that. -- YHL, CONLANG


Re: Classical bug

2015-01-27 Thread bearophile via Digitalmars-d-learn

Vladimir Panteleev:

But the check seems very simple, and is easily circumvented. 
This compiles:


byte[] func() {
 byte[1024] buffer;
 auto p = buffer[0..3];
 return p;
}


I guess such bugs will be detected (in safe code only!) after the 
implementation of: http://wiki.dlang.org/DIP69 Currently we are 
implementing a kind of pre-phase: http://wiki.dlang.org/DIP25


And here I have asked for @safe to become the default (Walter 
seems not against this idea):

https://d.puremagic.com/issues/show_bug.cgi?id=13838

Bye,
bearophile


I left my program open for 9 hours and it used up 700mb of ram, could someone review it?

2015-01-27 Thread Gan via Digitalmars-d-learn
I feel like the only way I can get better right now is if someone 
with more knowledge can give me some advice on the code I have 
written.


Here's the link: http://cl.ly/0s0Q1L1S3v0E

How can I make it use less CPU/RAM?
(Most code is in the Misc/SpaceBackground.d)


Re: I left my program open for 9 hours and it used up 700mb of ram, could someone review it?

2015-01-27 Thread bearophile via Digitalmars-d-learn

Gan:


How can I make it use less CPU/RAM?


Most tiny classes probably should be structs. More generally, use 
a struct every time you don't need a class.


You can start with those two:

struct SBRange {
double left = 0.0, right = 0.0, top = 0.0, bottom = 0.0;
}

struct Point(T) {
T x, y;
}

This probably isn't enough to solve your problems, but it's a 
start.


Bye,
bearophile


Re: static class vs. static struct

2015-01-27 Thread Piotrek via Digitalmars-d-learn

On Monday, 26 January 2015 at 21:55:19 UTC, anonymous wrote:

On Monday, 26 January 2015 at 21:33:10 UTC, Piotrek wrote:

On Monday, 26 January 2015 at 14:11:32 UTC, bearophile wrote:

Non-static structs/classes have an extra pointer.

Bye,
bearophile


Since when structs have an extra pointer? Maybe you are 
talking about nested structs?


Non-static means nested.


Hmm,this can be misleading. Nesting in structs doesn't introduce 
context pointer.


But I agree that if we take into account a hypothetical inferred 
static attribute for nesting in struct and the module scope 
cases, then the static and non-static classification looks the 
most suitable.


Piotrek


why there is no TBase in thrift for dlang?

2015-01-27 Thread zhmt via Digitalmars-d-learn

I am writing code below:
private Cmd deserialCmd(ubyte[] data)
{
Cmd ret;
TMemoryBuffer trans = new TMemoryBuffer(data);
auto prot = new TCompactProtocol!TMemoryBuffer(trans);
ret.read(prot);
return ret;
}

If I have diffrent Cmd, I have to repeat the code above,

TBase has methods like write(protocol) read(protocol), if it 
exists in thrift for dlang, I could write like this:


private TBase deserialCmd(ubyte[] data,TBase ret)
{
TMemoryBuffer trans = new TMemoryBuffer(data);
auto prot = new TCompactProtocol!TMemoryBuffer(trans);
ret.read(prot);
return ret;
}

But TBase doesnt exists in thrift for dlang, what can i do now?