Re: Unmanaged drop in replacemet for [] and length -= 1

2016-06-18 Thread Joerg Joergonson via Digitalmars-d-learn

Also, how to handle foreach(i, x; w) (use index + value)?





Unmanaged drop in replacemet for [] and length -= 1

2016-06-18 Thread Joerg Joergonson via Digitalmars-d-learn
I wanted to switch to std.container.Array but it doesn't seem to 
mimic [] for some odd ball reason. I threw this class together 
and it seems to work.


The only problem is that I can't do

carray.length -= 1;

I can't override `-=` because that is on the class. can I 
override it for length somehow or do I have to create a length 
wrapper class that has it overridden in it? Or is there a way to 
do it in cArray?


Basically I want to support code that does something like

auto x = [];
x.length -= 1;

and not have to rewrite that to x.length = x.length - 1;




public class cArray(T)
{
Array!T data;

public void assumeSafeAppend() { };

public @property int length()
{
return data.length;
}

public @property int length(int len)
{
for(int i = 0; i < len; i++)
data.removeBack();
return data.length;
}

ref T opIndex(int i) { return data[i]; }
@property int opDollar(size_t dim : 0)() { return 
data.length; }


this() { data = Array!T(); }


int opApply(int delegate(ref T) dg)
{
int result = 0;

for (int i = 0; i < data.length; i++)
{
result = dg(data[i]);
if (result)
break;
}
return result;
}

int opApplyReverse(int delegate(ref T) dg)
{
int result = 0;

for (int i = 0; i < data.length; i++)
{
result = dg(data[i]);
if (result)
break;
}
return result;
}


void opOpAssign(string op)(T d)
{
if (op == "~")
{
data ~= d;
}
}

bool canFind(T)(T d)
{
for(int i = 0; i < data.length; i++)
{
if (data[i] == d)
return true;
}
return false;
}
}


Re: canFind doesn't work on Array, replacing [] with array doesn't work, etc...

2016-06-18 Thread Joerg Joergonson via Digitalmars-d-learn

On Saturday, 18 June 2016 at 17:46:26 UTC, ag0aep6g wrote:
On Saturday, 18 June 2016 at 17:02:40 UTC, Joerg Joergonson 
wrote:
3. can't use canFind from algorithm. Complains it can't find a 
matching case. I tried many variations to get this to work.


canFind takes a range. Array isn't a range itself, but you can 
get one by slicing it with []:



import std.container.array;
import std.algorithm;
void main()
{
Array!int a = [1, 2, 3];
assert(a[].canFind(2));
assert(!a[].canFind(0));
}



Thanks. I've decided to implement a wrapper around Array so it is 
a drop in replacement for [].







Re: How to get access to Voldemort / private thingies

2016-06-18 Thread Johan Engelen via Digitalmars-d-learn

On Saturday, 18 June 2016 at 17:50:51 UTC, cy wrote:

On Saturday, 18 June 2016 at 08:41:30 UTC, Johan Engelen wrote:
Without going in too much detail, the problem is that I am not 
linking to opaque .o files.


The problem is the compiler has to assume you *might* be 
linking to opaque .o files, so it can't provide any 
introspection capabilities. There's no way to tell which 
"hidden type" that the getObject function is returning, since 
that's decided in the (possibly opaque) function body.


The function body is not opaque. In my compilation unit, the 
compiler knows it, and that's exactly why I run into troubles. 
For ease of discussion, just pretend the function that returns 
the Voldemort type is in the same file as the user code: let's 
say it's all in one source file.
Just like there is a way to get private types out of structs 
(using .tupleof or the Fields!Strukt[*] thing I pasted above), I 
was hoping there was a way to get a Voldemort type out of a 
function.


This is of course all a big hack. The pragma(mangle,..) thing 
works well, but unfortunately conflicts with the LDC inlining 
improvement I'm working on.
Like you say, I could copy the whole function definition, but it 
would mean copying quite a lot and some more types etc...


- Johan



Re: How to group similar member functions from different classes?

