having problem with `std.algorithm.each`

2015-11-30 Thread ref2401 via Digitalmars-d-learn
It seems like `std.algorithm.each` is not executed in the example 
below.

Could anyone tell why?
Thank you.


import std.algorithm;

void main(string[] args) {
int[] arr = [1, 2, 3, 4, 5];

arr.each!((ref e) => {
writeln(e); // does not print
++e;
})();

writeln(arr); // prints [1, 2, 3, 4, 5]
}


Re: having problem with `std.algorithm.each`

2015-11-30 Thread ref2401 via Digitalmars-d-learn

DMD 2.069.1
OS Win8.1 Enterprise


vibe.d-example illustrating Dynamic Textual Web-Interface

2015-11-30 Thread Nordlöw via Digitalmars-d-learn

Can somebody please show a enlightening example of, so called,

"revamp of the REST interface generator"

added to release 0.7.26 at:

http://vibed.org/blog/posts/vibe-release-0.7.26

I want to use vibe.d to add a modern dynamic web-interface to my 
knowledge graph I'm currently developing.


More specifically I would like an interface that makes rich use 
of dynamic HTML. When the user changes the contents of a 
search/command-line text-field the client logic shall on the fly 
(after a certain delay) lookup related information with 
vibe.d-server and present the user with relevant hits or 
completions in a another part of page (HTML div-block).


Does anybody have a similar vibe.d-project to be inspired from, 
in this regard?


Re: Classes as enums in D?

2015-11-30 Thread Marc Schütz via Digitalmars-d-learn

On Monday, 30 November 2015 at 08:08:20 UTC, Meta wrote:

class WhiteKey
{
private immutable int halfStepsToNext;
private immutable int halfStepsToPrevious;

enum
{
A = new WhiteKey(2, 2),
B = new WhiteKey(2, 1),
C = new WhiteKey(1, 2),
D = new WhiteKey(2, 2),
E = new WhiteKey(2, 1),
F = new WhiteKey(1, 2),
G = new WhiteKey(2, 2),
}

private this(int halfStepsToPrevious, int halfStepsToNext)
{
this.halfStepsToPrevious = halfStepsToPrevious;
this.halfStepsToNext = halfStepsToNext;
}
}

However, you do NOT want to do this, as everywhere you use 
WhiteKey's members, a new object will be created. For example:


auto f = WhiteKey.A;
auto n = WhiteKey.A;

import std.stdio;
writeln(, " ", );



You're misinterpreting this:

enum X {
A = new Object,
B = new Object,
}

void main() {
import std.stdio;
writeln(cast(void*) X.A);
writeln(cast(void*) X.A);
}

# output:
470910
470910

You're print the address of `f` and `n` on the stack, not the 
reference they're pointing to.


But it's true that enums of mutable _arrays_ do create a new 
instance every time they're used:


enum X {
A = [1,2,3],
B = [4,5,6],
}

void main() {
import std.stdio;
writeln(X.A.ptr);
writeln(X.A.ptr);
}

# output:
7FD887F0E000
7FD887F0E010


Re: DUB, Platform specifications and dependencies

2015-11-30 Thread Sönke Ludwig via Digitalmars-d-learn

Am 24.11.2015 um 19:51 schrieb Zardoz:

Actually I'm trying to setup dub to not grab a dependency on Windows (
https://github.com/Zardoz89/DEDCPU-16/blob/master/dub.sdl ) :

name "dedcpu"
authors "Luis Panadero Guardeño"
targetType "none"
license "BSD 3-clause"
description "DCPU-16 tools"

subPackage {
   name "lem1802"
   description "Visual LEM1802 font editor"
   targetType "executable"
   targetName "lem1802"
   excludedSourceFiles "src/bconv.d"
   excludedSourceFiles "src/ddis.d"
   dependency "gtk-d:gtkd" version="~>3.2.0" platform="posix"
   libs "gtkd" platform="windows"
}

...

How ever, running dub on Windows (tested on two different machines),
ignores platform specification for gtk-d dependency . What I'm doing
wrong ?


Platform specifications are currently not supported for dependencies due 
to the way the dependency resolver works. However, it is possible to use 
platform specific configurations for this purpose:


name "dedcpu"
authors "Luis Panadero Guardeño"
targetType "none"
license "BSD 3-clause"
description "DCPU-16 tools"

subPackage {
   name "lem1802"
   description "Visual LEM1802 font editor"
   targetType "executable"
   targetName "lem1802"
   excludedSourceFiles "src/bconv.d"
   excludedSourceFiles "src/ddis.d"
   libs "gtkd" platform="windows"

   configuration "nogtk" {
  platforms "windows"
   }

   configuration "gtk" {
  platforms "posix"
  dependency "gtk-d:gtkd" version="~>3.2.0"
   }
}



Re: DateTime.opBinary

2015-11-30 Thread Chris Wright via Digitalmars-d-learn
On Mon, 30 Nov 2015 01:30:28 -0800, Jonathan M Davis via
Digitalmars-d-learn wrote:

