Proper way to write benchmark code...

2016-05-22 Thread Era Scarecrow via Digitalmars-d-learn
 Is there a good article written for this? Preferably for D 
specifically...


 I notice as I'm working a bit with my challenge to make/update 
the symbol/id compressor that perhaps the GC is getting in the 
way and skewing the results. Means a number of what I've put up 
as benchmark values may wildly off. So forgive my ignorance.


 So first, compiling flags. What should be used? So far -inline 
-noboundscheck -O -release


 Flags for a C program (if it comes into play?) I only can see -o 
-c to be applicable (then link it to your program).


 How do I work around/with the GC? what code should I use for 
benchmarks?


 Currently I'm trying to use the TickDuration via Benchmark, but 
it isn't exactly an arbitrary unit of time. If benchmark is a bad 
choice, what's a good one?


 As for the GC, since it might be running or pause threads in 
order to run, how do I ensure it's stopped before I do my 
benchmarks?


Here's what I have so far...

  import core.thread : thread_joinAll;
  import core.memory;
  import std.datetime : benchmark;

  //test functions
  //actual functions slower than lambdas??
  auto f1 = (){};
  auto f2 = (){};

  int rounds = 100_000;

  GC.collect();
  //GC.reserve(1024*1024*32); //no guarantee of reserves. So 
would this help?

  thread_joinAll();   //guarentees the GC is done?
  GC.disable();   //turned off

  auto test1 = benchmark!(f1)(rounds);

  GC.collect();   //collect between tests
  thread_joinAll();   //make sure GC is done?

  auto test2 = benchmark!(f2)(rounds);
  //collect, joinall
  ...
  //optional cleanup after the fact? Or leave the program to do 
it after exiting?

  //GC.enable();
  //GC.collect();

 Is it better to have a bunch of free memory and ignore leaks? Or 
to free memory as it's going through for cases that require it?


  //compress returns memory malloc'd, compiled with DMC and C 
code.

  char *compress(cast(char*) ptr, int size);

  auto f3 = (){
compress(cast(char*) haystack.ptr, haystack.length);  
  //this with leaks?
GC.free(compress(cast(char*) haystack.ptr, haystack.length)); 
  //or this?

  };

 Is memory allocated by DMC freed properly by GC.free if I end up 
using it this way? (For all I know GC.free ignores the pointer). 
If I do a separate allocations to match what the functions and 
calls did, can I subtract it to get a cleaner set of statistics? 
Or is that line of thinking a wrong?


  auto f3_mm = (){
void *ptr = GC.malloc(1024);
GC.free(ptr);
  };

  auto test2 = benchmark!(f3, f3_mm)(rounds); //f3-f3_mm = delta?

 For the functions/lambdas passed to benchmark, is it better to 
provide all the information in the function and not have data 
stored elsewhere? Or store it all as a pure function? Does the 
overhead of the extra stack pointer make any difference?


 Is it better to collect all the tests and output the results all 
at once? Or is it okay or better to output the statistics as they 
are finished (between benchmarks and before the 
collection/thread_joinall calls)?


 What other things should I do/consider when writing basic 
benchmark code?


Re: Is there a way to make a class variable visible but constant to outsiders, but changeable (mutable) to the class itself?

2016-05-22 Thread Mike Parker via Digitalmars-d-learn

On Sunday, 22 May 2016 at 19:29:59 UTC, Meta wrote:

Const *is* necessary to prevent _myVar being written to through 
code like:


f.myVar = 4;

Of course this isn't necessary for value types, but for 
reference types.





I was referring specifically to marking the function const, not 
the return type. Marking the return type const is highly 
context-dependent. It's perfectly reasonable to return a 
non-const class reference from a getter property. As long as the 
internal reference is private, it isn't going to be overwritten 
externally without a setter property. I don't see how it could be 
considered necessary. For a pointer, sure, to prevent 
*(bar.fooVer) = Foo(10). But for class references it's only 
necessary if you don't want the returned instances members to be 
modified.


It's also useful for value types, IMO, for preventing someone 
from doing this:


f.myVar = 4;

And wondering why the code has no effect.


The compiler already gives an error message describing the 
problem:


 Error: function mod.Foo.myVar () is not callable using argument 
types (int)


How does const help here?


Re: Is there a way to clear an OutBuffer?

2016-05-22 Thread Jon Degenhardt via Digitalmars-d-learn

On Sunday, 22 May 2016 at 23:01:07 UTC, Ali Çehreli wrote:

On 05/22/2016 11:59 AM, Jon Degenhardt wrote:
Is there a way to clear an OutBuffer, but without freeing the 
internally
managed buffer? Something similar to std.array.appender.clear 
method.
Intent would be to reuse the OutBuffer, but without 
reallocating memory

for the buffer.

--Jon


Currently not possible. Enhancement request perhaps?

Looking at the implementation, setting its 'offset' member 
seems to work. Based on example from documentation:


import std.outbuffer;

void main() {
OutBuffer b = new OutBuffer();
b.writefln("a%sb", 16);
assert(b.toString() == "a16b\n");

b.offset = 0;
b.writefln("a%sb", 16);
assert(b.toString() == "a16b\n");
}

Bug report perhaps? :)

Ali


Thanks. Enhancement request: 
https://issues.dlang.org/show_bug.cgi?id=16062


Re: Is there a way to clear an OutBuffer?

2016-05-22 Thread Ali Çehreli via Digitalmars-d-learn

On 05/22/2016 11:59 AM, Jon Degenhardt wrote:

Is there a way to clear an OutBuffer, but without freeing the internally
managed buffer? Something similar to std.array.appender.clear method.
Intent would be to reuse the OutBuffer, but without reallocating memory
for the buffer.

--Jon


Currently not possible. Enhancement request perhaps?

Looking at the implementation, setting its 'offset' member seems to 
work. Based on example from documentation:


import std.outbuffer;

void main() {
OutBuffer b = new OutBuffer();
b.writefln("a%sb", 16);
assert(b.toString() == "a16b\n");

b.offset = 0;
b.writefln("a%sb", 16);
assert(b.toString() == "a16b\n");
}

Bug report perhaps? :)

Ali



Re: OpenGL with D tutorials

2016-05-22 Thread Rishub Nagpal via Digitalmars-d-learn

On Sunday, 22 May 2016 at 12:13:07 UTC, ixid wrote:
What is the best OpenGL tutorial with D to use? I've tried to 
use d-gamedev-intro and opengl-tutorials and seem to get 
errors, files that are no longer included are needed (dgl)? and 
deprecation messages.


Perhaps use a C++ tutorial : http://www.learnopengl.com/
You can attempt to do the examples in D using GFM


Re: Is there a way to make a class variable visible but constant to outsiders, but changeable (mutable) to the class itself?

2016-05-22 Thread Daniel N via Digitalmars-d-learn

On Saturday, 21 May 2016 at 17:32:47 UTC, dan wrote:
Is it possible to have a class which has a variable which can 
be seen from the outside, but which can only be modified from 
the inside?


Something like:

class C {
  int my_var = 3; // semi_const??
  void do_something() { my_var = 4; }
}



Yes, I prefer this idiom:

class C
{
  union
  {
private  int  var_rw;
public const(int) var_ro;
  }
}



Re: Is there a way to make a class variable visible but constant to outsiders, but changeable (mutable) to the class itself?

2016-05-22 Thread Meta via Digitalmars-d-learn

On Sunday, 22 May 2016 at 03:06:44 UTC, Mike Parker wrote:

On Saturday, 21 May 2016 at 19:17:00 UTC, dan wrote:

Thanks Vit, Meta, and Yuxuan for your speedy help!

So 3 pieces to put together, function, const, and @property 
(and i guess final for protection against subclasses).


Minimally, there are two pieces to this: a private member 
variable and a function.


class Foo {
  private int _myVar;
  int myVar() { return _myVar; }
}

The private is necessary because class members in D are all 
public by default. If it isn't there, then _myVar can be 
directly modified outside of the module. Be aware, though (if 
you aren't already), that private members *are* accessible 
outside of the class in the same module, e.g.:


module foo;

class Foo {
  private int _myVar;
  int myVar() { return _myVar; }
}

void printMyVar() {
import std.stdio : writeln;
auto f = new Foo;
writeln(f);
}

As for 'const' and '@property', neither is strictly a 
requirement to implement this idiom. Adding const means that 
you can call the function through const references, but if 
that's not something you want to allow for some reason, then 
don't add it.


@property right now doesn't really do anything other than allow 
for self-documenting code. Perhaps one day it will be fully 
implemented and require callers to drop the parentheses in 
calls to @property functions, but for now it doesn't do that. 
Use it as much as you want, but just understand it isn't 
necessary for the functionality you are after.


Const *is* necessary to prevent _myVar being written to through 
code like:


f.myVar = 4;

Of course this isn't necessary for value types, but for reference 
types.


It's also useful for value types, IMO, for preventing someone 
from doing this:


f.myVar = 4;

And wondering why the code has no effect.


Is there a way to clear an OutBuffer?

2016-05-22 Thread Jon Degenhardt via Digitalmars-d-learn
Is there a way to clear an OutBuffer, but without freeing the 
internally managed buffer? Something similar to 
std.array.appender.clear method. Intent would be to reuse the 
OutBuffer, but without reallocating memory for the buffer.


--Jon


Re: Game Development Using D

2016-05-22 Thread Karabuta via Digitalmars-d-learn

On Saturday, 21 May 2016 at 15:53:18 UTC, David wrote:

Hi,

I want to try to create a game using D. I'm a complete newbie 
though (other than having C/C++ experience). Where would I 
start? Does D have an openGL binding? I am assuming I'll need 
to leverage a good amount C APIs? Any list of these that would 
be useful it a game setting?


Obviously this all depends on *how* much work I want to do. 
Ideally, I'd like a collection of tools that will get me 
roughly the equivalent of what XNA provides.


If you don't know much OpenGL, go for DSFML 
https://github.com/Jebbs/DSFML


Re: Is there a 128-bit integer in D?

2016-05-22 Thread Era Scarecrow via Digitalmars-d-learn

On Sunday, 22 May 2016 at 12:03:16 UTC, Guillaume Piolat wrote:
I don't know how this works, someone has to propose it, clean 
it up and respond to feedback I guess.


 Glancing briefly at the source, it appears (from what I saw) 
it's heavily based on doubling the size of a lower type (2 64's 
to equal a 128, etc). This means to get the next level up you 
double again. So if I read it right for example, the opIncrease 
does this almost verbatim:


  opIncrease() { //aka ++
low++;
if (low == 0) //overflow detection
  high++;
  }

 Going from native up one level is easy enough, but to do a 256 
