Re: how to call class' template constructor

2014-10-12 Thread Cliff via Digitalmars-d-learn
On Sunday, 12 October 2014 at 19:46:41 UTC, ketmar via 
Digitalmars-d-learn wrote:

Hello.

please, how to call template constructor of a class? it's 
completely

escaped my mind. i.e. i have this class:

  class A {
this(alias ent) (string name) {
  ...
}
  }

and i want to do:

  void foo () { ... }
  auto a = new A!foo(xFn);

yet compiler tells me that

template instance A!foo A is not a template declaration, it is 
a class


yes, i know that i can rewrite constructor to something like 
this:


  this(T) (string name, T fn) if (isCallable!T) {
...
  }

and then use autodeduction, but i want the first form! ;-)


How about a static factory method?  Or do you know there is a 
syntax for invoking a templatized constructor and just can't 
remember it?


Localizing a D application - best practices?

2014-09-28 Thread Cliff via Digitalmars-d-learn
Coming from the C# world, all of localization we did was based on 
defining string resource files (XML-formatted source files which 
were translated into C# classes with named-string accessors by 
the build process) that would get included in the final 
application.  For log messages, exception messages (because 
unhandled exceptions could make it to the user in the case of a 
bug) and format strings used for the above we would create a 
string table entry and this file would eventually get localized 
by the appropriate team.


Is there a recommended pattern for applications in D that wish to 
do localization?


Thanks.


Re: Object.factory from shared libraries

2014-09-26 Thread Cliff via Digitalmars-d-learn

On Friday, 26 September 2014 at 15:45:11 UTC, Jacob Carlborg
wrote:

On 2014-09-26 16:24, krzaq wrote:

That would be satisfactory to me, except for the linux-only 
part.


In that case, I think I'll simply try to call filename() as 
the factory

function in each library - that should work everywhere, right?


Dynamic libraries only work properly on Linux. This has nothing 
to do with Object.factory.


What is the nature of D's so/dll support?  Or is there a page
describing it?


Re: can't understand why code do not working

2014-09-22 Thread Cliff via Digitalmars-d-learn

On Monday, 22 September 2014 at 20:12:28 UTC, Suliman wrote:

void worker()
{
int value = 0;
while (value =10)
{

value = receiveOnly!int();
writeln(value);
int result = value * 3;
ownerTid.send(result);
}
}

give me:

Running .\app1.exe
2
6
3
9
4
12
5
15
6
18
std.concurrency.OwnerTerminated@std\concurrency.d(234): Owner 
terminated


0x00405777 in pure @safe void 
std.concurrency.receiveOnly!(int).receiveOnly().__
lambda3(std.concurrency.OwnerTerminated) at 
C:\DMD\dmd2\windows\bin\..\..\src\ph

obos\std\concurrency.d(730)
0x0040B88D in @safe void std.concurrency.Message.map!(pure 
@safe void function(s
td.concurrency.OwnerTerminated)*).map(pure @safe void 
function(std.concurrency.O
wnerTerminated)*) at 
C:\DMD\dmd2\windows\bin\..\..\src\phobos\std\concurrency.d(

158)
0x0040B0B6 in 
D3std11concurrency10MessageBox151__T3getTDFNbNfiZvTPFNaNfC3std11co
ncurrency14LinkTerminatedZvTP8047E12172B30CAF110369CD57C78A37 
at C:\DMD\dmd2\win

dows\bin\..\..\src\phobos\std\concurrency.d(1159)


Is stdout threadsafe?


Re: can't understand why code do not working

2014-09-22 Thread Cliff via Digitalmars-d-learn

On Monday, 22 September 2014 at 21:28:25 UTC, Cliff wrote:
On Monday, 22 September 2014 at 21:24:58 UTC, monarch_dodra 
wrote:
On Monday, 22 September 2014 at 21:19:37 UTC, Steven 
Schveighoffer wrote:

On 9/22/14 4:37 PM, Cliff wrote:



Is stdout threadsafe?


Yes, stdout is thread safe, it's based on C's stdout which is 
thread safe.


-Steve


Techinallly, though thread safe, concurrent writes will 
create garbled text. D goes one step further in the sense that 
it prevents concurrent writes entirely.


The reason I ask is that the first iteration of his loop does 
not show up in stdout as presented.  I would expect 1 and 3 to 
be the first two lines of the output.


For that matter I also don't expect the 6 and 18 - nevermind, 
there must be something else going on with that loop.  He must 
have changed the loop limits.


