Re: reading file byLine

2015-09-12 Thread deed via Digitalmars-d-learn

On Saturday, 12 September 2015 at 12:51:04 UTC, Namal wrote:
Anyway, there is no .reverse for strings I guess, what is the 
way to completely reverse a string in D?


What do you want to do? Do you want to keep your data in original 
order, but get a reversed view of it for something, or do you 
actually want to reverse your original array?


You can use `retro` to simply read your array backwards, i.e.:
string s = "Some text";
s.retro.writeln;   // `txet emoS`
s.writeln; // `Some text`
s.retro.find("e"); // `Some te` (Surprising to me. Error? 
2.067.1)

s.retro.until("e").writeln;// `tx`
s.find("e");   // `Some`

The string is still kept in original order, but `retro` returns a 
range reading the array backwards. If you want to store a 
reversed string in memory, that's possible too, of course. One 
way of doing it, is to convert your retro range to a string:


s = s.retro.to!string;
s.writeln; // `txet emoS`

This will allocate new memory for the reversed copy and assign it 
to `s`.
Reverse is an algorithm that swaps values at different indices 
and since `string` is an alias for `const(char)[]`, it's not 
allowed. It means that each element of the array is const, so you 
cannot mutate any elements, but you can append or remove elements 
or assign the slice to view another part of the string or some 
other string. Hence, a `s.reverse` will give you an error. If you 
use `char[]` instead of `const(char)[]` you can use reverse and 
reuse the same memory for the reversed `char[]`. To illustrate:


char[] t = ['a', 'b', 'c'];
std.algorithm.reverse(t);
t.writeln; // `cba`

// s[0] = s[$-1];  // Error, cannot mutate const elements
auto r = s.retro;
s.length = 0;
r.each!(e => s ~= e);
s.writeln; // s has the reversed string, obtained 
through
   // a temporary range object, setting 
length to
   // zero and appending the elements 
from the
   // range, which is allowed for 
const(char)[]


Re: Does a synchronization yield on waiting thread?

2015-09-12 Thread Steven Schveighoffer via Digitalmars-d-learn

On 9/12/15 10:48 PM, Prudence wrote:

It would seem to be the logical thing to do?

That is, suppose two threads are sharing a resource. Thread A has it
locked. B is "waiting". Is B in a loop burning cycles running in the
background(regardless of thread.sleep, which only alleviates the
problem) or does it yield completely and somehow inform the lock to
resume it when A has unlocked the resources?


If you are using D mutexes or synchronized statements, it uses the OS' 
mechanisms (e.g. pthreads). For all supported OSes, this means it is 
asleep and waiting for the OS to awaken it when it has locked the resource.