type it would do the same from a lower level. Meaning the int256 
type does opIncrease on 2 int128 types, which break down to the 
native long type. Making a 256 or 512 silently creates all the 
lower ones until it can lower to a native type as the starting 
point.



 More importantly is if they fully act as built-in types with 
their declarations, except (for maybe auto promotion of course, 
although that might work too). The only two operations that are 
going to be slow no matter how you slice it are multiply and 
divide. If you don't use multiply or divide, your code will be 
fast regardless how many levels it goes.


Re: Game Development Using D

2016-05-22 Thread Vadim Lopatin via Digitalmars-d-learn

On Saturday, 21 May 2016 at 15:53:18 UTC, David wrote:

Hi,

I want to try to create a game using D. I'm a complete newbie 
though (other than having C/C++ experience). Where would I 
start? Does D have an openGL binding? I am assuming I'll need 
to leverage a good amount C APIs? Any list of these that would 
be useful it a game setting?


Obviously this all depends on *how* much work I want to do. 
Ideally, I'd like a collection of tools that will get me 
roughly the equivalent of what XNA provides.


In DlangUI there is Tetris game example

Repository: https://github.com/buggins/dlangui

Tetris: 
https://github.com/buggins/dlangui/tree/master/examples/tetris


Clone repository, cd dlangui/examples/tetris, dub run

As well, there are OpenGL example

https://github.com/buggins/dlangui/tree/master/examples/opengl

and DMiner example (minecraft-like rendering engine)

https://github.com/buggins/dlangui/tree/master/examples/dminer




Re: OpenGL with D tutorials

2016-05-22 Thread Guillaume Piolat via Digitalmars-d-learn

On Sunday, 22 May 2016 at 14:04:48 UTC, ixid wrote:

On Sunday, 22 May 2016 at 12:55:47 UTC, Guillaume Piolat wrote:

On Sunday, 22 May 2016 at 12:13:07 UTC, ixid wrote:
What is the best OpenGL tutorial with D to use? I've tried to 
use d-gamedev-intro and opengl-tutorials and seem to get 
errors, files that are no longer included are needed (dgl)? 
and deprecation messages.


Not a tutorial by any means but this example program can get 
you started with the annoying "Modern" OpenGL: 
https://github.com/d-gamedev-team/gfm/blob/master/examples/simpleshader/simpleshader.d