> On Sunday, November 29, 2015 23:53:41 Chris Wright via
> Digitalmars-d-learn wrote:
>> Unfortunately, ddoc doesn't automatically cross-reference these for
>> you,
>> which results in confusion. (As if it weren't confusing enough to have
>> everything wrapped in templates with filters rather than simply using
>> const(Duration).)
> 
> Once TickDuration finally goes away, then functions like DateTime's
> opBinary can be simplified to just take Duration.
> 
> But until TickDuration and the few things that use it in their API have
> gone through the deprecation process, we're stuck with it in places like
> this.
> 
> - Jonathan M Davis

Or with explicit overloads, which would be easier to document and easier 
to read and as easy to maintain.


Re: Any D IDE on Mac OSX with debugging support?

2015-11-30 Thread Bruno Medeiros via Digitalmars-d-learn

On 16/11/2015 06:45, Vadim Lopatin wrote:

Hello,


Is there any IDE which allows debugging D apps on OSX?
I'm trying Mono-D, but getting error
"Debugger operation failed A syntax error in expression, near
'sizeof(void*)'"

GDB is installed using homebrew. Probably, something is wrong with my
gdb. When I'm trying to start debugging using console GDB interface - it
fails with message about unsigned application. Is there any instruction
how to get GDB working on OSX?

Code completion and symbol lookups neither do not work on Mono-D / OSX.
Does anyone managed to get it working?


Best regards,
 Vadim