The first one burns cycles and can have timing problems. I.e., What if A
locks and unlocks at the same rate that B checks? (I suppose a random
sleep time would help with this) (


You don't have to worry about this. If A unlocks a resource that B is 
waiting for the lock, it cannot lock it again before B gets it.


-Steve


Does a synchronization yield on waiting thread?

2015-09-12 Thread Prudence via Digitalmars-d-learn

It would seem to be the logical thing to do?

That is, suppose two threads are sharing a resource. Thread A has 
it locked. B is "waiting". Is B in a loop burning cycles running 
in the background(regardless of thread.sleep, which only 
alleviates the problem) or does it yield completely and somehow 
inform the lock to resume it when A has unlocked the resources?


The first one burns cycles and can have timing problems. I.e., 
What if A locks and unlocks at the same rate that B checks? (I 
suppose a random sleep time would help with this) (


"Yielding", OTOH, has B burn no cycles waiting in a loop. This 
can lead to optimization and prioritization and all that(after an 
unlock, all the threads waiting can be called, but in what order).


Obviously yielding is more complex and requires the threads kept 
track of(an array for each lock/unlock pair) but far more 
efficient.


I'm hoping D does this, but not holding my breath.





Re: Adjacent Pairs Range

2015-09-12 Thread deed via Digitalmars-d-learn

On Saturday, 12 September 2015 at 10:17:19 UTC, Nordlöw wrote:
How do I most elegantly iterate all the adjacent pairs in an 
`InputRange` using Phobos?


Something like

[1,2,3,4] => [(1,2), (2,3), (3,4)]


Why not just:

zip(arr[0 .. $-1], arr[1 .. $])

?


Re: Massive linker error after upgrade to DMD 2.068.1-1

2015-09-12 Thread Nicholas Wilson via Digitalmars-d-learn

On Saturday, 12 September 2015 at 20:05:28 UTC, rcorre wrote:
After upgrading from DMD 2.068.0-1 to DMD 2.068.1-1, my project 
began producing a large linker error (when built using dub).


I was able to trace it down to a single line:

target = target.adjacent(Diagonals.yes).randomSample(1).front;

target is of type RowCol 
(https://github.com/rcorre/dtiled/blob/master/src/dtiled/coords.d#L36),
which has an `adjacent` property that leverages chain, only, 
and take.


When I comment this line out (or build on a system with DMD 
2.068.0-1), I can build fine. When uncommented, I see (post 
ddemangle):


http://dpaste.com/1PJB35V

I've tried to break this down into a reduced example to prove 
that I can call randomSample on the range returned by 
RowCol.adjacent (http://dpaste.com/13G9WDE). This runs fine, so 
it seems to be a deeper issue with my build environment (full 
project at https://github.com/rcorre/damage_control if you're 
curious).


I don't want to turn this into "please debug my project for 
me", but do people have general hints on how to track down 
issues like this?


Thanks!

Side note: I see src/transition.d in the output, but I don't 
see how it is relevant to the error (it isn't even imported by 
the file causing the error).


there was talk of adding symbol name compression some time ago ( 
to reduce lib size ( and maybe make ddemangle not fail on long 
syms?). This might be the cause of your problems i.e. the new 
compiler emitting references to compressed names but built with 
the old compiler using the not compressed ones. Try rebuilding 
everything and see if that fixes it.


Re: shared array?

2015-09-12 Thread Laeeth Isharc via Digitalmars-d-learn

On Saturday, 12 September 2015 at 13:42:44 UTC, Prudence wrote:
Saying that it doesn't use it most of the time is not an 
answer/solution. Using it at all is a problem because one 
doesn't know when and where. I realize there is a switch 
now(-vgc), and maybe that is the solution, but you say "well, 
phobos only uses 0.01% on the GC", yet since you either don't, 
can't, or won't know where that is, then it might as well be 
100% if you would like to potentially get off the GC one day.


"you either don't, can't, or won't know where that is"

just check the signature, no ? eg

http://dlang.org/phobos/std_string.html
pure nothrow @nogc @system inout(char)[] fromStringz(inout(char)* 
cString);

 ^


Re: shared array?

2015-09-12 Thread Adam D. Ruppe via Digitalmars-d-learn

On Saturday, 12 September 2015 at 13:42:44 UTC, Prudence wrote:
Using it at all is a problem because one doesn't know when and 
where.


It is called when the collect function is called and where it was 
called from.


D's garbage collector isn't magic, it is just a function that 
frees memory when the pool runs low.


It's like playing Russian roulette. It doesn't matter if only 
1/6 times will kill you. It's totally different than 0/6.


The big difference is the garbage collector doesn't actually kill 
you.


Memory corruption, use-after-free, and double-free bugs on the 
other hand often do terminate your process.


Re: shared array?

2015-09-12 Thread Adam D. Ruppe via Digitalmars-d-learn
On Saturday, 12 September 2015 at 22:36:00 UTC, Laeeth Isharc 
wrote:
Thank you for this.  How large is the allocation for closure 
for a delegate?  Just a pair of pointers?


It depends on what the delegate needs to capture. It makes a copy 
of the local variables the function is referencing. (Usually 
pretty small I'd gamble; how big are your typical function's 
arguments and local vars?)




Re: shared array?

2015-09-12 Thread Laeeth Isharc via Digitalmars-d-learn
On Saturday, 12 September 2015 at 06:23:12 UTC, Jonathan M 
Davis wrote:
Aside from the few classes in Phobos, its GC usage is almost 
entirely restricted to when it allocates arrays or when it has 
to allocate a closure for a delegate, which can happen in some 
cases when passing predicates to range-based algorithms. 
Avoiding functions that need to allocate arrays avoids that 
source of allocation, and using functors or function pointers 
as predicates avoids having to allocate closures. So, you 
_can_ end up with GC allocations accidentally in Phobos if 
you're not careful, but on the whole, the assertion that 
Phobos uses the GC heavily is FUD - or at least a 
misunderstanding. But as we make more of the functions use 
lazy ranges rather than arrays (particularly with regards to 
strings), and we make more of the code @nogc, it becomes even 
clearer that the GC isn't involved. Also, improvements to how 
lambdas are handled should reduce how often closures have to 
be allocated for them.


Thank you for this.  How large is the allocation for closure for 
a delegate?  Just a pair of pointers?


On Saturday, 12 September 2015 at 13:42:44 UTC, Prudence wrote:

I don't think it's that simple.

Saying that it doesn't use it most of the time is not an 
answer/solution. Using it at all is a problem because one 
doesn't know when and where. I realize there is a switch 
now(-vgc), and maybe that is the solution, but you say "well, 
phobos only uses 0.01% on the GC", yet since you either don't, 
can't, or won't know where that is, then it might as well be 
100% if you would like to potentially get off the GC one day.


It's like playing Russian roulette. It doesn't matter if only 
1/6 times will kill you. It's totally different than 0/6.


But if you hardly use the GC, how long is it really going to take 
to run?




Re: Why does reverse also flips my other dynamic array?

2015-09-12 Thread Ali Çehreli via Digitalmars-d-learn

On 09/12/2015 02:29 PM, Marco Leise wrote:

> Note that often the original dynamic array has additional
> capacity beyond its length. This can be used to ~= additional
> items without causing a reallocation, but is lost when you
> do the assignment "b = a".

Actually, the capacity is still there, useful to the runtime. 
Interestingly, the first dynamic array that is appended the new element 
becomes the owner of that capacity. The capacity of the other dynamic 
array becomes 0.


import std.stdio;

void main(){

  int [] a = [1,2,3,4,5];
  int [] b = a;

  writeln(a.ptr, " ", b.ptr);
  writeln(a.capacity, " ", b.capacity);
  a ~= 42;// <-- change to b, now b owns the capacity
  writeln(a.ptr, " ", b.ptr);
  writeln(a.capacity, " ", b.capacity);
}

Ali



Re: Hello World Example with Glade?

2015-09-12 Thread Mike McKee via Digitalmars-d-learn
On Saturday, 12 September 2015 at 09:03:53 UTC, Russel Winder 
wrote:
Once you get to a GtkD application bigger than "Hello World" 
create a project and use Dub.


Oh yes, Dub. That's the ticket. Thanks, mate.




Massive linker error after upgrade to DMD 2.068.1-1

2015-09-12 Thread rcorre via Digitalmars-d-learn
After upgrading from DMD 2.068.0-1 to DMD 2.068.1-1, my project 
began producing a large linker error (when built using dub).


I was able to trace it down to a single line:

target = target.adjacent(Diagonals.yes).randomSample(1).front;

target is of type RowCol 
(https://github.com/rcorre/dtiled/blob/master/src/dtiled/coords.d#L36),
which has an `adjacent` property that leverages chain, only, and 
take.


When I comment this line out (or build on a system with DMD 
2.068.0-1), I can build fine. When uncommented, I see (post 
ddemangle):


http://dpaste.com/1PJB35V

I've tried to break this down into a reduced example to prove 
that I can call randomSample on the range returned by 
RowCol.adjacent (http://dpaste.com/13G9WDE). This runs fine, so 
it seems to be a deeper issue with my build environment (full 
project at https://github.com/rcorre/damage_control if you're 
curious).


I don't want to turn this into "please debug my project for me", 
but do people have general hints on how to track down issues like 
this?


Thanks!

Side note: I see src/transition.d in the output, but I don't see 
how it is relevant to the error (it isn't even imported by the 
file causing the error).


Re: Why does reverse also flips my other dynamic array?

2015-09-12 Thread Marco Leise via Digitalmars-d-learn
Am Sat, 12 Sep 2015 10:55:50 +
schrieb "Namal" :

> > Why is also b flipped here? This doesn't happen if I use static 
> > arrays.
> 
> nvm. I need to .dup that.

Correct, static arrays are value types and copied on
assignment. Dynamic arrays on the other hand are generally
slices of memory on the garbage collected heap represented by
just a pointer to the first element and a length. When you
assign those you only get a second reference to the same data.

Note that often the original dynamic array has additional
capacity beyond its length. This can be used to ~= additional
items without causing a reallocation, but is lost when you
do the assignment "b = a". (This is so you can't accidentally
append different items to both a and b and have them overwrite
each other.) You can query the actual capacity with a.capacity.

Just felt like writing down some knowledge you might need
along the way. :)

-- 
Marco



Re: I guess this is a bug?

2015-09-12 Thread anonymous via Digitalmars-d-learn
On Saturday 12 September 2015 20:28, Random D user wrote:

> prints (with option B):
> bar: 0.00, 0.00   // BUG??
> baz: 1.00, 2.00

Looks like a bug to me. Please file an issue at https://issues.dlang.org/


Re: I guess this is a bug?

2015-09-12 Thread Random D user via Digitalmars-d-learn
On Saturday, 12 September 2015 at 18:28:02 UTC, Random D user 
wrote:

or is it some obscure feature conflict?

[...]


Oh... and I'm using win 64-bit and dmd 2.068.1, but this behavior 
was present earlier than that...


I guess this is a bug?

2015-09-12 Thread Random D user via Digitalmars-d-learn

or is it some obscure feature conflict?

struct Foo
{
this( float x_, float y_ )
{
// option A
//x = x_;
//y = y_;

// option B
v[0] = x_;
v[1] = y_;
}

union
{
struct
{
float x = 0;
float y = 0;
}
float[2] v;
}
}

struct Bar
{
Foo foo = Foo( 1, 2 );
}

Bar bar;
Bar baz = bar.init;

printf( "bar: %f, %f\n", bar.foo.x, bar.foo.y );
printf( "baz: %f, %f\n", baz.foo.x, baz.foo.y );
-
prints (with option B):
bar: 0.00, 0.00   // BUG??
baz: 1.00, 2.00

prints (with option A):
bar: 1.00, 2.00
baz: 1.00, 2.00
-
Luckily the option A works as I expected and is good enough for 
me...


Re: Calling D from C, C++, Python…

2015-09-12 Thread Adam D. Ruppe via Digitalmars-d-learn
On Saturday, 12 September 2015 at 18:20:37 UTC, Brad Roberts 
wrote:
You can get away with it in some circumstances, but it's at 
your own risk.


Yeah, I agree.


Re: Mixin templates accessing mixed out scope

2015-09-12 Thread anonymous via Digitalmars-d-learn
On Saturday 12 September 2015 19:36, Prudence wrote:

> On Saturday, 12 September 2015 at 17:11:04 UTC, anonymous wrote:
[...]
>> class MyStore
>> {
>> class SingleStore
>> {
>> static void New() // Removing 'static' compiles
>> {
>> new SingleStore();
>> }
>> }
>> }
[...]
>> As for a fix: I guess SingleStore isn't supposed to be a nested 
>> class. Mark it static then.
> 
> NO! That is the whole point!

So New is supposed to create a SingleStore that's associated with a MyStore?

That static can't work like that then. It doesn't escape just the first 
level (SingleStore), but all levels (SingleStore and MyStore). That is, a 
static method isn't bound to any object. But you need a MyStore to construct 
a (nested) SingleStore. You have to pass a MyStore somehow.

It can come via `this.outer`:

class MyStore
{
class SingleStore
{
SingleStore New()
{
return new SingleStore;
}
}
}
void main()
{
auto ms = new MyStore;
auto ss1 = ms.new SingleStore;
auto ss2 = ss1.New();
}

But here you need a SingleStore object to call New on. Not what you want, I 
think.

Or the MyStore can come via parameter:

class MyStore
{
class SingleStore
{
static SingleStore New(MyStore ms)
{
return ms.new SingleStore;
}
}
}
void main()
{
auto ms = new MyStore;
auto ss = MyStore.SingleStore.New(ms);
}


Or you can move New a level up, into MyStore, and then plain `this` is the 
needed MyStore:

class MyStore
{
class SingleStore
{
}
SingleStore NewSingleStore()
{
return new SingleStore;
}
}
void main()
{
auto ms = new MyStore;
auto ss = ms.NewSingleStore();
}




Re: Calling D from C, C++, Python…

2015-09-12 Thread Brad Roberts via Digitalmars-d-learn

On 9/12/15 9:20 AM, Adam D. Ruppe via Digitalmars-d-learn wrote:

On Saturday, 12 September 2015 at 09:47:55 UTC, Jacob Carlborg wrote:

Well, if your D function doesn't use anything of the runtime I guess it's not 
necessary.


Right. If you don't call into the threading system in the druntime, you should 
be ok. Keep in mind
though that the GC uses the threads and the new expression, array literals, 
array append, and
others use the GC.

Runtime.initialize is also what calls static and module constructors... and 
might have
responsibility for fixing up dynamic casting of class objects in a shared lib 
too, I'm not sure
about that.

But if you avoid the bulk of the runtime functions, indeed you can get away 
without initializing it.
Just that null thread handle is likely to cause segfaults in places where you 
might not expect if
you don't.

It is best to initialize it. Lots of C libraries need an init an teardown call, 
so surely the Python
interop provides some solution for it. idk what it would be though.


I think it's safest to say (and it belongs in the spec somewhere) that executing D code before 
initializing the runtime results in undefined behavior, or something along those lines.  You can get 
away with it in some circumstances, but it's at your own risk.


Re: Mixin templates accessing mixed out scope

2015-09-12 Thread Prudence via Digitalmars-d-learn

On Saturday, 12 September 2015 at 17:11:04 UTC, anonymous wrote:

On Saturday 12 September 2015 16:30, Ali Çehreli wrote:


Reduced:

[...]

Error: type SingleStore is not an expression


Reduced further:


class MyStore
{
class SingleStore
{
static void New() // Removing 'static' compiles
{
new SingleStore();
}
}
}


And now the problem can be spotted:

SingleStore is a nested class. That means, instances of it are 
bound to MyStore instances. But New is static, so it doesn't 
have a MyStore to which it could attach the `new SingleStore`.


That error message is pretty awful. I filed an issue: 
https://issues.dlang.org/show_bug.cgi?id=15049


like most D errors ;/ it's the #1 problem I'm struggling with in 
D. Remember there's another error with remove, that isn't 
releated to SingleStore.


As for a fix: I guess SingleStore isn't supposed to be a nested 
class. Mark it static then.


NO! That is the whole point!

SingleStore is a momento that wraps the key value pair, e.g.,

auto t = DeleteStore.New("mycooldelegate", (int x) { return true; 
});


t is suppose to be a single store.

then

t.Remove();

removes the delegate from the store.

Note I don't have to know the actual key or delegate!! Which is 
the whole point!


Else it would lool like this:

store[][string] mydelegatestore;

auto dg = (int x) { return true; };
mydelegatestore["mycooldelegate"] ~= dg;

then

mydelegatestore["mycooldelegate"].remove(d => dg == d);

which requires remembering both the key and the delegate, which 
makes using inline lambdas infeasible(because you'll never be 
able to remove them).


I see no reason why SingleStore has to be static. The mixin 
template should insert all that stuff into the class, which, by 
the way, works...


I've also used that New pattern all that time and it works, maybe 
not for nested classes. Moving SingleStore outside the template 
works.




I've fixed the issue with that, it's not as pretty but works. 
Still have the remove error:









import std.stdio;
import std.concurrency;



extern (C) int getch();
import std.string;
import std.concurrency;
import core.time;
import core.thread;
import std.container.array;
import std.typecons;







public class SingleStore(TKey, TValue)
{
public TValue Value;
public TKey Key;
public TValue[][TKey] Store;
public auto Remove()
{
import std.algorithm;
		remove!(c => Value == c")(Store[this.Key], 
SwapStrategy.unstable);	 // Not working, can't disambiguate

}

public static auto New(TKey k, TValue v, ref TValue[][TKey] s)
{
auto o = new SingleStore!(TKey, TValue)(k, v);  
o.Store = s;
return o;
}

private this(TKey k, TValue v)
{
Key = k;
Value = v;
}
}

public mixin template ObjectStore(TKey, TValue)
{
public static TValue[][TKey] store;

public static auto New(TKey k, TValue v)
{
(store[k]) ~= v;
auto o = SingleStore!(TKey, TValue).New(k, v, store);
o.store = store;
return o;
}


}


class MyStore
{
mixin ObjectStore!(string, bool delegate(int));


}

void main()
{
	auto s = MyStore.New("x", (int x) { return true; }); // works, 
stores delegate in MyStore


	s.Remove();  // Doesn't work because std.algorithm's remove 
isn't working


getch();
}

(The whole point of nesting was so I wouldn't have to explicitly 
create a pointer to the store in SingleStore. Should have worked, 
probably another one of D's bugs)








Re: Mixin templates accessing mixed out scope

2015-09-12 Thread anonymous via Digitalmars-d-learn
On Saturday 12 September 2015 16:30, Ali Çehreli wrote:

> Reduced:
[...]
> Error: type SingleStore is not an expression

Reduced further:


class MyStore
{
class SingleStore
{
static void New() // Removing 'static' compiles
{
new SingleStore();
}
}
}


And now the problem can be spotted:

SingleStore is a nested class. That means, instances of it are bound to 
MyStore instances. But New is static, so it doesn't have a MyStore to which 
it could attach the `new SingleStore`.

That error message is pretty awful. I filed an issue:
https://issues.dlang.org/show_bug.cgi?id=15049

As for a fix: I guess SingleStore isn't supposed to be a nested class. Mark 
it static then.


Re: Mixin templates accessing mixed out scope

2015-09-12 Thread Prudence via Digitalmars-d-learn

On Saturday, 12 September 2015 at 14:30:16 UTC, Ali Çehreli wrote:

On 09/12/2015 06:37 AM, Prudence wrote:


Says the creating new SingleStore is not an expression


Reduced:

mixin template ObjectStore(TKey)
{
class SingleStore
{
static void New()// Removing 'static' compiles
{
new SingleStore();
}
}
}

class MyStore
{
mixin ObjectStore!(int);
}

void main()
{
auto s = new MyStore();
}

Error: type SingleStore is not an expression

Ali


Are you saying this is a bug or something else? The only 
alternative I can think of is to use string mixins but this 
method should work?


Re: version and configuration

2015-09-12 Thread Adam D. Ruppe via Digitalmars-d-learn

On Saturday, 12 September 2015 at 14:41:45 UTC, Spacen Jasset
It appears that I can't put this in a module and import it 
elsewhere to test the version specifications as they are all in 
their own namespaces. Is this then a dead end for having a 
feature configuration file?


Correct, version doesn't get imported. You're better off using 
enums or ordinary variables.




Re: Calling D from C, C++, Python…

2015-09-12 Thread Adam D. Ruppe via Digitalmars-d-learn
On Saturday, 12 September 2015 at 09:47:55 UTC, Jacob Carlborg 
wrote:
Well, if your D function doesn't use anything of the runtime I 
guess it's not necessary.


Right. If you don't call into the threading system in the 
druntime, you should be ok. Keep in mind though that the GC uses 
the threads and the new expression, array literals, array 
append, and others use the GC.


Runtime.initialize is also what calls static and module 
constructors... and might have responsibility for fixing up 
dynamic casting of class objects in a shared lib too, I'm not 
sure about that.


But if you avoid the bulk of the runtime functions, indeed you 
can get away without initializing it. Just that null thread 
handle is likely to cause segfaults in places where you might not 
expect if you don't.


It is best to initialize it. Lots of C libraries need an init an 
teardown call, so surely the Python interop provides some 
solution for it. idk what it would be though.


Re: version and configuration

2015-09-12 Thread tcak via Digitalmars-d-learn
On Saturday, 12 September 2015 at 14:41:45 UTC, Spacen Jasset 
wrote:

If I say this in one module:

version = current

version (Current) {
   version = featurea;
   version = featureb;
   version = featurec;
}

It appears that I can't put this in a module and import it 
elsewhere to test the version specifications as they are all in 
their own namespaces. Is this then a dead end for having a 
feature configuration file?


Is the way to do this to define some constants in a module, and 
test these instead. Something like:


features.d

featureA = true;
featureB = false;
--

main.d

import features;

static if (featureA == true) {

}


You can use enum to define featureA, featureB, and featureC 
instead of version.


You could say,

version(Current){
enum featureA = true;
enum featureB = true;
enum featureC = true;
}


version and configuration

2015-09-12 Thread Spacen Jasset via Digitalmars-d-learn

If I say this in one module:

version = current

version (Current) {
   version = featurea;
   version = featureb;
   version = featurec;
}

It appears that I can't put this in a module and import it 
elsewhere to test the version specifications as they are all in 
their own namespaces. Is this then a dead end for having a 
feature configuration file?


Is the way to do this to define some constants in a module, and 
test these instead. Something like:


features.d

featureA = true;
featureB = false;
--

main.d

import features;

static if (featureA == true) {

}


Re: Mixin templates accessing mixed out scope

2015-09-12 Thread Ali Çehreli via Digitalmars-d-learn

On 09/12/2015 06:37 AM, Prudence wrote:


Says the creating new SingleStore is not an expression


Reduced:

mixin template ObjectStore(TKey)
{
class SingleStore
{
static void New()// Removing 'static' compiles
{
new SingleStore();
}
}
}

class MyStore
{
mixin ObjectStore!(int);
}

void main()
{
auto s = new MyStore();
}

Error: type SingleStore is not an expression

Ali



Re: shared array?

2015-09-12 Thread Prudence via Digitalmars-d-learn
On Saturday, 12 September 2015 at 06:23:12 UTC, Jonathan M Davis 
wrote:
On Friday, September 11, 2015 23:29:05 Laeeth Isharc via 
Digitalmars-d-learn wrote:
On Friday, 11 September 2015 at 21:58:28 UTC, Adam D. Ruppe 
wrote:

> [...]

Seems to be quite a lot of FUD wrt use of standard library and 
GC, which means also perhaps we don't communicate this point 
very well as a community.  Making Phobos GC-optional perhaps 
is an ultimate answer.  But people seem to think that you're 
back to C without the GC.


Aside from the few classes in Phobos, its GC usage is almost 
entirely restricted to when it allocates arrays or when it has 
to allocate a closure for a delegate, which can happen in some 
cases when passing predicates to range-based algorithms. 
Avoiding functions that need to allocate arrays avoids that 
source of allocation, and using functors or function pointers 
as predicates avoids having to allocate closures. So, you _can_ 
end up with GC allocations accidentally in Phobos if you're not 
careful, but on the whole, the assertion that Phobos uses the 
GC heavily is FUD - or at least a misunderstanding. But as we 
make more of the functions use lazy ranges rather than arrays 
(particularly with regards to strings), and we make more of the 
code @nogc, it becomes even clearer that the GC isn't involved. 
Also, improvements to how lambdas are handled should reduce how 
often closures have to be allocated for them.




I don't think it's that simple.

Saying that it doesn't use it most of the time is not an 
answer/solution. Using it at all is a problem because one doesn't 
know when and where. I realize there is a switch now(-vgc), and 
maybe that is the solution, but you say "well, phobos only uses 
0.01% on the GC", yet since you either don't, can't, or won't 
know where that is, then it might as well be 100% if you would 
like to potentially get off the GC one day.


It's like playing Russian roulette. It doesn't matter if only 1/6 
times will kill you. It's totally different than 0/6.






Mixin templates accessing mixed out scope

2015-09-12 Thread Prudence via Digitalmars-d-learn


The following code simply does not work, it might be buggy now 
after fiddling with it but basically remove and the 
SingleStore.New are not working(Says the creating new SingleStore 
is not an expression, which makes no sense to me).



Essentially I'm creating a mixin template so I can have different 
"object stores", which is just an associative array of arrays. 
The SingleStore wraps the key, value pair added to the store so 
that I can keep track of and remove the added object easily 
without having to explicitly remember everything(e.g., what if 
TValue is a delegate? Then it get's messy).


Why remove can't disambiguate is beyond me... Why I can't create 
a SingleStore!(int, double)() is beyond me! It would be nice if 
D's errors were a little more helpful!



Error		Error: type SingleStore!(int, double) is not an 
expression		Test.d	56


auto o = new SingleStore!(TKey, TValue)(k, v);  

huh??? Have I just lost it or is this how one is suppose to 
create such an object?


(I do realize that I do not have to parameterize SingleStore. It 
is irrelevant here though)


import std.stdio;
import std.concurrency;



extern (C) int getch();
import std.string;
import std.concurrency;
import core.time;
import core.thread;
import std.container.array;
import std.typecons;








// Creates a static Associative Array that stores multiple values 
per key. The object returned by New can then be used to remove 
the object without having to remember the object specifically.

public mixin template ObjectStore(TKey, TValue)
{
	// The object store. It is static. Mixin the template into it's 
different types to create different types of stores. All objects 
of that type are then in the same store.

public static TValue[][TKey] store;

public static auto New(TKey k, TValue v)
{
(store[k]) ~= v;
auto o = SingleStore!(TKey, TValue).New(k, v);
return o;
}

public static auto Remove(SingleStore!(TKey, TValue) o)
{
import std.algorithm;
		//remove!(c => (this.Value == c))(store[o.Key], 
SwapStrategy.unstable);

}


public class SingleStore(TKey, TValue)
{
public TValue Value;
public TKey Key;

public auto Remove()
{
import std.algorithm;
			//remove!("c => (this.Value == c)")(store[this.Key], 
SwapStrategy.unstable);

//super.Remove(this);
}

public static auto New(TKey k, TValue v)
{
auto o = new SingleStore!(TKey, TValue)(k, v);  
return o;
}

private this(TKey k, TValue v)
{
Key = k;
Value = v;
}
}

}


class MyStore
{
mixin ObjectStore!(int, double);
}

void main()
{
auto s = new MyStore();


getch();




Re: reading file byLine

2015-09-12 Thread Namal via Digitalmars-d-learn

On Monday, 7 September 2015 at 10:28:20 UTC, deed wrote:

On Monday, 7 September 2015 at 10:25:09 UTC, deed wrote:

writeln(x);// or you can pass it to a function.


I meant `writeln(x + 5)`


If I have just red your post before I started using reverse on 
dynamic arrays...
Anyway, there is no .reverse for strings I guess, what is the way 
to completely reverse a string in D?


Re: Difference between back (`) and double (") quoted strings

2015-09-12 Thread Adam D. Ruppe via Digitalmars-d-learn

On Saturday, 12 September 2015 at 08:22:03 UTC, NX wrote:
What if I told you, you should search the official reference 
before asking such things in the forum?


Searching is kinda hard, so I encourage people to ask if 
something doesn't come up quickly. And then we need to be sure to 
always answer because their question might be the thing that 
comes up on some future user's search, and if they see "google 
it", their reaction might be "what the &^&*%^ do you think 
brought me here?!?!?!"


(at least that's my reaction!)


Re: Adjacent Pairs Range

2015-09-12 Thread Nordlöw

On Saturday, 12 September 2015 at 11:46:55 UTC, Nordlöw wrote:

I wrote my own as adjacentTuples and adjacentPairs:

https://github.com/nordlow/justd/blob/master/range_ex.d#L702

Note: No yet extended to N > 2.

An alternative naming would be overlappingTuples/Pairs.


Should this go into Phobos?


Re: Adjacent Pairs Range

2015-09-12 Thread Nordlöw
On Saturday, 12 September 2015 at 11:34:03 UTC, Bahman Movaqar 
wrote:

On 09/12/2015 03:09 PM, "Nordlöw" wrote:

InputRange please, not RandomAccessRanges ;)


Oops!  Here's one using only `InputRange` interface:


I wrote my own as adjacentTuples and adjacentPairs:

https://github.com/nordlow/justd/blob/master/range_ex.d#L702

Note: No yet extended to N > 2.

An alternative naming would be overlappingTuples/Pairs.


Re: Adjacent Pairs Range

2015-09-12 Thread Bahman Movaqar via Digitalmars-d-learn
On 09/12/2015 04:04 PM, Bahman Movaqar wrote:
> Oops!  Here's one using only `InputRange` interface:

I believe I need to warn you that I'm just learning D; so take my
solution at your own risk :-)

-- 
Bahman Movaqar


Re: Adjacent Pairs Range

2015-09-12 Thread Bahman Movaqar via Digitalmars-d-learn
On 09/12/2015 03:09 PM, "Nordlöw" wrote:
> InputRange please, not RandomAccessRanges ;)

Oops!  Here's one using only `InputRange` interface:

T[][] collate(T)(T[] a)
{
  alias CollateResult = Tuple!(T[][], "result", T, "tlHd");
  CollateResult _collate(CollateResult collres)
  {
if (!a.empty) {
  auto newTlHd = a.front;
  a.popFront();
  return _collate(
CollateResult(
  collres.result ~ [collres.tlHd, newTlHd],
  newTlHd
)
  );
} else {
  return collres;
}
  }

  if (!a.empty) {
auto tlHd = a.front;
a.popFront();
return _collate(
  CollateResult([], tlHd)
).result;
  } else {
return [];
  }
}

unittest {
  writeln([10, 20, 30].collate!int);
}


-- 
Bahman Movaqar


Re: Why does reverse also flips my other dynamic array?

2015-09-12 Thread Namal via Digitalmars-d-learn
Why is also b flipped here? This doesn't happen if I use static 
arrays.


nvm. I need to .dup that.


Re: Adjacent Pairs Range

2015-09-12 Thread Nordlöw
On Saturday, 12 September 2015 at 10:35:41 UTC, Bahman Movaqar 
wrote:

On 09/12/2015 02:47 PM, "Nordlöw" wrote:
How do I most elegantly iterate all the adjacent pairs in an 
`InputRange` using Phobos?


Something like

[1,2,3,4] => [(1,2), (2,3), (3,4)]


That's call `collate`ing IIRC.
A quick solution would be using `std.range.transposed`:

auto a = [1,2,3,4];
auto ll = [a, a[1..$]];
transpose(ll); // returns [[1, 2], [2, 3], [3, 4], [4]]


InputRange please, not RandomAccessRanges ;)


Re: Adjacent Pairs Range

2015-09-12 Thread Bahman Movaqar via Digitalmars-d-learn
On 09/12/2015 02:47 PM, "Nordlöw" wrote:
> How do I most elegantly iterate all the adjacent pairs in an
> `InputRange` using Phobos?
> 
> Something like
> 
> [1,2,3,4] => [(1,2), (2,3), (3,4)]

That's call `collate`ing IIRC.
A quick solution would be using `std.range.transposed`:

auto a = [1,2,3,4];
auto ll = [a, a[1..$]];
transpose(ll); // returns [[1, 2], [2, 3], [3, 4], [4]]

Though you have to take care of the dangling last element yourself.

-- 
Bahman Movaqar


Why does reverse also flips my other dynamic array?

2015-09-12 Thread Namal via Digitalmars-d-learn

import std.stdio;

void main(){

  int [] a = [1,2,3,4,5];
  int [] b = a;

writeln(b, " ", a);
a.reverse;
writeln(b, " ", a);
}


I get:

[1, 2, 3, 4, 5] [1, 2, 3, 4, 5]
[5, 4, 3, 2, 1] [5, 4, 3, 2, 1]

Why is also b flipped here? This doesn't happen if I use static 
arrays.


Adjacent Pairs Range

2015-09-12 Thread Nordlöw
How do I most elegantly iterate all the adjacent pairs in an 
`InputRange` using Phobos?


Something like

[1,2,3,4] => [(1,2), (2,3), (3,4)]



Re: Calling D from C, C++, Python…

2015-09-12 Thread Jacob Carlborg via Digitalmars-d-learn

On 2015-09-12 10:56, Russel Winder via Digitalmars-d-learn wrote:


I have a small D function (C linkage) compiled to a shared object that
I am calling from Python via CFFI that works fine with no D runtime
initialization. Thus I have experimental evidence "always" is not
entirely the case! I really need to explore the boundaries of what
point you have to actually initialize the D runtime…


Well, if your D function doesn't use anything of the runtime I guess 
it's not necessary. Example:


void foo ()
{
printf("foo\n");
}

--
/Jacob Carlborg


Re: Difference between back (`) and double (") quoted strings

2015-09-12 Thread Bahman Movaqar via Digitalmars-d-learn
On 09/12/2015 12:52 PM, NX wrote:
> What if I told you, you should search the official reference before
> asking such things in the forum?

I did search the net for terms such as "d lang back quoted string" or "d
lang multi line string" or "d lang string interpolation" before asking here.
However the term "Wysiwyg string" didn't occur to my mind and from what
I could gather from the net and the test programs I wrote, I couldn't
determine the difference between `-string and "=string.  Hence
I decided to ask people here.

> http://dlang.org/lex.html#WysiwygString

Thanks for the help.


-- 
Bahman Movaqar

http://BahmanM.com - https://twitter.com/bahman__m

https://github.com/bahmanm - https://gist.github.com/bahmanm

PGP Key ID: 0x6AB5BD68 (keyserver2.pgp.com)


Re: Hello World Example with Glade?

2015-09-12 Thread Russel Winder via Digitalmars-d-learn
On Sat, 2015-09-12 at 06:58 +, Mike McKee via Digitalmars-d-learn
wrote:
[…]
> I just tested and you are correct. Now, is there a way that I can 
> edit /etc/dmd.conf and make it so that I don't need to do this 
> pkg-config stuff all the time?

I think editing /etc/dmd.conf would be the wrong way forward on this. I
would suggest using one of SCons, CMake, Make as a build tool and
encode this in the build rules. Once you get to a GtkD application
bigger than "Hello World" create a project and use Dub. 

-- 
Russel.
=
Dr Russel Winder  t: +44 20 7585 2200   voip: sip:russel.win...@ekiga.net
41 Buckmaster Roadm: +44 7770 465 077   xmpp: rus...@winder.org.uk
London SW11 1EN, UK   w: www.russel.org.uk  skype: russel_winder



signature.asc
Description: This is a digitally signed message part


Re: Calling D from C, C++, Python…

2015-09-12 Thread Russel Winder via Digitalmars-d-learn
On Fri, 2015-09-11 at 21:50 +0200, Jacob Carlborg via Digitalmars-d
-learn wrote:
> On 2015-09-10 20:01, Russel Winder via Digitalmars-d-learn wrote:
> > Is there an easy way of knowing when you do not have to initialize
> > the
> > D runtime system to call D code from, in this case, Python via a C
> > adapter?
> 
> You always need to initialize the D runtime, unless you have a D main
> function. You can initialize the runtime as many times you like, 
> assuming you also deinitialize it the same number of times.

I have a small D function (C linkage) compiled to a shared object that
I am calling from Python via CFFI that works fine with no D runtime
initialization. Thus I have experimental evidence "always" is not
entirely the case! I really need to explore the boundaries of what
point you have to actually initialize the D runtime…

-- 
Russel.
=
Dr Russel Winder  t: +44 20 7585 2200   voip: sip:russel.win...@ekiga.net
41 Buckmaster Roadm: +44 7770 465 077   xmpp: rus...@winder.org.uk
London SW11 1EN, UK   w: www.russel.org.uk  skype: russel_winder



signature.asc
Description: This is a digitally signed message part


Re: Difference between back (`) and double (") quoted strings

2015-09-12 Thread NX via Digitalmars-d-learn
On Saturday, 12 September 2015 at 08:13:33 UTC, Bahman Movaqar 
wrote:
Is there any or they are just simply syntactically equivalent? 
Are there any official docs on this?


What if I told you, you should search the official reference 
before asking such things in the forum?


http://dlang.org/lex.html#WysiwygString

Wysiwyg: What you see is what you get

  writeln(`\asd"fg"hj
hmph'`);


\asd"fg"hj
haha'



Difference between back (`) and double (") quoted strings

2015-09-12 Thread Bahman Movaqar via Digitalmars-d-learn
Is there any or they are just simply syntactically equivalent?
Are there any official docs on this?

-- 
Bahman Movaqar

http://BahmanM.com - https://twitter.com/bahman__m
https://github.com/bahmanm - https://gist.github.com/bahmanm
PGP Key ID: 0x6AB5BD68 (keyserver2.pgp.com)



signature.asc
Description: OpenPGP digital signature


Re: How To: Passing curried functions around

2015-09-12 Thread Bahman Movaqar via Digitalmars-d-learn

On Friday, 11 September 2015 at 21:06:32 UTC, Ali Çehreli wrote:

On 09/11/2015 02:04 PM, Ali Çehreli wrote:

The same keyword has a different use with
templates:


And the official documentation:

  http://dlang.org/template.html#TemplateAliasParameter



Thanks again!


Re: Hello World Example with Glade?

2015-09-12 Thread Mike McKee via Digitalmars-d-learn

On Friday, 11 September 2015 at 20:29:05 UTC, Mike Wey wrote:
You should be able to drop the -L-ldl and -I/usr/ flags, as 
they are included in the pkg-config output.


I just tested and you are correct. Now, is there a way that I can 
edit /etc/dmd.conf and make it so that I don't need to do this 
pkg-config stuff all the time?