Re: can't understand why code do not working

2014-09-22 Thread Cliff via Digitalmars-d-learn

On Monday, 22 September 2014 at 21:24:58 UTC, monarch_dodra wrote:
On Monday, 22 September 2014 at 21:19:37 UTC, Steven 
Schveighoffer wrote:

On 9/22/14 4:37 PM, Cliff wrote:



Is stdout threadsafe?


Yes, stdout is thread safe, it's based on C's stdout which is 
thread safe.


-Steve


Techinallly, though thread safe, concurrent writes will 
create garbled text. D goes one step further in the sense that 
it prevents concurrent writes entirely.


The reason I ask is that the first iteration of his loop does not 
show up in stdout as presented.  I would expect 1 and 3 to be the 
first two lines of the output.


Re: Interop with C++ library - what toolchain do you use?

2014-09-18 Thread Cliff via Digitalmars-d-learn

On Thursday, 18 September 2014 at 08:27:07 UTC, Szymon Gatner
wrote:

On Wednesday, 17 September 2014 at 22:28:44 UTC, Cliff wrote:

So I am trying to use a C++ library with D.  My toolchain is
currently Visual Studio 2013 with Visual D, using the DMD
compiler.  When trying to link, I obviously ran into the OMF 
vs.
COFF issue, which makes using the C++ library a bit of a trial 
to

say the least (I played around with some lib format converters
but perhaps unsurprisingly this led to somewhat unpredictable
behavior.)  I'd like to fix up my toolchain to avoid having 
this

issue.

For those of you who are on Windows and who do D and C++ 
interop,

what toolchain have you had success with?

Additionally, I have heard tell that D now allows calling C++
non-virtual class methods but I have not found documentation on
how to make this work (how do I define the C++ class layout in 
D

- I know how to do it for vtable entries but not non-virtual
methods.)  Pointers to docs or samples would be much 
appreciated.


Thanks!


I am using Visual Studio 2012 (in x64 bit mode that is).

Binary D distribution also comes with phobos64.lib that C++ 
executable has to link. With VisualD plugin you can just add D 
static library project to the solution and the link C++ exe to 
it. It is very easy to make back-and-forth function calls 
between C++/D. I recommend Adam Ruppe's excellent D Cookbook 
Integration chapter on the details on how to expose this for 
cross-lang usage.


It does not all work correctly tho. I reported my issues on 
learn subforum but didn't get much help. In short: it seems 
not everything in D run-time (Phobos) gets properly initialized 
even after successful rt_init() call. In my case simple call to 
writeln() causes a crash because stdout is not properly 
initialized on D'd side.


Happy hybridizing!


Thanks guys.  I do have that book, but I was unaware of the COFF
capabilities of the 64-bit DMD, I'll take a look at it.


Re: Code doesn't work - why?

2014-09-17 Thread Cliff via Digitalmars-d-learn

On Wednesday, 17 September 2014 at 21:33:01 UTC, Robin wrote:
Here is the fully working code for everyone experiencing 
similar bugs or problems with pointers and value types. =)


struct DeterministicState {
public:
	this(string name, bool isFinal, DeterministicState *[char] 
transits...) {

this.name = name;
this.finalState = isFinal;
this.addTransits(transits);
}

this(string name, bool isFinal) {
this.name = name;
this.finalState = isFinal;
}

this(bool isFinal, DeterministicState *[char] transits...) {
this(, isFinal, transits);
}

this(DeterministicState *[char] transits...) {
this(, false, transits);
}

void addTransits(DeterministicState *[char] newTransits) {
foreach (immutable key; newTransits.keys) {
transits[key] = newTransits[key];
}
}

string getName() const {
return name;
}

bool isFinalState() const {
return finalState;
}

bool hasNext(char input) const {
return (input in transits) ? true : false;
}

DeterministicState * getNext(char input) {
return transits[input];
}

string toString() const {
return name;
}

private:
string name;
DeterministicState *[char] transits;
bool finalState;
}

struct DeterministicFiniteAutomaton {
public:
DeterministicState *[] input(char[] input) {
DeterministicState *[] trace = [ start ];
auto currentState = trace[0];
foreach (immutable c; input) {
if (!currentState.hasNext(c)) {
writeln(currentState.toString() ~  has no next for  ~ 
to!string(c));

break;
} else {
writeln(currentState.toString() ~  has next for  ~ 
to!string(c));

}
currentState = currentState.getNext(c);
trace ~= currentState;
}
return trace;
}

this(DeterministicState * start) {
this.start = start;
}

private:
DeterministicState * start;
}