Yes, DDT ( http://ddt-ide.github.io/ ) supports debugging on OSX, using 
GDB, but you have to install it with homebrew. If you have done it 
already, then it should work, but check this article as well: 
http://ntraft.com/installing-gdb-on-os-x-mavericks/



--
Bruno Medeiros
https://twitter.com/brunodomedeiros


Re: Can't understand how to do server response with vibed

2015-11-30 Thread Sebastiaan Koppe via Digitalmars-d-learn

On Sunday, 29 November 2015 at 07:37:56 UTC, Suliman wrote:
On Saturday, 28 November 2015 at 23:21:21 UTC, Sebastiaan Koppe 
wrote:

On Saturday, 28 November 2015 at 19:05:59 UTC, Suliman wrote:
And also I can't understand difference between 
HTTPClientRequest and HTTPServerRequest


If the application (vibe.d) makes a request, it is the client. 
If the request is made to your application, it is the server.


In your case your angular app is the client, and your vibe.d 
app is the server. Therefor, HTTPServer(Request|Response).


Could you explain me about: HTTPServerRequest and HTTPRequest. 
I can't understand difference.


No idea. Probably HTTPRequest provides common functionality for 
both HTTPServerRequest and HTTPClientRequest. Just guessing.


Re: vibe.d-example illustrating Dynamic Textual Web-Interface

2015-11-30 Thread Sebastiaan Koppe via Digitalmars-d-learn

On Monday, 30 November 2015 at 10:08:17 UTC, Nordlöw wrote:

Can somebody please show a enlightening example of, so called,

"revamp of the REST interface generator"

added to release 0.7.26 at:

http://vibed.org/blog/posts/vibe-release-0.7.26

I want to use vibe.d to add a modern dynamic web-interface to 
my knowledge graph I'm currently developing.


More specifically I would like an interface that makes rich use 
of dynamic HTML. When the user changes the contents of a 
search/command-line text-field the client logic shall on the 
fly (after a certain delay) lookup related information with 
vibe.d-server and present the user with relevant hits or 
completions in a another part of page (HTML div-block).


Does anybody have a similar vibe.d-project to be inspired from, 
in this regard?


I have written a mpd (Music Player Daemon) client in D and just 
finished the web app (bar a couple of features).


All of the rendering is done on the client and only json data is 
transmitted between server and web app. Just finished the 
WebSocket part so if anybody changes the music/volume/playlist, 
all clients update the interface.


The code could use a cleanup/refactoring but everything is 
working.


The D parts of interest are the MpdApiInterface1 and MpdApi1 
classes in source/app.d


For the JS parts you can start with source/index.js, but its full 
of ES6 classes and react code. So that might not be your cup of 
tea. In index.js there are mainly React Classes (basically html 
renderers) and React Container Classes (basically code handling 
input changes).


In the source/actions folder there are some js files that call 
the server and modify state (volume/song). In the source/stores 
folder, some js files that only retrieve data.


The js architecture if influenced by react/flux/facebook's, but I 
am toying with it to see what fits for me. Specifically I rely a 
lot on RxJS for the observables.


For the project I decided to put js and d files in the same 
folders. That might not have been a good idea. Also dub has some 
issues with one of the D dependencies that I haven't updated in 
months.


Code can be found here: https://bitbucket.org/skoppe/mpc/src


Re: vibe.d-example illustrating Dynamic Textual Web-Interface

2015-11-30 Thread David DeWitt via Digitalmars-d-learn
On Monday, 30 November 2015 at 20:05:42 UTC, Sebastiaan Koppe 
wrote:



Code can be found here: https://bitbucket.org/skoppe/mpc/src


Looks good.  Have you looked at Redux and Webpack?  I am working 
on a Redux example and we have switched to Webpack and Redux at 
work and it is nice.


Re: DUB, Platform specifications and dependencies

2015-11-30 Thread Zardoz via Digitalmars-d-learn

On Monday, 30 November 2015 at 16:54:43 UTC, Sönke Ludwig wrote:

Am 24.11.2015 um 19:51 schrieb Zardoz:
Actually I'm trying to setup dub to not grab a dependency on 
Windows (

https://github.com/Zardoz89/DEDCPU-16/blob/master/dub.sdl ) :

name "dedcpu"
authors "Luis Panadero Guardeño"
targetType "none"
license "BSD 3-clause"
description "DCPU-16 tools"

subPackage {
   name "lem1802"
   description "Visual LEM1802 font editor"
   targetType "executable"
   targetName "lem1802"
   excludedSourceFiles "src/bconv.d"
   excludedSourceFiles "src/ddis.d"
   dependency "gtk-d:gtkd" version="~>3.2.0" platform="posix"
   libs "gtkd" platform="windows"
}

...

How ever, running dub on Windows (tested on two different 
machines),
ignores platform specification for gtk-d dependency . What I'm 
doing

wrong ?


Platform specifications are currently not supported for 
dependencies due to the way the dependency resolver works. 
However, it is possible to use platform specific configurations 
for this purpose:


name "dedcpu"
authors "Luis Panadero Guardeño"
targetType "none"
license "BSD 3-clause"
description "DCPU-16 tools"

subPackage {
   name "lem1802"
   description "Visual LEM1802 font editor"
   targetType "executable"
   targetName "lem1802"
   excludedSourceFiles "src/bconv.d"
   excludedSourceFiles "src/ddis.d"
   libs "gtkd" platform="windows"

   configuration "nogtk" {
  platforms "windows"
   }

   configuration "gtk" {
  platforms "posix"
  dependency "gtk-d:gtkd" version="~>3.2.0"
   }
}


Thanks!! I ended doing some minor change to get it working :

subPackage {
  name "lem1802"
  description "Visual LEM1802 font editor"
  excludedSourceFiles "src/bconv.d"
  excludedSourceFiles "src/ddis.d"

  configuration "nogtk" {
targetType "executable"
targetName "lem1802"
platform "windows"
libs "gtkd"
  }

  configuration "gtk" {
targetType "executable"
targetName "lem1802"
platform "posix"
dependency "gtk-d:gtkd" version="~>3.2.0"
  }
}



Re: vibe.d-example illustrating Dynamic Textual Web-Interface

2015-11-30 Thread Sebastiaan Koppe via Digitalmars-d-learn

On Monday, 30 November 2015 at 20:23:48 UTC, David DeWitt wrote:
Have you looked at Redux and Webpack?  I am working on a Redux 
example and we have switched to Webpack and Redux at work and 
it is nice.


I know about both yes. Webpack would probably beat browserify, 
but I haven't gotten the time to migrate myself. Their hot code 
reloading looks good though.


Isn't redux the client side for GraphQL? I followed it a bit but 
it being so fresh, decided to wait.


A GraphQL interface generator for vibe.d would be nice.


Re: vibe.d-example illustrating Dynamic Textual Web-Interface

2015-11-30 Thread David DeWitt via Digitalmars-d-learn
On Monday, 30 November 2015 at 20:38:12 UTC, Sebastiaan Koppe 
wrote:

On Monday, 30 November 2015 at 20:23:48 UTC, David DeWitt wrote:
Have you looked at Redux and Webpack?  I am working on a Redux 
example and we have switched to Webpack and Redux at work and 
it is nice.


I know about both yes. Webpack would probably beat browserify, 
but I haven't gotten the time to migrate myself. Their hot code 
reloading looks good though.


Isn't redux the client side for GraphQL? I followed it a bit 
but it being so fresh, decided to wait.


A GraphQL interface generator for vibe.d would be nice.


Redux is luscious!!!  It is a flux-like implementation but you 
dont have to deal with individual stores as the app's state lives 
in a single object tree. Once you go thru it you'll like it.



https://github.com/rackt/redux

https://github.com/xgrommx/awesome-redux


Re: isAllocator

2015-11-30 Thread Rikki Cattermole via Digitalmars-d-learn

On 01/12/15 3:23 AM, Tofu Ninja wrote:

On Monday, 30 November 2015 at 14:21:49 UTC, Tofu Ninja wrote:

Is there something like isInputRange for allocators, I tried looking
for something but couldn't find anything? If not, why not?


Aka, some way to check that type T is an allocator.


Doesn't look like it.

bool isAllocator(Alloc)() pure {
return __traits(compiles, {IAllocator alloc = new 
CAllocatorImpl!Alloc;});
}

That should work however.


need help with Windows CreateNamedPipe Security attributes process with undefined symbols at compile time

2015-11-30 Thread Jonathan Villa via Digitalmars-d-learn

Hi,

I've been trying to create a NamedPipe with security attributes 
but at compile time throws:

Error 42: Symbol Undefined _InitializeSecurityDescriptor@8
Error 42: Symbol Undefined _SetSecurityDescriptorDacl@16

This is my code, I'm trying to do it using a class:


module asi.pipe;

import core.sys.windows.windows;
import core.sys.windows.winbase;
import core.stdc.stdlib;

class Pipe
{
private HANDLE hPipe;
private SECURITY_ATTRIBUTES sa;

this(string name)
{
CreatePipe(name);
}

private void CreatePipe(string pipename)
{

sa.lpSecurityDescriptor = malloc(SECURITY_DESCRIPTOR.sizeof);

InitializeSecurityDescriptor(cast(PSECURITY_DESCRIPTOR)sa.lpSecurityDescriptor, 
1);

SetSecurityDescriptorDacl(cast(PSECURITY_DESCRIPTOR)sa.lpSecurityDescriptor, 
TRUE, cast(ACL*)0, FALSE);
sa.nLength = sa.sizeof;
sa.bInheritHandle = TRUE;

CreateNamedPipeA(cast(char*)pipename,
 PIPE_ACCESS_DUPLEX,
 (PIPE_TYPE_BYTE | 
PIPE_READMODE_BYTE | PIPE_WAIT),
 PIPE_UNLIMITED_INSTANCES,
 4096,
 1536,
 0,
 );
}
}


Additional Info:
The installer came with just a few files to handle with Windows, 
comparing the the huge files that are in the repository of 
druntime: 
https://github.com/D-Programming-Language/druntime/tree/master/src/core/sys/windows
So I renamed the old import to Windows2 and copied this whole 
github/windows folder there and used its winbase.d because there 
are the functions definitions that I needed.


This functions that throw me errors belongs to the advapi32.dll 
file. I tried to add pragma(lib "advapi32"); but it didn't work.


I'm new/noob dealing with D and I would appreciate any help.

thanks.


Re: need help with Windows CreateNamedPipe Security attributes process with undefined symbols at compile time

2015-11-30 Thread Nicholas Wilson via Digitalmars-d-learn

On Tuesday, 1 December 2015 at 04:10:55 UTC, Jonathan Villa wrote:

Hi,

I've been trying to create a NamedPipe with security attributes 
but at compile time throws:

Error 42: Symbol Undefined _InitializeSecurityDescriptor@8
Error 42: Symbol Undefined _SetSecurityDescriptorDacl@16


What is causing this: Is this a compile or a linker error?


This is my code, I'm trying to do it using a class:


module asi.pipe;

import core.sys.windows.windows;
import core.sys.windows.winbase;
import core.stdc.stdlib;

class Pipe
{
private HANDLE hPipe;
private SECURITY_ATTRIBUTES sa;

this(string name)
{
CreatePipe(name);
}

private void CreatePipe(string pipename)
{

sa.lpSecurityDescriptor = malloc(SECURITY_DESCRIPTOR.sizeof);

InitializeSecurityDescriptor(cast(PSECURITY_DESCRIPTOR)sa.lpSecurityDescriptor, 
1);

is the cast necessary?


SetSecurityDescriptorDacl(cast(PSECURITY_DESCRIPTOR)sa.lpSecurityDescriptor, 
TRUE, cast(ACL*)0, FALSE);
sa.nLength = sa.sizeof;
sa.bInheritHandle = TRUE;

CreateNamedPipeA(cast(char*)pipename,
you want toStringz(pipename) or if you know pipename is a null 
terminated string pipename.ptr

 PIPE_ACCESS_DUPLEX,
 (PIPE_TYPE_BYTE | 
PIPE_READMODE_BYTE | PIPE_WAIT),
 PIPE_UNLIMITED_INSTANCES,
 4096,
 1536,
 0,
 );
}
}


Additional Info:
The installer came with just a few files to handle with 
Windows, comparing the the huge files that are in the 
repository of druntime: 
https://github.com/D-Programming-Language/druntime/tree/master/src/core/sys/windows
So I renamed the old import to Windows2 and copied this whole 
github/windows folder there and used its winbase.d because 
there are the functions definitions that I needed.


This functions that throw me errors belongs to the advapi32.dll 
file. I tried to add pragma(lib "advapi32"); but it didn't work.

this needs a comma i.e.
pragma(lib ,"advapi32");
it may also require the appropriate file suffix (.dll or .lib)


I'm new/noob dealing with D and I would appreciate any help.

thanks.




Re: Classes as enums in D?

2015-11-30 Thread Andrew LaChance via Digitalmars-d-learn

On Monday, 30 November 2015 at 08:08:20 UTC, Meta wrote:
This doesn't quite work in D; you'd have to make each WhiteKey 
const (which is probably not a bad idea anyway if you're using 
it like an enum). However, it's better to just do this with 
plain old value-type structs. It's exactly the same as my 
previous code defining a WhiteKey class with an embedded enum, 
but using a struct instead of a class.


Thanks for the responses, everyone!  Meta's is closest to one of 
the solutions I was thinking about and I'll probably end up going 
that way.  It's not quite as pretty (as I would need to 
explicitly add the enum "id" to the struct and switch on that 
value instead of just the enum variable and I'll need to make 
sure no ids overlap), but it should work.  I take it that not 
many others would be interested in the syntactic sugar of these 
like I am? :)