Thanks, I have tried to get all the libraries and link them and 
now get a huge number of error messages (I am almost certainly 
missing or have mis-installed something, it doesn't complain 
about failing to import anything though.


SeverityCodeDescription Project FileLineSuppression 
State
Error		Error 42: Symbol Undefined 
_D3gfm4sdl26window10SDL2Window11swapBuffersMFZv (void 
gfm.sdl2.window.SDL2Window.swapBuffers())		C:\Users\Adam\Documents\Visual Studio 2015\Projects\opengl\opengl\		
Error		Error 42: Symbol Undefined 
_D8derelict7opengl39functions7glClearPWNbNikZv		C:\Users\Adam\Documents\Visual Studio 2015\Projects\opengl\opengl\		
Error		Error 42: Symbol Undefined 
_D3gfm4sdl23sdl4SDL213processEventsMFZv (void 
gfm.sdl2.sdl.SDL2.processEvents())		C:\Users\Adam\Documents\Visual Studio 2015\Projects\opengl\opengl\		
Error		Error 42: Symbol Undefined 
_D3gfm6opengl7program9GLProgram3useMFZv (void 
gfm.opengl.program.GLProgram.use())		C:\Users\Adam\Documents\Visual Studio 2015\Projects\opengl\opengl\		
Error		Error 42: Symbol Undefined 
_D3gfm6opengl3vao5GLVAO6unbindMFZv (void 
gfm.opengl.vao.GLVAO.unbind())		C:\Users\Adam\Documents\Visual 
Studio 2015\Projects\opengl\opengl\		
Error		Error 42: Symbol Undefined 
_D3gfm6opengl7program9GLProgram5unuseMFZv (void 
gfm.opengl.program.GLProgram.unuse())		C:\Users\Adam\Documents\Visual Studio 2015\Projects\opengl\opengl\		
Error		Error 42: Symbol Undefined 
_D8derelict7opengl39functions10glViewportPWNbNiZv		C:\Users\Adam\Documents\Visual Studio 2015\Projects\opengl\opengl\		
Error		Error 42: Symbol Undefined 
_D3gfm4sdl28keyboard12SDL2Keyboard9isPressedMFiZb (bool 
gfm.sdl2.keyboard.SDL2Keyboard.isPressed(int))		C:\Users\Adam\Documents\Visual Studio 2015\Projects\opengl\opengl\		
Error		Error 42: Symbol Undefined 
_D3gfm4sdl26window10SDL2Window8setTitleMFAyaZv (void 
gfm.sdl2.window.SDL2Window.setTitle(immutable(char)[]))		C:\Users\Adam\Documents\Visual Studio 2015\Projects\opengl\opengl\		
Error		Error 42: Symbol Undefined 
_D3gfm6opengl7program9GLProgram7uniformMFAyaZC3gfm6opengl7uniform9GLUniform (gfm.opengl.uniform.GLUniform gfm.opengl.program.GLProgram.uniform(immutable(char)[]))		C:\Users\Adam\Documents\Visual Studio 2015\Projects\opengl\opengl\		
Error		Error 42: Symbol Undefined 
_D3gfm4sdl23sdl4SDL28keyboardMFZC3gfm4sdl28keyboard12SDL2Keyboard (gfm.sdl2.keyboard.SDL2Keyboard gfm.sdl2.sdl.SDL2.keyboard())		C:\Users\Adam\Documents\Visual Studio 2015\Projects\opengl\opengl\		
Error		Error 42: Symbol Undefined 
_D3gfm4math6matrix21__T6MatrixTfVii4Vii4Z6Matrix8identityFNaNbNiNfZS3gfm4math6matrix21__T6MatrixTfVii4Vii4Z6Matrix		C:\Users\Adam\Documents\Visual Studio 2015\Projects\opengl\opengl\		
Error		Error 42: Symbol Undefined 
_D8derelict4sdl29functions12SDL_GetTicksPUNbNiZk		C:\Users\Adam\Documents\Visual Studio 2015\Projects\opengl\opengl\		


Consider using DUB then.

$ dub
Package logger can be installed with version 2.66.0.
Use "dub upgrade" to perform those changes.
Performing "debug" build using dmd for x86_64.
colorize 1.0.5: target for configuration "library" is up to date.
gfm:logger 6.0.2: target for configuration "library" is up to 
date.
derelict-util 2.0.6: target for configuration "library" is up to 
date.
derelict-gl3 1.0.18: target for configuration "library" is up to 
date.

gfm:math 6.0.2: target for configuration "library" is up to date.
gfm:opengl 6.0.2: target for configuration "library" is up to 
date.
derelict-sdl2 1.9.7: target for configuration "library" is up to 
date.

gfm:sdl2 6.0.2: target for configuration "library" is up to date.
simpleshader ~master: target for configuration "application" is 
up to date.

To force a rebuild of up-to-date targets, run again with --force.
Running ./simpleshader




Re: mutable keyword

2016-05-22 Thread chmike via Digitalmars-d-learn
There is a benefit in not allowing to get pointers to class 
members. It allows to have movable object instances and this give 
access to some faster GC algorithms like generational garbage 
collection which is in use in Java.


As an old C++ programmer and newbee in D programming, the D 
constness and immutability concepts are confusing. They are so 
strong that I hardly see any use for them with objects.


There would be a much wider use to be able to tell user that he 
may assume the object is constant from the interface point of 
view. This is a huge help in code readability and program 
validity checking. I had the impression that this is the 
principle used for the pure keyword.



I would vote against the introduction of the mutable keyword as 
it exist in C++ because it is a half backed solution. First it 
tells the compiler that this variable is modifiable at any time 
by any method of the class. This is way I always felt 
uncomfortable using mutable. It punches a big hole in the 
constness protection.


The other problem is with inheritance (I know it's old school, 
but ok). If a derived class needs to modify a member variable of 
a base class that wasn't declared mutable, you're stuck. That is 
why C++ introduced the const_cast. This is much better in that 
the hole in the constness protection is very limited, but the 
code is also less pleasant to read.


How could be the D way to solve this ? My feeling is that it 
could be by introducing a mutate{...} block. All instructions in 
that block would be allowed to modify the const object. The 
developer intent would be clear, the code readable and the hole 
limited.


The difference with the C++ model is that in C++ we switch off 
the constness flag of member variables for a persistent or short 
time, in D we would switch off constness control in a block of 
instructions.


I didn't thought of all the implications yet.

I only talked about const objects. I still need to find a use 
case for immutable objects /S


Re: OpenGL with D tutorials

2016-05-22 Thread ixid via Digitalmars-d-learn

On Sunday, 22 May 2016 at 12:55:47 UTC, Guillaume Piolat wrote:

On Sunday, 22 May 2016 at 12:13:07 UTC, ixid wrote:
What is the best OpenGL tutorial with D to use? I've tried to 
use d-gamedev-intro and opengl-tutorials and seem to get 
errors, files that are no longer included are needed (dgl)? 
and deprecation messages.


Not a tutorial by any means but this example program can get 
you started with the annoying "Modern" OpenGL: 
https://github.com/d-gamedev-team/gfm/blob/master/examples/simpleshader/simpleshader.d


Thanks, I have tried to get all the libraries and link them and 
now get a huge number of error messages (I am almost certainly 
missing or have mis-installed something, it doesn't complain 
about failing to import anything though.


SeverityCodeDescription Project FileLineSuppression 
State
Error		Error 42: Symbol Undefined 
_D3gfm4sdl26window10SDL2Window11swapBuffersMFZv (void 
gfm.sdl2.window.SDL2Window.swapBuffers())		C:\Users\Adam\Documents\Visual Studio 2015\Projects\opengl\opengl\		
Error		Error 42: Symbol Undefined 
_D8derelict7opengl39functions7glClearPWNbNikZv		C:\Users\Adam\Documents\Visual Studio 2015\Projects\opengl\opengl\		
Error		Error 42: Symbol Undefined 
_D3gfm4sdl23sdl4SDL213processEventsMFZv (void 
gfm.sdl2.sdl.SDL2.processEvents())		C:\Users\Adam\Documents\Visual Studio 2015\Projects\opengl\opengl\		
Error		Error 42: Symbol Undefined 
_D3gfm6opengl7program9GLProgram3useMFZv (void 
gfm.opengl.program.GLProgram.use())		C:\Users\Adam\Documents\Visual Studio 2015\Projects\opengl\opengl\		
Error		Error 42: Symbol Undefined 
_D3gfm6opengl3vao5GLVAO6unbindMFZv (void 
gfm.opengl.vao.GLVAO.unbind())		C:\Users\Adam\Documents\Visual 
Studio 2015\Projects\opengl\opengl\		
Error		Error 42: Symbol Undefined 
_D3gfm6opengl7program9GLProgram5unuseMFZv (void 
gfm.opengl.program.GLProgram.unuse())		C:\Users\Adam\Documents\Visual Studio 2015\Projects\opengl\opengl\		
Error		Error 42: Symbol Undefined 
_D8derelict7opengl39functions10glViewportPWNbNiZv		C:\Users\Adam\Documents\Visual Studio 2015\Projects\opengl\opengl\		
Error		Error 42: Symbol Undefined 
_D3gfm4sdl28keyboard12SDL2Keyboard9isPressedMFiZb (bool 
gfm.sdl2.keyboard.SDL2Keyboard.isPressed(int))		C:\Users\Adam\Documents\Visual Studio 2015\Projects\opengl\opengl\		
Error		Error 42: Symbol Undefined 
_D3gfm4sdl26window10SDL2Window8setTitleMFAyaZv (void 
gfm.sdl2.window.SDL2Window.setTitle(immutable(char)[]))		C:\Users\Adam\Documents\Visual Studio 2015\Projects\opengl\opengl\		
Error		Error 42: Symbol Undefined 
_D3gfm6opengl7program9GLProgram7uniformMFAyaZC3gfm6opengl7uniform9GLUniform (gfm.opengl.uniform.GLUniform gfm.opengl.program.GLProgram.uniform(immutable(char)[]))		C:\Users\Adam\Documents\Visual Studio 2015\Projects\opengl\opengl\		
Error		Error 42: Symbol Undefined 
_D3gfm4sdl23sdl4SDL28keyboardMFZC3gfm4sdl28keyboard12SDL2Keyboard 
(gfm.sdl2.keyboard.SDL2Keyboard 
gfm.sdl2.sdl.SDL2.keyboard())		C:\Users\Adam\Documents\Visual 
Studio 2015\Projects\opengl\opengl\		
Error		Error 42: Symbol Undefined 
_D3gfm4math6matrix21__T6MatrixTfVii4Vii4Z6Matrix8identityFNaNbNiNfZS3gfm4math6matrix21__T6MatrixTfVii4Vii4Z6Matrix		C:\Users\Adam\Documents\Visual Studio 2015\Projects\opengl\opengl\		
Error		Error 42: Symbol Undefined 
_D8derelict4sdl29functions12SDL_GetTicksPUNbNiZk		C:\Users\Adam\Documents\Visual Studio 2015\Projects\opengl\opengl\		




Re: Use Requests to send data to webpage - how?

2016-05-22 Thread TheDGuy via Digitalmars-d-learn

On Friday, 20 May 2016 at 14:42:19 UTC, TheDGuy wrote:

On Friday, 20 May 2016 at 09:21:33 UTC, Kagamin wrote:

Does this work?

Request rq = Request();
Response rs = rq.exec!"GET"("http://somewebpage.org/;, 
[parameter:data]);


No :(


If i call my SQL.php function directly with:

Response rs = rq.exec!"GET"("http://site/SQL.php;, 
["action":"insertTemp","value":"7"]);


it works perfectly fine. But i don't understand why my html site 
does not get that request...Any ideas?


Re: mutable keyword

2016-05-22 Thread Jonathan M Davis via Digitalmars-d-learn
On Sunday, May 22, 2016 09:42:54 Jack Applegame via Digitalmars-d-learn wrote:
> On Saturday, 21 May 2016 at 21:49:23 UTC, Jonathan M Davis wrote:
> > Rebindable is in kind of a weird grey area. It involves a
> > union, not casting, and it's only ever mutating the class
> > reference, not the object itself. Certainly, if it mutated the
> > object, it would be undefined behavior, but it's just mutating
> > the reference - but then again, the type system itself doesn't
> > really differentiate between the two. So, I suspect that what
> > it comes down to is that Rebindable is doing something that you
> > really shouldn't, but because it's using a union rather than a
> > cast, and it's so limited in what it's mutating, its behavior
> > is effectively defined in the sense that the compiler has no
> > leeway, but's probably still technically undefined. I don't
> > know what Walter would say though. Certainly, if casting were
> > involved, it would be undefined behavior without question, and
> > it's not the sort of thing that you should be doing in your own
> > code.
> >
> > [...]
>
> I agree. But I think we need something that allows *logical*
> const and immutable.
> Strict binding constness to physical memory constancy is not
> always necessary and sometimes even harmful.

Actually doing logical const/immutable isn't really possible. Not even C++
does that. Rather, it provides backdoors that you can then use to implement
a sort of logical const, but there is zero guarantee that that's actually
what's happening. It's completely a matter of convention at that point. At
best, it would be possible to indicate that a portion of a type is const or
immutable while allowing certain parts of it to be mutable, in which case,
there are zero constness guarantees about the part that's always mutable and
no guarantees that the stuff that's mutable isn't part of the logical state
of the object.

Given how const and immutable work in D, having any portion of them be
treated as mutable, becomes highly problematic. It could theoretically be
done by having to mark such variables with an attribute and mark such types
with a similar attribute so that the compiler knows that the type in
question is not really following the rules of const or immutable and thus
doesn't make any assumptions about it like it would normally, but it risks
getting rather complicated, and Walter is most definitely not in favor of
such an idea. He is absolutely adamant that const is useless unless it's
fully const with no backdoors whatsoever. So, I'd be very surprised if any
kind of @mutable were added to the language. Certainly, you'd have to have a
very strong technical reason to convince him otherwise, and odds are that
he's just going to tell you to not use const, if it's not going to work for
the type to be const.

- Jonathan M Davis



Re: OpenGL with D tutorials

2016-05-22 Thread Guillaume Piolat via Digitalmars-d-learn

On Sunday, 22 May 2016 at 12:13:07 UTC, ixid wrote:
What is the best OpenGL tutorial with D to use? I've tried to 
use d-gamedev-intro and opengl-tutorials and seem to get 
errors, files that are no longer included are needed (dgl)? and 
deprecation messages.


Not a tutorial by any means but this example program can get you 
started with the annoying "Modern" OpenGL: 
https://github.com/d-gamedev-team/gfm/blob/master/examples/simpleshader/simpleshader.d


Re: mutable keyword

2016-05-22 Thread Marc Schütz via Digitalmars-d-learn

On Sunday, 22 May 2016 at 09:42:54 UTC, Jack Applegame wrote:
I agree. But I think we need something that allows *logical* 
const and immutable.
Strict binding constness to physical memory constancy is not 
always necessary and sometimes even harmful.


http://wiki.dlang.org/DIP89


OpenGL with D tutorials

2016-05-22 Thread ixid via Digitalmars-d-learn
What is the best OpenGL tutorial with D to use? I've tried to use 
d-gamedev-intro and opengl-tutorials and seem to get errors, 
files that are no longer included are needed (dgl)? and 
deprecation messages.


Re: Is there a 128-bit integer in D?

2016-05-22 Thread Guillaume Piolat via Digitalmars-d-learn

On Sunday, 22 May 2016 at 09:47:54 UTC, Era Scarecrow wrote:

On Sunday, 22 May 2016 at 09:39:45 UTC, Saurabh Das wrote:

On Sunday, 22 May 2016 at 09:07:32 UTC, Guillaume Piolat wrote:

https://github.com/d-gamedev-team/gfm/blob/master/integers/gfm/integers/wideint.d


wideint was exactly what I was looking for! Thank you so much 
:)


 I WANT! If this does the job, I want it as part of the Phobos 
library!!


This request again ;)
I don't know how this works, someone has to propose it, clean it 
up and respond to feedback I guess.


Re: Is there a 128-bit integer in D?

2016-05-22 Thread Era Scarecrow via Digitalmars-d-learn

On Sunday, 22 May 2016 at 09:39:45 UTC, Saurabh Das wrote:

On Sunday, 22 May 2016 at 09:07:32 UTC, Guillaume Piolat wrote:

https://github.com/d-gamedev-team/gfm/blob/master/integers/gfm/integers/wideint.d


wideint was exactly what I was looking for! Thank you so much :)


 I WANT! If this does the job, I want it as part of the Phobos 