void main()
{
auto s0 = DeterministicState(s0, false);
auto s1 = DeterministicState(s1, false);
auto s2 = DeterministicState(s2, true);
s0.addTransits(['0' :  s1, '1' :  s2]);
s1.addTransits(['0' :  s0, '1' :  s2]);
s2.addTransits(['0' :  s2, '1' :  s2]);
auto dfa = DeterministicFiniteAutomaton( s0);
auto trace = dfa.input(0001.dup);
foreach (t; trace) {
writeln(t.toString());
}
writeln(Trace Length =  ~ to!string(trace.length));
}

Regards,
Rob


Out of curiosity, why did you decide to stick with structs
instead of simply using classes?  To avoid heap allocations?


Re: Code doesn't work - why?

2014-09-17 Thread Cliff via Digitalmars-d-learn

On Wednesday, 17 September 2014 at 21:45:01 UTC, Robin wrote:
This is actually a good question as this code isn't really 
complex or doesn't require the best possible performance.
But in case I will ever need optimum performance I should have 
learned how to handle tasks with value types which is the main 
reason why I chose them instead of reference types - for 
learning purposes.


- can't hurt! ;)

Regards,
Rob


Probably also has applicability when creating compile-time data
structures that have scope-limited lifetimes.


Interop with C++ library - what toolchain do you use?

2014-09-17 Thread Cliff via Digitalmars-d-learn

So I am trying to use a C++ library with D.  My toolchain is
currently Visual Studio 2013 with Visual D, using the DMD
compiler.  When trying to link, I obviously ran into the OMF vs.
COFF issue, which makes using the C++ library a bit of a trial to
say the least (I played around with some lib format converters
but perhaps unsurprisingly this led to somewhat unpredictable
behavior.)  I'd like to fix up my toolchain to avoid having this
issue.

For those of you who are on Windows and who do D and C++ interop,
what toolchain have you had success with?

Additionally, I have heard tell that D now allows calling C++
non-virtual class methods but I have not found documentation on
how to make this work (how do I define the C++ class layout in D
- I know how to do it for vtable entries but not non-virtual
methods.)  Pointers to docs or samples would be much appreciated.

Thanks!


Re: compile automation, makefile, or whatever?

2014-09-16 Thread Cliff via Digitalmars-d-learn

On Tuesday, 16 September 2014 at 19:00:05 UTC, K.K. wrote:

Hey I have a quick question: Does D have it's own version of
makefiles or anything (preferably simpler)?
So instead of typing in PowerShell dmd file1.d file2.d
lib\foo.lib -Isrc\ . I could just type most of that into a
file and then just type dmd file.X

I've seen some people make really complex .d files that have a
lot of interchangeability but at the moment I wouldn't really
need something of that scale. Also, I'm not using DUB; I'd 
prefer

to just use the command line.

..Can pragma's help with this, aside from linking just the libs?


I want to say somewhere on the forums are some descriptions of
using CMake for this.  Might try searching for that.


Re: compile automation, makefile, or whatever?

2014-09-16 Thread Cliff via Digitalmars-d-learn

On Tuesday, 16 September 2014 at 20:29:12 UTC, K.K. wrote:

On Tuesday, 16 September 2014 at 19:26:29 UTC, Cliff wrote:

I want to say somewhere on the forums are some descriptions of
using CMake for this.  Might try searching for that.


Yeah I just looked up the CMake thing. It definitely seems worth
playing with, though I'm not really sure how extensive it's D
support currently is :S


Out of curiosity, why are you not using dub (on the command-line)?


Re: compile automation, makefile, or whatever?

2014-09-16 Thread Cliff via Digitalmars-d-learn

On Tuesday, 16 September 2014 at 20:45:29 UTC, K.K. wrote:

On Tuesday, 16 September 2014 at 20:31:33 UTC, Cliff wrote:
Out of curiosity, why are you not using dub (on the 
command-line)?


I'm not against using it or anything, but I've found that it
didn't help me significantly nor did I have the patience to
figure out it's whole set of issues, D by itself is already
enough trouble xD

Plus with my spastic work style, it kinda slowed me down.

However, it is something I may consider when I have an actually
organized project with a final goal. A lot of what I'm doing
right now is just experiments. Though, if Cmake + D does the
trick then I might not use DUB in the end. Hard to say atm.