Re: vibe.d-example illustrating Dynamic Textual Web-Interface

2015-11-30 Thread Sönke Ludwig via Digitalmars-d-learn

Am 30.11.2015 um 11:08 schrieb Nordlöw:

Can somebody please show a enlightening example of, so called,

"revamp of the REST interface generator"

added to release 0.7.26 at:

http://vibed.org/blog/posts/vibe-release-0.7.26

I want to use vibe.d to add a modern dynamic web-interface to my
knowledge graph I'm currently developing.

More specifically I would like an interface that makes rich use of
dynamic HTML. When the user changes the contents of a
search/command-line text-field the client logic shall on the fly (after
a certain delay) lookup related information with vibe.d-server and
present the user with relevant hits or completions in a another part of
page (HTML div-block).

Does anybody have a similar vibe.d-project to be inspired from, in this
regard?


This would be more targeted to the web interface generator 
(vibe.web.web), which is not affected by the changes mentioned above, 
but the interface is pretty similar. For a very simple example, you can 
have a look at this:

https://github.com/rejectedsoftware/vibe.d/tree/master/examples/web_ajax


Re: Classes as enums in D?

2015-11-30 Thread Andrew LaChance via Digitalmars-d-learn
On Monday, 30 November 2015 at 07:54:49 UTC, Rikki Cattermole 
wrote:
enums don't have to be integral, but for performance reasons it 
is for the best.