2016-06-18 Thread kinke via Digitalmars-d-learn

On Saturday, 18 June 2016 at 07:03:25 UTC, cy wrote:
So how would you do it? Defining A.foo, B.foo, etc in one 
place, and A.bar, B.bar, etc in another?


No such thing in D. But you can always be creative and use an 
overloaded helper function containing the actual implementation 
if you want to group the code by functionality and not by type:


class A { int foo() { return doFoo(this); }
class B : A { int foo() { return doFoo(this); }

...

void doFoo(A a) { ... }
void doFoo(B b) { ... }

If you need full access to the members, the helpers would need to 
be in the same module, otherwise you can put them into a separate 
helper.


Re: How to group similar member functions from different classes?

2016-06-18 Thread cy via Digitalmars-d-learn

On Saturday, 18 June 2016 at 07:03:25 UTC, cy wrote:
So how would you do it? Defining A.foo, B.foo, etc in one 
place, and A.bar, B.bar, etc in another?


The only thing I've been able to figure is a horrible hack, where 
your member functions are something like


// off in define_foos.d

template foo_for(T) {
  static if(is(T == A)) {
enum foo_for = q{
  int foo () {
return bar+42;
  }
};
  } else static if(is(T == B)) {
enum foo_for = q{
  int foo () {
return bar+23;
  }
   };
  }
}

// in classes.d

import define_foos: foo_for;

struct A {
int bar;
mixin(foo_for!A);
}

struct B {
int bar;
mixin(foo_for!B);
}

// etc

void main() {
import std.stdio;
A a = A(0);
B b = B(1);
writeln(b.foo());
writeln(a.foo());
}


Re: How to get access to Voldemort / private thingies

2016-06-18 Thread cy via Digitalmars-d-learn

On Saturday, 18 June 2016 at 08:41:30 UTC, Johan Engelen wrote:
Without going in too much detail, the problem is that I am not 
linking to opaque .o files.


The problem is the compiler has to assume you *might* be linking 
to opaque .o files, so it can't provide any introspection 
capabilities. There's no way to tell which "hidden type" that the 
getObject function is returning, since that's decided in the 
(possibly opaque) function body.


You could hack something with debugging, I suppose. (Probably 
not. DWARF/ptrace is ridiculously undocumented.) Perhaps you 
could make an alternate version of the function with the same 
source code, that returns auto instead of an Object type? As in...


Object evil() {
  class Vold {...}
  Vold v = new Vold(...);
  return v;
}


auto notevil() {
  static class Vold {...}
  Vold v = new Vold(...);
  return v;
}

...

auto hack = new typeof(notevil());

That can magically allow you to return type Vold, even though 
it's an error to say


Vold notevil() {
  static class Vold { ... }
  ...
}


Re: Templated class defaults and inheritence

2016-06-18 Thread Joerg Joergonson via Digitalmars-d-learn


This is solved through simple inheritance constraints and 
aliasing with qualification.



class X;
class subfoo;
class subbaz : subfoo;
class foo(T) if (is(T : subfoo)) X;
class baz(T) if (is(T : subbaz)) foo!T;

then when we need foo with "default",

alias foo = qualified.foo!subfoo;

Without the qualification, which I guess requires having this 
stuff in a separate module, there is no conflict. alias foo = 
foo!subfoo; fails circularly.










Re: canFind doesn't work on Array, replacing [] with array doesn't work, etc...

2016-06-18 Thread ag0aep6g via Digitalmars-d-learn

On Saturday, 18 June 2016 at 17:02:40 UTC, Joerg Joergonson wrote:
3. can't use canFind from algorithm. Complains it can't find a 
matching case. I tried many variations to get this to work.


canFind takes a range. Array isn't a range itself, but you can 
get one by slicing it with []:



import std.container.array;
import std.algorithm;
void main()
{
Array!int a = [1, 2, 3];
assert(a[].canFind(2));
assert(!a[].canFind(0));
}



canFind doesn't work on Array, replacing [] with array doesn't work, etc...

2016-06-18 Thread Joerg Joergonson via Digitalmars-d-learn

Have working code that uses []. Trying to replace with Array!

1. Can't use make in field initialization. Complains about malloc 
in static context.


2. can't decrement the length. So Darr.length = Darr.length - 1; 
fails
This means we can't remove the element easily. I see a removeBack 
but since I can't get the code to compile I don't know if this is 
a replacement. If so, why now allow setting the length?


can't set the length to 0. Why not? Why not just allow length to 
be set?


3. can't use canFind from algorithm. Complains it can't find a 
matching case. I tried many variations to get this to work.





Re: Can anybody install DDT on Eclipse Neon or Mars?

2016-06-18 Thread Seb via Digitalmars-d-learn

On Saturday, 18 June 2016 at 16:46:26 UTC, Mark wrote:
I've spent may hours trying to do this in OSX. Everything goes 
fine from the marketplace window...until I restart Eclipse and 
find no files have been added?


Any words of consolation or advice will be greatly appreciated.

Desperately,

Mark


Unfortunately I can't help you any DDT, but are you sure you 
really need this?
D is so simple that basically every plain  text editor works 
great. If you need a bit more convenience you might have a look 
for options here:

https://wiki.dlang.org/IDEs


Can anybody install DDT on Eclipse Neon or Mars?

2016-06-18 Thread Mark via Digitalmars-d-learn
I've spent may hours trying to do this in OSX. Everything goes 
fine from the marketplace window...until I restart Eclipse and 
find no files have been added?


Any words of consolation or advice will be greatly appreciated.

Desperately,

Mark


Re: Is it possible to create a static factory method on a templated struct?

2016-06-18 Thread ketmar via Digitalmars-d-learn

On Saturday, 18 June 2016 at 16:05:53 UTC, Gary Willoughby wrote:

I've tried the following code and I get the error:


you still have to instantiate you `Foo` here:

  auto foo = Foo.of!(string);

no, you can't call even static methods of *uninstantiated* 
template. uninstantiated template doesn't exist at all. this 
works, for example:


  auto foo = Foo!int.of!(string);


Re: ARSD PNG memory usage

2016-06-18 Thread Joerg Joergonson via Digitalmars-d-learn

On Saturday, 18 June 2016 at 02:01:29 UTC, Adam D. Ruppe wrote:
On Saturday, 18 June 2016 at 01:20:16 UTC, Joerg Joergonson 
wrote:
Error: undefined identifier 'sleep', did you mean function 
'Sleep'?		


"import core.thread; sleep(10);"


It is `Thread.sleep(10.msecs)` or whatever time - `sleep` is a 
static member of the Thread class.



They mention to use PeekMessage and I don't see you doing 
that, not sure if it would change things though?


I am using MsgWaitForMultipleObjectsEx which blocks until 
something happens. That something can be a timer, input event, 
other message, or an I/O thing... it doesn't eat CPU unless 
*something* is happening.


Yeah, I don't know what though. Adding Sleep(5); reduces it's 
consumption to 0% so it is probably just spinning. It might be 
the nvidia issue that creates some weird messages to the app.


I'm not too concerned about it as it's now done to 0, it is 
minimal wait time for my app(maybe not acceptable for performance 
apps but ok for mine... at least for now).


As I continue to work on it, I might stumble on the problem or it 
might disappear spontaneously.




Re: Templated class defaults and inheritence

2016-06-18 Thread Joerg Joergonson via Digitalmars-d-learn

On Saturday, 18 June 2016 at 12:15:56 UTC, Klaus Kalsesh wrote:
On Saturday, 18 June 2016 at 02:11:23 UTC, Joerg Joergonson 
wrote:

I have something like

class X;
class subfoo : X;
class subbaz : X;

class foo : X
{
subfoo bar;
}

class baz : X;


which I have modified so that

class subbaz : subfoo;
class baz : foo;

(essentially baz is now a derivation of foo while before it 
was of X)


the problem is that subbaz uses subfoo bar; when it also needs 
to use a derived type. (so it is a full derivation of foo and 
subfoo)



To accomplish that I parameterized foo so I can do

class foo!T : X
{
T bar;
}


and I can now do

class baz : foo!subbaz;


There are two problems with this though:


1. How can I create a default foo!(T = subfoo) so I can just 
instantiate classes like new foo() and it is the same as 
foo!subfoo()? I tried creating a class like class foo : 
foo!subfoo; but I get a collision. I guess an alias will work 
here just fine though?(just thought of it)


You must declare an alias:

alias FooSubfoo = foo!subfoo;
FooSubfoo fsf = new FooSubfoo;



No, this is not what I'm asking

I would want something like

alias foo = foo!subfoo;

Not sure, though, if a when I instantiate like

new foo();

If the compiler will understand it is new foo!subfoo(); I see no 
problem here but haven't tested it.




2. The real problem is that baz isn't really a true derivation 
of foo like it should be. foo!subfoo and foo!subbaz are 
different types. I want the compiler to realize that 
foo!subbaz(and hence baz) is really a derived foo!subfoo and 
ultimately X.


For multiple inheritence in classes, the standard way of doing 
is with interfaces.




This is not multiple inheritance and alias this won't work.

Let me explain better:

X -> foo%subfoo
  -> baz%subbaz

by -> I mean inherits and by %, I mean "uses"(say, as a field or 
method parameter or whatever)


This then says that foo uses subfoo and is derived from X. 
Similarly for baz.


These are two distinct types(hence the two different lines)

Now, if baz inherits from foo instead of X, we have

X -> foo%subfoo -> baz%subfoo

But if subfoo -> subbaz, then we should be able to do

X -> foo%subfoo -> (baz%subfoo) -> baz%subbaz.

Note the are now on the same line. baz%subbaz is a derived type 
of foo%subfoo, not just X as in the first two line case.


This is an important distinction in the type system.


It's sort of multiple inheritance in that multiple types are used 
but each type only inherits once.


In C# we have the "where" keyword that lets us tell the compiler 
something like "where subbaz inherits from subfoo".




Dlang once had a page that had ways to express stuff like this 
but I can no longer find it ;/


It might be a syntax like

class baz!T : foo!(T : subfoo);

which may say "T must be derived from subfoo". Then baz!subbaz 
would work and it would be a derived type of foo!subfoo(rather 
than just X).









Is it possible to create a static factory method on a templated struct?

2016-06-18 Thread Gary Willoughby via Digitalmars-d-learn

I've tried the following code and I get the error:

Error: template Foo(A) does not have property 'of'


struct Foo(A)
{
private int _foo;

@disable this();

public this(int foo)
{
this._foo = foo;
}

public static auto of(B)()
{
return Foo!(B)(8);
}
}

void main(string[] args)
{
auto foo = Foo.of!(string);
}


Is it possible to even have static methods on structs like this? 
What am I doing wrong?


Re: How to enable feedback for AssertError?

2016-06-18 Thread your_name via Digitalmars-d-learn

Sorry for the necromancy.

I failed to reproduce the issue for single threaded programs. 
Which is good.


As far as std.concurrency is concerned I still have questions.
1. How does the main thread of a program print an 
(Assert)Error/Exception when it is terminated by it ?


2. Where is the place to make a suggestion for a documentation 
update for std.concurrency?
 For someone unware to this pitfall it can be quite a time waster 
so a Notice in the docs would be nice.


Thanks for the help.


Re: Is there a more elegant way of testing T for multiple types?

2016-06-18 Thread ag0aep6g via Digitalmars-d-learn

On 06/18/2016 04:37 PM, Gary Willoughby wrote:

Here I'm testing T is either a class or interface:

void foo(T)(T bar) if (is(T == class) || is(T == interface))
{
 ...
}

Is there a more elegant way of testing T for multiple types? Because it
doesn't scale well if I need to add more.

I would love to use something like this:

void foo(T)(T bar) if (T in [class, interface])
{
 ...
}

Is something like this currently possible using type tuples?


There's std.meta.staticIndexOf [1]. Use it like so:

void foo(T)(T bar) if (staticIndexOf!(T, int, float) >= 0) { ... }

Doesn't work with `class` or `interface`, though, because those are not 
types.




[1] https://dlang.org/phobos/std_meta.html#.staticIndexOf


Is there a more elegant way of testing T for multiple types?

2016-06-18 Thread Gary Willoughby via Digitalmars-d-learn

Here I'm testing T is either a class or interface:

void foo(T)(T bar) if (is(T == class) || is(T == interface))
{
...
}

Is there a more elegant way of testing T for multiple types? 
Because it doesn't scale well if I need to add more.


I would love to use something like this:

void foo(T)(T bar) if (T in [class, interface])
{
...
}

Is something like this currently possible using type tuples?


Re: Vanilla Vim with D

2016-06-18 Thread Dlangofile via Digitalmars-d-learn

On Saturday, 18 June 2016 at 12:36:04 UTC, Adam D. Ruppe wrote:

On Saturday, 18 June 2016 at 07:55:41 UTC, Dlangofile wrote:

- compile from inside (rdmd or dmd right now)
- jump in the code fixing the compilation errors?


:set makeprg=rdmd\ %


Do that to setup, then just

:make

to tell it to run that command and jump around to the errors it 
sees.


Thank you Adam, so I'm going to learn about the quickfix...



Re: Vanilla Vim with D

2016-06-18 Thread Adam D. Ruppe via Digitalmars-d-learn

On Saturday, 18 June 2016 at 07:55:41 UTC, Dlangofile wrote:

- compile from inside (rdmd or dmd right now)
- jump in the code fixing the compilation errors?


:set makeprg=rdmd\ %


Do that to setup, then just

:make

to tell it to run that command and jump around to the errors it 
sees.


Re: Templated class defaults and inheritence

2016-06-18 Thread Klaus Kalsesh via Digitalmars-d-learn

On Saturday, 18 June 2016 at 02:11:23 UTC, Joerg Joergonson wrote:

I have something like

class X;
class subfoo : X;
class subbaz : X;

class foo : X
{
subfoo bar;
}

class baz : X;


which I have modified so that

class subbaz : subfoo;
class baz : foo;

(essentially baz is now a derivation of foo while before it was 
of X)


the problem is that subbaz uses subfoo bar; when it also needs 
to use a derived type. (so it is a full derivation of foo and 
subfoo)



To accomplish that I parameterized foo so I can do

class foo!T : X
{
T bar;
}


and I can now do

class baz : foo!subbaz;


There are two problems with this though:


1. How can I create a default foo!(T = subfoo) so I can just 
instantiate classes like new foo() and it is the same as 
foo!subfoo()? I tried creating a class like class foo : 
foo!subfoo; but I get a collision. I guess an alias will work 
here just fine though?(just thought of it)


You must declare an alias:

alias FooSubfoo = foo!subfoo;
FooSubfoo fsf = new FooSubfoo;


2. The real problem is that baz isn't really a true derivation 
of foo like it should be. foo!subfoo and foo!subbaz are 
different types. I want the compiler to realize that 
foo!subbaz(and hence baz) is really a derived foo!subfoo and 
ultimately X.


For multiple inheritence in classes, the standard way of doing is 
with interfaces.


So instead of a template:

class foo: interfaceThis, interfaceThat
{}


alias this can work too but there only can be one:

class foo!T : X
{
T bar;
alias bar this;
}

This pattern is called the "Curiously recurring template" BTW. 
With the alias this it's almost usable, which was not the case in 
the original form (because you have an indirection to access the 
derived type that's indicated by the template parameter.
Actually from the machine code POV it's still the case but in the 
source code it's hidden by the alias this shortcut.








Re: How to get access to Voldemort / private thingies

2016-06-18 Thread Johan Engelen via Digitalmars-d-learn

Someone figured out how to do it and put it in std.traits! ;-)

Example:
```
import std.stdio;
import core.thread;
import std.traits;

void main()
{
Fields!Thread[11] a;
writeln(typeid(a));
}
```
This prints "core.thread.Thread.Context" , which is a private 
struct type of core.thread.Thread.

Success! :-)

-Johan


Re: How to get access to Voldemort / private thingies

2016-06-18 Thread Johan Engelen via Digitalmars-d-learn

On Friday, 17 June 2016 at 21:07:31 UTC, cy wrote:

On Friday, 17 June 2016 at 19:49:18 UTC, Johan Engelen wrote:

Hi all,
  Is there another way to get access to Voldemort class 
methods, or private class members, other than using


[... snip ...]

Because of the guarantee that you can link to opaque .o files,


Thanks for your response.
Without going in too much detail, the problem is that I am not 
linking to opaque .o files. The user symbol has to have the same 
type (as the hidden type) internally in the compiler; both will 
be present in the same translation unit because of inlining, and 
so if the mangled names are the same, their types have to be the 
same.


Hmm...
-Johan


Re: Variadic function with parameters all of a specific type

2016-06-18 Thread Nordlöw via Digitalmars-d-learn

On Friday, 17 June 2016 at 21:20:01 UTC, Timon Gehr wrote:

void foo(T[] a...)@nogc{}

void bar()@nogc{
foo(1,2,3);
}


Ahh, cool. I was completely unaware of this feature.

Doc here: 
https://dlang.org/spec/function.html#typesafe_variadic_functions


Vanilla Vim with D

2016-06-18 Thread Dlangofile via Digitalmars-d-learn

Hi to all,

I'm trying to approach Vim as an editor for D, starting from 
scratch...
I've a pretty vanilla vimrc, and only CtrlP as a plugin: not a so 
bad experience right now!


First question, what's the best, simplest and fast approach for:

- compile from inside (rdmd or dmd right now)
- jump in the code fixing the compilation errors?

Thanks vim guys!

/DF


Re: vibe.d - asynchronously wait() for process to exit

2016-06-18 Thread cy via Digitalmars-d-learn

On Friday, 17 June 2016 at 13:53:15 UTC, Vladimir Panteleev wrote:

Geod24 on IRC suggested signalfd + createFileDescriptorEvent. I 
think this would work, but isn't it possible to wrap the fd 
returned by signalfd into a Vibe.d stream and read it directly? 
I'm just not sure how.


Well, vibe.d streams are defined as interfaces, so you'd have to 
import vibe.core.stream: InputStream, and create a SignalFdInput 
class that implemented all the required methods. When it requires 
you to "wait" for data available, you save 
core.task.Task.getThis() somewhere, and... basically do what 
you're doing with createFileDescriptorEvent, just resuming the 
task instead of handling the event in a callback.


I should point out that createFileDescriptorEvent is an assert(0) 
for libasync.


How to group similar member functions from different classes?

2016-06-18 Thread cy via Digitalmars-d-learn

When I define functions like:

class A {
  abstract void format(...) {...}
}

class B : A {
  void format(...) {...}
}

class C : A {
  void format(...) {...}
}

and so on, often these different member functions all share a lot 
in common. Maybe they are the only ones that require formatting 
modules, that do I/O, that do string manipulation, that sort of 
thing.


But if I made a second function, say for instance clone() or 
replaceWithDucks(), it too might import a lot of modules, and 
perform a lot of logic. And there may be many, many different 
types of object here.


In C++ I could forward declare the member functions, then put all 
"::format(...)" member functions together in their own source 
file, making everything pretty neat and tidy. How do I do that in 
D? As near as I can tell, you can only define member functions 
inside the class definition itself, and you can't add to that 
definition piece-wise like


// file 1
class A ... {
  void format(...) { ... }
}
...

// file 2
class A ... {
 void doathing() { ... }
}
...

So how would you do it? Defining A.foo, B.foo, etc in one place, 
and A.bar, B.bar, etc in another?