library!!


Re: Overload new and delete to not use GC?

2016-05-22 Thread Basile B. via Digitalmars-d-learn

On Sunday, 22 May 2016 at 07:35:32 UTC, Rusty wrote:
I know it's possible to do [explicit object 
allocation](http://wiki.dlang.org/Memory_Management#Explicit_Class_Instance_Allocation) on the heap, but I find that quite cumbersome.


So.. is it possible to overload 'new' and 'delete' to not use 
GC?


use the Mallocator with make and dispose:

import std.stdio;
import std.experimental.allocator: make, dispose;
import std.experimental.allocator.mallocator: Mallocator;

class Foo{}

void main(string[] args)
{
Foo foo = make!Foo(Mallocator.instance);
dispose(Mallocator.instance, foo);
}

Also, it seems many features of the language rely on GC. Is 
there a definitive list of those ?


If you use DMD as compiler the switch -vgc can help to track GC 
allocations.


Re: mutable keyword

2016-05-22 Thread Jack Applegame via Digitalmars-d-learn

On Saturday, 21 May 2016 at 21:49:23 UTC, Jonathan M Davis wrote:
Rebindable is in kind of a weird grey area. It involves a 
union, not casting, and it's only ever mutating the class 
reference, not the object itself. Certainly, if it mutated the 
object, it would be undefined behavior, but it's just mutating 
the reference - but then again, the type system itself doesn't 
really differentiate between the two. So, I suspect that what 
it comes down to is that Rebindable is doing something that you 
really shouldn't, but because it's using a union rather than a 
cast, and it's so limited in what it's mutating, its behavior 
is effectively defined in the sense that the compiler has no 
leeway, but's probably still technically undefined. I don't 
know what Walter would say though. Certainly, if casting were 
involved, it would be undefined behavior without question, and 
it's not the sort of thing that you should be doing in your own 
code.


[...]


I agree. But I think we need something that allows *logical* 
const and immutable.
Strict binding constness to physical memory constancy is not 
always necessary and sometimes even harmful.


Re: Is there a 128-bit integer in D?

2016-05-22 Thread Saurabh Das via Digitalmars-d-learn

On Sunday, 22 May 2016 at 09:07:32 UTC, Guillaume Piolat wrote:

On Sunday, 22 May 2016 at 07:40:08 UTC, Nicholas Wilson wrote:

On Saturday, 21 May 2016 at 09:43:38 UTC, Saurabh Das wrote:
I see that 'cent' and 'ucent' are reserved for future use but 
not yet implemented. Does anyone have a working 
implementation of these types?


Alternatively, is there an any effort towards implementation 
of arbitrary-sized integers in Phobos?


Thanks,
Saurabh


There is a recursive arbitrary sized integer implementation 
floating around somewhere. Perhaps try dub for some math 
libraries.


https://github.com/d-gamedev-team/gfm/blob/master/integers/gfm/integers/wideint.d


wideint was exactly what I was looking for! Thank you so much :)