enum Foo : string {
A = "a",
B = "b",
C = "d",
ERROR = "What are you talking about?"
}

void main() {
import std.stdio : writeln;
Foo foo = Foo.ERROR;
writeln(foo, " is: ", cast(string)foo);
}

Also you are welcome in #d on Freenode (IRC) if you are 
interesting in talking with the rest of us!
Btw you probably want tuples (std.typecons : tuple) to emulate 
those values.


Oh interesting.  So you are saying I could have a struct WhiteKey 
{...} and then an enum that extends WhiteKey?  You also mention 
Tuples, are you recommending the Tuple's type be (WhiteKey, int)? 
 Thanks!


Re: Classes as enums in D?

2015-11-30 Thread Meta via Digitalmars-d-learn
On Monday, 30 November 2015 at 07:48:37 UTC, Andrew LaChance 
wrote:

Hello,
D has intrigued me for a while, and I thought I would finally 
read up on it!  I've been reading "Programming in D" by Ali 
Çehreli and I've been thinking about how I can use the language 
in a side project I'm working on, porting it from java to D.  
One of the uncommonly-used features of java that I like is how 
enums can be full classes (though I don't like that there's no 
option to use enums as e.g. regular ints).  This allows several 
benefits, such as the ability to use them in switch statements 
like regular enums, the full set of objects is known at compile 
time, all objects are immutable, it's impossible to 
accidentally or purposefully create new objects of that type, 
etc...


For example (in java), if I wanted to have an enum that 
describes all the white keys on a piano keyboard and have 
members that describe the number of half-steps to the next 
white key and to the previous white key, I can define an enum 
(the "id" or enum value is implicitly defined so it doesn't 
have to be explicitly written in the definition):


enum WhiteKey
{
A(2,2),
B(2,1),
C(1,2),
D(2,2),
E(2,1),
F(1,2),
G(2,2);

private final int halfStepsToNext;
private final int halfStepsToPrevious;

WhiteKey(int halfStepsPrevious, int halfStepsNext)
{
this.halfStepsToPrevious = halfStepsPrevious;
this.halfStepsToNext = halfStepsNext;
}
}

From what I've read and seen, in D all enums have forced to 
integral types.  Is it possible to do the above in D and I have 
just missed it?  I can think of a few ways around it (such as 
statically create and define a bunch of WhiteKey structs, ...), 
but none are as clean as the above.  If this isn't something 
supported, is it on a roadmap of wanted features?


Thanks!  I'm looking forward to really getting to know the 
language.


Yes and no. You can use arbitrary types for enums in D but a lot 
of the time you shouldn't when it involves types that are not 
Plain Old Data. A naive translation would be like this:


class WhiteKey
{
private immutable int halfStepsToNext;
private immutable int halfStepsToPrevious;

enum
{
A = new WhiteKey(2, 2),
B = new WhiteKey(2, 1),
C = new WhiteKey(1, 2),
D = new WhiteKey(2, 2),
E = new WhiteKey(2, 1),
F = new WhiteKey(1, 2),
G = new WhiteKey(2, 2),
}

private this(int halfStepsToPrevious, int halfStepsToNext)
{
this.halfStepsToPrevious = halfStepsToPrevious;
this.halfStepsToNext = halfStepsToNext;
}
}

However, you do NOT want to do this, as everywhere you use 
WhiteKey's members, a new object will be created. For example:


auto f = WhiteKey.A;
auto n = WhiteKey.A;

import std.stdio;
writeln(, " ", );