Would you be willing to provide some more detail on what about it
you didn't like (errors, missing features, etc.)?  I ask because
build systems are a particular interest of mine and I have
projects in this area which can always use more user data.


Re: compile automation, makefile, or whatever?

2014-09-16 Thread Cliff via Digitalmars-d-learn

On Tuesday, 16 September 2014 at 21:05:18 UTC, K.K. wrote:

On Tuesday, 16 September 2014 at 20:53:08 UTC, Cliff wrote:
Would you be willing to provide some more detail on what about 
it
you didn't like (errors, missing features, etc.)?  I ask 
because

build systems are a particular interest of mine and I have
projects in this area which can always use more user data.


I'll try, but I haven't used it at all since maybe.. April?

One of the main things that annoyed me about it was how 
sensitive

it could be. The best comparison I can give is that it reminded
me ALOT of Haxe. Both are very flimsy, and very poorly
documented. (Nothing beats a good manual as far as I'm 
concerned!)


The other thing, as I briefly mentioned, was it really didn't
speed anything up, unless maybe you were working on a larger
project.


Obviously I'm not a master of any sort, but the main point I'd
take from this is it wasn't inviting.

Hope that helps a bit :3


Yep, that's useful information to me.  Over the years I have
found that build systems *generally* tend to be uninviting.  My
suspicion is that comes down to a few reasons:

1. Builds end up being a LOT more complicated that you would
expect as soon as you step out of a single project with a few
source files and default options
2. Build tooling is typically built and maintained by people who
end up being relatively specialist - either they are part of the
small cabal of people who know the tooling intimately, or they
have been forced into it and know just enough to get by for their
organization.
3. Most build tooling is designed to solve a particular subset of
actual build-related problems, with much less though given to how
it fits holistically into the entire developer workflow.
4. Build tooling is almost never treated like an actual product -
documentation is written for wizards, not lay-people.

As a result, the casual user is a bit SOL.

(NOTE: This is not a rant specifically aimed at DUB, but my
general observation on the state of build tooling.)


Re: Idiomatic async programming like C# async/await

2014-09-14 Thread Cliff via Digitalmars-d-learn

On Sunday, 14 September 2014 at 09:19:11 UTC, Kagamin wrote:

On Friday, 12 September 2014 at 03:59:58 UTC, Cliff wrote:
...but std.parallelism.Task requires parameterization on the 
function which the task would execute - that is clearly an 
implementation detail of the store.


I think, you can wrap the Task in a class.

abstract class CTask
{
  abstract void wait();
}

abstract class CTask(TResult)
{
  abstract TResult result();
}

class CTTask(TTask): CTask(TResult)
{
  TTask task; //std.parallelism.Task
  override void wait(){ ... }
  override TResult result(){ ... }
}


Yep, that's what I figured.  Thanks :)



Re: Idiomatic async programming like C# async/await

2014-09-12 Thread Cliff via Digitalmars-d-learn

On Friday, 12 September 2014 at 07:15:33 UTC, Kagamin wrote:
async/await is not so much about futures/promises, but 
optimization of IO-bound operations, i.e. when you wait on 
network/disk, you don't consume stack, threads and similar 
resources, an analog in D is vibe.d


I should have been more clear - it's not the async/await bit I am
interested in so much as the Task behavior - that I have some
object which represents the (future) completed state of a task
without the recipient of that object having to know what the type
of the task function is as they are only interested in the task
result.

I'll take a closer look at vibe.d and see if they already have a
system representing this before I cook up my own.


Idiomatic async programming like C# async/await

2014-09-11 Thread Cliff via Digitalmars-d-learn

(New to D, old hand at software engineering...)

I come from .NET and have made heavy use of the async/await 
programming paradigm there.  In particular, the Task mechanism 
(futures/promises) lets one encapsulate the future result of some 
work and pass that around.  D seems to have something similar in 
std.parallelism.Task, but this seems to additionally encapsulate 
and expose the actual work to do.


What I want to do is be able to define an interface that performs 
certain possibly-slow operations and presents a Task-based 
interface.  Example in C#:


interface MyStore
{
TaskKey Store(byte[] content);
Taskbyte[] Retrieve(Key key);
}

What I feel like I *want* to do in D is something roughly similar:

interface MyDStore
{
Task!Key Store(InputRange!ubyte content);
Task!(InputRange!ubyte) Retrieve(Key key);
}

...but std.parallelism.Task requires parameterization on the 
function which the task would execute - that is clearly an 
implementation detail of the store.


What is the correct D idiom to use in this case?