Saurabh



Re: Overload new and delete to not use GC?

2016-05-22 Thread Guillaume Piolat via Digitalmars-d-learn

On Sunday, 22 May 2016 at 07:35:32 UTC, Rusty wrote:
I know it's possible to do [explicit object 
allocation](http://wiki.dlang.org/Memory_Management#Explicit_Class_Instance_Allocation) on the heap, but I find that quite cumbersome.


So.. is it possible to overload 'new' and 'delete' to not use 
GC?


The way to do it is to use emplace() and destroy() instead.
https://p0nce.github.io/d-idioms/#Placement-new-with-emplace

Unique!T, RefCounted!T, and Scoped!T use emplace() internally.


Also, it seems many features of the language rely on GC. Is 
there a definitive list of those?


- new
- Appending and concatenating slices
- Homogeneous template parameters void f(
- Some array literals
- Closures that escape
- other features I don't recall

@nogc lets you avoid all of them and is necessary to avoid 
unintentional allocations.




Re: Is there a 128-bit integer in D?

2016-05-22 Thread Guillaume Piolat via Digitalmars-d-learn

On Sunday, 22 May 2016 at 07:40:08 UTC, Nicholas Wilson wrote:

On Saturday, 21 May 2016 at 09:43:38 UTC, Saurabh Das wrote:
I see that 'cent' and 'ucent' are reserved for future use but 
not yet implemented. Does anyone have a working implementation 
of these types?


Alternatively, is there an any effort towards implementation 
of arbitrary-sized integers in Phobos?


Thanks,
Saurabh


There is a recursive arbitrary sized integer implementation 
floating around somewhere. Perhaps try dub for some math 
libraries.


https://github.com/d-gamedev-team/gfm/blob/master/integers/gfm/integers/wideint.d


Re: Is there a 128-bit integer in D?

2016-05-22 Thread Nicholas Wilson via Digitalmars-d-learn

On Saturday, 21 May 2016 at 09:43:38 UTC, Saurabh Das wrote:
I see that 'cent' and 'ucent' are reserved for future use but 
not yet implemented. Does anyone have a working implementation 
of these types?


Alternatively, is there an any effort towards implementation of 
arbitrary-sized integers in Phobos?


Thanks,
Saurabh


There is a recursive arbitrary sized integer implementation 
floating around somewhere. Perhaps try dub for some math 
libraries.


Overload new and delete to not use GC?

2016-05-22 Thread Rusty via Digitalmars-d-learn
I know it's possible to do [explicit object 
allocation](http://wiki.dlang.org/Memory_Management#Explicit_Class_Instance_Allocation) on the heap, but I find that quite cumbersome.


So.. is it possible to overload 'new' and 'delete' to not use GC?

Also, it seems many features of the language rely on GC. Is there 
a definitive list of those?




Re: Is there a 128-bit integer in D?

2016-05-22 Thread tcak via Digitalmars-d-learn

On Saturday, 21 May 2016 at 09:56:51 UTC, Stefan Koch wrote:

On Saturday, 21 May 2016 at 09:43:38 UTC, Saurabh Das wrote:
I see that 'cent' and 'ucent' are reserved for future use but 
not yet implemented. Does anyone have a working implementation 
of these types?


Alternatively, is there an any effort towards implementation 
of arbitrary-sized integers in Phobos?


Thanks,
Saurabh


There is BigInt in phobos.


I think cent and ucent could be implemented as ulong is possible 
to be used on 32-bit systems by adding extra assembly 
instructions. This way, when (and if) 128-bit systems are 
developed, compiler would be updated only.


Re: Linking C code with D...

2016-05-22 Thread Era Scarecrow via Digitalmars-d-learn

On Sunday, 22 May 2016 at 06:10:07 UTC, rikki cattermole wrote:

Move id_compress out of the unittest block.


 Well that certainly did it...


Re: Linking C code with D...

2016-05-22 Thread rikki cattermole via Digitalmars-d-learn

On 22/05/2016 6:08 PM, Era Scarecrow wrote:

 While trying to make a benchmark for comparing results of my possible
compression replacement, I'm finding it quite difficult to link the c
code with the d code.

 So I've managed to compile the compress.c file using dmc, but when I
link the obj file to reference it I don't seem to see it...

I've linked a prototype before trying to call it

What am I hopelessly doing wrong?
unittest {
string haystack = "...";
auto f1 = (){
RawBuffer rb;
compress(rb, haystack);
};

auto f2 = (){
size_t plen;
extern (C) char *id_compress(char *id, int idlen, size_t *plen);
id_compress(cast(char*) haystack.ptr, haystack.length, );
};

auto memoryhungry_compress = benchmark!(f1)(100_000);
auto original_compress = benchmark!(f2)(100_000);
...
}

$ dmd compress_c.obj compress.d -g -O -unittest -main

 Error 42: Symbol Undefined
__D8compress16__unittestL366_5FZ9__lambda2MFZ11id_compressUPaiPkZPa



Move id_compress out of the unittest block.


Linking C code with D...

2016-05-22 Thread Era Scarecrow via Digitalmars-d-learn
 While trying to make a benchmark for comparing results of my 
possible compression replacement, I'm finding it quite difficult 
to link the c code with the d code.


 So I've managed to compile the compress.c file using dmc, but 
when I link the obj file to reference it I don't seem to see it...


I've linked a prototype before trying to call it

What am I hopelessly doing wrong?
unittest {
string haystack = "...";
auto f1 = (){
RawBuffer rb;
compress(rb, haystack);
};

auto f2 = (){
size_t plen;
extern (C) char *id_compress(char *id, int idlen, size_t 
*plen);
id_compress(cast(char*) haystack.ptr, haystack.length, 
);

};

auto memoryhungry_compress = benchmark!(f1)(100_000);
auto original_compress = benchmark!(f2)(100_000);
...
}

$ dmd compress_c.obj compress.d -g -O -unittest -main

 Error 42: Symbol Undefined 
__D8compress16__unittestL366_5FZ9__lambda2MFZ11id_compressUPaiPkZPa