This will two different addresses, because a new object is being 
created each time. It's basically taking the expression `new 
Key(2, 2)` and copy-pasting it wherever you use WhiteKey.A. 
Java's enums are basically syntax sugar for this:


class WhiteKey
{
private immutable int halfStepsToNext;
private immutable int halfStepsToPrevious;

public static WhiteKey A = new WhiteKey(2, 2);
public static WhiteKey B = new WhiteKey(2, 1);
public static WhiteKey C = new WhiteKey(1, 2);
public static WhiteKey D = new WhiteKey(2, 2);
public static WhiteKey E = new WhiteKey(2, 1);
public static WhiteKey F = new WhiteKey(1, 2);
public static WhiteKey G = new WhiteKey(2, 2);

private this(int halfStepsToPrevious, int halfStepsToNext)
{
this.halfStepsToPrevious = halfStepsToPrevious;
this.halfStepsToNext = halfStepsToNext;
}
}

This doesn't quite work in D; you'd have to make each WhiteKey 
const (which is probably not a bad idea anyway if you're using it 
like an enum). However, it's better to just do this with plain 
old value-type structs. It's exactly the same as my previous code 
defining a WhiteKey class with an embedded enum, but using a 
struct instead of a class.




Re: Classes as enums in D?

2015-11-30 Thread Mike Parker via Digitalmars-d-learn
On Monday, 30 November 2015 at 07:58:43 UTC, Andrew LaChance 
wrote:




Oh interesting.  So you are saying I could have a struct 
WhiteKey {...} and then an enum that extends WhiteKey?


enums can't *extend* anything. You can do this:

struct WhiteKeyS {
immutable int halfStepsToPrevious;
immutable int halfStepsToNext;
}

enum WhiteKey {
A = WhiteKeyS(2, 2),
B = WhiteKeyS(2, 1),
C = WhiteKeyS(1, 2)
}

void main() {
import std.stdio;
writeln(WhiteKey.A.halfStepsToPrevious);
}


Re: Classes as enums in D?

2015-11-30 Thread Rikki Cattermole via Digitalmars-d-learn

On 30/11/15 8:58 PM, Andrew LaChance wrote:

On Monday, 30 November 2015 at 07:54:49 UTC, Rikki Cattermole wrote:

enums don't have to be integral, but for performance reasons it is for
the best.

enum Foo : string {
A = "a",
B = "b",
C = "d",
ERROR = "What are you talking about?"
}

void main() {
import std.stdio : writeln;
Foo foo = Foo.ERROR;
writeln(foo, " is: ", cast(string)foo);
}

Also you are welcome in #d on Freenode (IRC) if you are interesting in
talking with the rest of us!
Btw you probably want tuples (std.typecons : tuple) to emulate those
values.


Oh interesting.  So you are saying I could have a struct WhiteKey {...}
and then an enum that extends WhiteKey?  You also mention Tuples, are
you recommending the Tuple's type be (WhiteKey, int)?  Thanks!


An enum does not extend other types.
It specifies what the type of the value will be.
It is a little like a map in that way.

I would recommend that the tuple to be the value type of the enum.
You could alias TypeTuple and use it directly and name the fields.
All it is, is a struct.


Re: Constness understanding

2015-11-30 Thread Kagamin via Digitalmars-d-learn
Unfortunately in D constant doesn't mean constant :( it means 
readonly: you can read it, but it can change in other ways. 
Immutable means constant - doesn't change in any way.


Re: having problem with `std.algorithm.each`

2015-11-30 Thread anonymous via Digitalmars-d-learn

On 30.11.2015 11:50, visitor wrote:

though i don"t understand why it fails silently ??


ref2491's original code is valid, but doesn't have the intended meaning. 
`e => {foo(e);}` is the same as `(e) {return () {foo(e);};}`, i.e. a 
(unary) function that returns a (nullary) delegate. Calling it does not 
run foo. In contrast, calling this runs foo: `e => foo(e)`.


Re: having problem with `std.algorithm.each`

2015-11-30 Thread visitor via Digitalmars-d-learn

On Monday, 30 November 2015 at 09:56:08 UTC, ref2401 wrote:

DMD 2.069.1
OS Win8.1 Enterprise


in a multiline statement, i believe you must use :
arr.each!((ref e) {
writeln(e);
++e;
})
"=>" is for oneliner

though i don"t understand why it fails silently ??


Re: Constness understanding

2015-11-30 Thread drug via Digitalmars-d-learn

On 30.11.2015 13:27, Kagamin wrote:

Unfortunately in D constant doesn't mean constant :( it means readonly:
you can read it, but it can change in other ways. Immutable means
constant - doesn't change in any way.

Thanks, considering 'const' as 'readonly' explains my case rather well.


Pixelbuffer to draw on a surface

2015-11-30 Thread TheDGuy via Digitalmars-d-learn

Hi,
is there any possibility, to draw with a pixelbuffer to a surface 
(for example with GTKD) and to update it every few milliseconds?


Re: Pixelbuffer to draw on a surface

2015-11-30 Thread lobo via Digitalmars-d-learn

On Monday, 30 November 2015 at 08:37:04 UTC, TheDGuy wrote:

Hi,
is there any possibility, to draw with a pixelbuffer to a 
surface (for example with GTKD) and to update it every few 
milliseconds?


Are any of these suitable for your needs?

https://github.com/DerelictOrg/DerelictSDL2
http://dgame-dev.de/

https://github.com/DerelictOrg/DerelictSFML2
https://github.com/DerelictOrg/DerelictAllegro5
http://api.gtkd.org/src/gdk/Cairo.html


I'm using DerelictSDL2 and Dgame for small 2D hobby games and 
loving it. Haven't used the others for D pixel rendering but I'm 
sure they're fine too.


bye,
lobo


Constness understanding

2015-11-30 Thread drug via Digitalmars-d-learn
I have some struct and other struct stores reference to the first one 
like a pointer to constant. Nevertheless I can change the value of the 
first struct. Is it some hack?


http://dpaste.dzfl.pl/0dfa3dff2df7


Re: DateTime.opBinary

2015-11-30 Thread Jonathan M Davis via Digitalmars-d-learn
On Sunday, November 29, 2015 23:53:41 Chris Wright via Digitalmars-d-learn 
wrote:
> Unfortunately, ddoc doesn't automatically cross-reference these for you,
> which results in confusion. (As if it weren't confusing enough to have
> everything wrapped in templates with filters rather than simply using
> const(Duration).)

Once TickDuration finally goes away, then functions like DateTime's opBinary
can be simplified to just take Duration.

But until TickDuration and the few things that use it in their API have gone
through the deprecation process, we're stuck with it in places like this.

- Jonathan M Davis



Re: Classes as enums in D?

2015-11-30 Thread Meta via Digitalmars-d-learn

On Monday, 30 November 2015 at 10:22:54 UTC, Marc Schütz wrote:

You're misinterpreting this:

enum X {
A = new Object,
B = new Object,
}

void main() {
import std.stdio;
writeln(cast(void*) X.A);
writeln(cast(void*) X.A);
}

# output:
470910
470910

You're print the address of `f` and `n` on the stack, not the 
reference they're pointing to.


But it's true that enums of mutable _arrays_ do create a new 
instance every time they're used:


enum X {
A = [1,2,3],
B = [4,5,6],
}

void main() {
import std.stdio;
writeln(X.A.ptr);
writeln(X.A.ptr);
}

# output:
7FD887F0E000
7FD887F0E010


Whoops, you're right. I forgot you have to cast to a pointer for 
classes.


Re: Pixelbuffer to draw on a surface

2015-11-30 Thread TheDGuy via Digitalmars-d-learn

On Monday, 30 November 2015 at 09:09:00 UTC, lobo wrote:

On Monday, 30 November 2015 at 08:37:04 UTC, TheDGuy wrote:

Hi,
is there any possibility, to draw with a pixelbuffer to a 
surface (for example with GTKD) and to update it every few 
milliseconds?


Are any of these suitable for your needs?

https://github.com/DerelictOrg/DerelictSDL2
http://dgame-dev.de/

https://github.com/DerelictOrg/DerelictSFML2
https://github.com/DerelictOrg/DerelictAllegro5
http://api.gtkd.org/src/gdk/Cairo.html


I'm using DerelictSDL2 and Dgame for small 2D hobby games and 
loving it. Haven't used the others for D pixel rendering but 
I'm sure they're fine too.


bye,
lobo

Thanks for your answer,
but there is no specific solution inside GTKD?



Re: Pixelbuffer to draw on a surface

2015-11-30 Thread visitor via Digitalmars-d-learn

but there is no specific solution inside GTKD?


for example, in pseudo code

auto surf = new Surface(...); // look for cairo.Surface doc
auto cr = cairo.Context.Context.create(surf);

// pb is an existing gdkpixbuf.Pixbuf
gdk.Cairo.setSourcePixbuf(cr, pb, width, height);
cr.paint();

adding onDraw event listener on the widget owner of the Pixbuf 
(gtk.Image for example)


Re: having problem with `std.algorithm.each`

2015-11-30 Thread visitor via Digitalmars-d-learn

On Monday, 30 November 2015 at 12:03:08 UTC, anonymous wrote:

On 30.11.2015 11:50, visitor wrote:

though i don"t understand why it fails silently ??


ref2491's original code is valid, but doesn't have the intended 
meaning. `e => {foo(e);}` is the same as `(e) {return () 
{foo(e);};}`, i.e. a (unary) function that returns a (nullary) 
delegate. Calling it does not run foo. In contrast, calling 
this runs foo: `e => foo(e)`.



`e => {foo(e);}` is the same as `(e) {return () {foo(e);};}`


Ok, Thanks ! :-)


Re: Pixelbuffer to draw on a surface

2015-11-30 Thread drug via Digitalmars-d-learn

On 30.11.2015 16:09, drug wrote:

On 30.11.2015 15:49, TheDGuy wrote:

On Monday, 30 November 2015 at 09:09:00 UTC, lobo wrote:

On Monday, 30 November 2015 at 08:37:04 UTC, TheDGuy wrote:

Hi,
is there any possibility, to draw with a pixelbuffer to a surface
(for example with GTKD) and to update it every few milliseconds?


Are any of these suitable for your needs?

https://github.com/DerelictOrg/DerelictSDL2
http://dgame-dev.de/

https://github.com/DerelictOrg/DerelictSFML2
https://github.com/DerelictOrg/DerelictAllegro5
http://api.gtkd.org/src/gdk/Cairo.html


I'm using DerelictSDL2 and Dgame for small 2D hobby games and loving
it. Haven't used the others for D pixel rendering but I'm sure they're
fine too.

bye,
lobo

Thanks for your answer,
but there is no specific solution inside GTKD?


As I know cairo is a part of GTK, so you consider using cairo as a
specific solution.

*you can consider


Re: Pixelbuffer to draw on a surface

2015-11-30 Thread drug via Digitalmars-d-learn

On 30.11.2015 15:49, TheDGuy wrote:

On Monday, 30 November 2015 at 09:09:00 UTC, lobo wrote:

On Monday, 30 November 2015 at 08:37:04 UTC, TheDGuy wrote:

Hi,
is there any possibility, to draw with a pixelbuffer to a surface
(for example with GTKD) and to update it every few milliseconds?


Are any of these suitable for your needs?

https://github.com/DerelictOrg/DerelictSDL2
http://dgame-dev.de/

https://github.com/DerelictOrg/DerelictSFML2
https://github.com/DerelictOrg/DerelictAllegro5
http://api.gtkd.org/src/gdk/Cairo.html


I'm using DerelictSDL2 and Dgame for small 2D hobby games and loving
it. Haven't used the others for D pixel rendering but I'm sure they're
fine too.

bye,
lobo

Thanks for your answer,
but there is no specific solution inside GTKD?

As I know cairo is a part of GTK, so you consider using cairo as a 
specific solution.


Re: having problem with `std.algorithm.each`

2015-11-30 Thread ref2401 via Digitalmars-d-learn

On Monday, 30 November 2015 at 12:03:08 UTC, anonymous wrote:

On 30.11.2015 11:50, visitor wrote:

though i don"t understand why it fails silently ??


ref2491's original code is valid, but doesn't have the intended 
meaning. `e => {foo(e);}` is the same as `(e) {return () 
{foo(e);};}`, i.e. a (unary) function that returns a (nullary) 
delegate. Calling it does not run foo. In contrast, calling 
this runs foo: `e => foo(e)`.


Got it. Thank you)


Re: isAllocator

2015-11-30 Thread Tofu Ninja via Digitalmars-d-learn

On Monday, 30 November 2015 at 14:21:49 UTC, Tofu Ninja wrote:
Is there something like isInputRange for allocators, I tried 
looking for something but couldn't find anything? If not, why 
not?


Aka, some way to check that type T is an allocator.


isAllocator

2015-11-30 Thread Tofu Ninja via Digitalmars-d-learn
Is there something like isInputRange for allocators, I tried 
looking for something but couldn't find anything? If not, why not?


Re: switch with enum

2015-11-30 Thread Steven Schveighoffer via Digitalmars-d-learn

On 11/25/15 12:17 AM, tcak wrote:

On Wednesday, 25 November 2015 at 03:59:01 UTC, Steven Schveighoffer wrote:

On 11/24/15 10:51 PM, tcak wrote:

I have seen a code a while ago, but even by looking at documentation, I
couldn't have found anything about it.

Let's say I have defined an enum;

enum Status: ubyte{
  Busy = 1,
  Active = 2
}

and received a ubyte value from user.

ubyte userValue;

I want to switch over userValue, but that should depend on Status.

switch( userValue ){

}

What I mean is that compiler should enforce values of enum "Status" to
be declared in switch as it would be done with "final switch", but as
you can guess, user might enter a value that is not defined by Status.
Thus, I should be able to enter the case "default" as well.


All final switch does is ensure you are covering all possible enums.
It assumes that the value is already a valid enum value. If you did
final switch on userValue, it would require you handle all 256
possible values for ubyte. So you would have to cast first.



I remember it something like switch( userValue ) with( Status ){...},
but not sure about it. Maybe it was D1 code. Is there anything like this
currently?


What this does (and yes, it should work) is make it so you don't have
to type "Status.Busy" within your case statements. You can just type
"Busy". That's all.



As far as I see, "default" case is not allowed when final switch
is used. From compiler developer's perspective, it is meaningful
and I can understand, but thinking about use cases as I have
given an example, this limitation prevents writing "tight" code.
(That is the term I could have found to express I am trying to say).


I can see what you mean -- you want the compiler to warn that you 
haven't covered all the cases, but you don't want to have the 
expectation that the program should be in an undefined state if the 
default case is executed (currently there *is* a default case in final 
switch, which is basically assert(0)).


I think this is a worthy possibility for an enhancement -- if default 
case of a final switch is provided, then it should override automatic 
"assert(0)" default case.


-Steve