Re: Undefined identifier error with enum in the separate module

2014-02-02 Thread Philippe Sigaud
Bearophile:
 I have seen other people hit this problem (like problems caused by having a
 set.d module containing a Set struct plus a set helper function).

 Is this worth warning against (with a dmd warning/error)?

I'd prefer an error, with the same kind of nice message you get when
you forget to import std.stdio.
Ideally, the error should be caught as soon as the module is compiled,
like shadowing errors (which it is, in a way).

module foo contains a member named foo. Module members cannot shadow
module names.


Re: Undefined identifier error with enum in the separate module

2014-02-02 Thread bearophile

Philippe Sigaud:

I'd prefer an error, with the same kind of nice message you get 
when

you forget to import std.stdio.
Ideally, the error should be caught as soon as the module is 
compiled,

like shadowing errors (which it is, in a way).

module foo contains a member named foo. Module members cannot 
shadow module names.


This is the ER I have opened, if you have comments or you don't 
agree with my ideas, please comment there:

https://d.puremagic.com/issues/show_bug.cgi?id=12059

Bye,
bearophile


Re: std.typecons wrap interface with NVI

2014-02-02 Thread Dicebot
`wrap` has relatively simple meaning: for any interface I and 
class C it generates class C' inheriting I and implementing all 
methods of I in terms of forwarding calls to methods of C with 
same name and signature.


What does happen here is that `wrap` was not implemented with NVI 
in mind and tries to override `both` in C' which, of course, 
can't work.


File an enhancement request for `wrap` to become aware of NVI.


Re: std.typecons.Rebindable

2014-02-02 Thread Jack Applegame

What's the point of using Rebindable for a non-[class|interface]
type? They are the only type where it makes sense, since you
can't give different qualifiers to the handler and the object.

I'm surprised it even handles arrays at all. As a matter of 
fact,

the documentation says:
However, Rebindable!(T) does not compile if T is a non-class
type.
If arrays worked, I think it was buggy behavior. There's no 
sense

in having a Rebindable array.
Hmm. I need to pass an unique associative array to several 
threads. If this array has immutable elements, but isn't 
immutable itself, then what happen if one of threads adds or 
removes element from array?
Associative arrays is some kind of hash-table. Adding alements in 
one thread can cause undefined behaviour in other thread.

Am I wrong?


How to embed data within an executable?

2014-02-02 Thread Gary Willoughby
I'm using DMD to create an executable and wondered what is the 
best way to embed data into it.


For example, i want to embed base64 image data in the executable 
then access it during runtime. Is this possible? if so how?


Re: How to embed data within an executable?

2014-02-02 Thread bearophile

Gary Willoughby:

I'm using DMD to create an executable and wondered what is the 
best way to embed data into it.


For example, i want to embed base64 image data in the 
executable then access it during runtime. Is this possible? if 
so how?


There are various ways to do it. If your data is not too much, 
one way is to use a hex string. You can also use 
mixin(import(...)).


Bye,
bearophile


Re: How to embed data within an executable?

2014-02-02 Thread Gary Willoughby

On Sunday, 2 February 2014 at 16:52:38 UTC, bearophile wrote:

Gary Willoughby:

I'm using DMD to create an executable and wondered what is the 
best way to embed data into it.


For example, i want to embed base64 image data in the 
executable then access it during runtime. Is this possible? if 
so how?


There are various ways to do it. If your data is not too much, 
one way is to use a hex string. You can also use 
mixin(import(...)).


Bye,
bearophile


How do you access that at runtime?


Re: How to embed data within an executable?

2014-02-02 Thread bearophile

Gary Willoughby:


How do you access that at runtime?


In the usual ways. A hex string is just a different kind of 
literal for a string (so if you need hex data you need to cast 
it, unfortunately). The mixin(import(...)) can import anything, 
including your base64 data, that you can convert at compile-time 
or run-time in what you need.


Bye,
bearophile


Re: std.typecons wrap interface with NVI

2014-02-02 Thread Matthew Dudley

On Sunday, 2 February 2014 at 11:07:41 UTC, Dicebot wrote:
`wrap` has relatively simple meaning: for any interface I and 
class C it generates class C' inheriting I and implementing all 
methods of I in terms of forwarding calls to methods of C with 
same name and signature.


What does happen here is that `wrap` was not implemented with 
NVI in mind and tries to override `both` in C' which, of 
course, can't work.


File an enhancement request for `wrap` to become aware of NVI.


That's kinda what I suspected was happening.

I've added it to bugzilla:
https://d.puremagic.com/issues/show_bug.cgi?id=12064

Thanks guys


@trusted delegates all over std.array

2014-02-02 Thread TheFlyingFiddle

Why is std.array litered with @trusted delegates?

Ex:

in the clear method in Appender.

void clear() @safe pure nothrow
{
   if(_data)
   {
  _data.arr = ()@trusted { return _data.arr.ptr[0 .. 0]; }();
   }
}

Is it to enable @safe? And should it be allowed i mean the code 
does deal with pointers so it really should be made @trusted? Or 
is the reasoning that as much of the standard library should be 
@safe as possible so some small hacks are ok?


Re: @trusted delegates all over std.array

2014-02-02 Thread Andrej Mitrovic

On Sunday, 2 February 2014 at 17:40:47 UTC, TheFlyingFiddle wrote:

Why is std.array litered with @trusted delegates?


IIRC these were added in the last few releases to make code 
CTFE-able or to allow pure code to call such functions. A lot of 
array/string-processing code wasn't usable from CTFE/pure, this 
was one workaround to the problem.


Re: How to embed data within an executable?

2014-02-02 Thread Gary Willoughby

On Sunday, 2 February 2014 at 17:17:14 UTC, bearophile wrote:

Gary Willoughby:


How do you access that at runtime?


In the usual ways. A hex string is just a different kind of 
literal for a string (so if you need hex data you need to cast 
it, unfortunately). The mixin(import(...)) can import 
anything, including your base64 data, that you can convert at 
compile-time or run-time in what you need.


Bye,
bearophile


Ah right i see what you mean. Something like this:

template embed(string file)
{
private string getData()
{
return Base64.encode(cast(ubyte[])import(file));
}

enum embed = getData();
}


Re: How to embed data within an executable?

2014-02-02 Thread Gary Willoughby

On Sunday, 2 February 2014 at 17:25:12 UTC, Orvid King wrote:
Base64.encode(import(myFile));` should work perfectly, 
provided myFile is in a path passed to dmd with the -j switch.


Yep it does.


Re: How to embed data within an executable?

2014-02-02 Thread Dicebot

On Sunday, 2 February 2014 at 17:45:37 UTC, Gary Willoughby wrote:

enum embed = getData();
}


I am pretty sure you want immutable variable here, not enum. 
Latter is placement constant.


Re: @trusted delegates all over std.array

2014-02-02 Thread Jesse Phillips

On Sunday, 2 February 2014 at 17:47:21 UTC, Andrej Mitrovic wrote:
On Sunday, 2 February 2014 at 17:40:47 UTC, TheFlyingFiddle 
wrote:

Why is std.array litered with @trusted delegates?


IIRC these were added in the last few releases to make code 
CTFE-able or to allow pure code to call such functions. A lot 
of array/string-processing code wasn't usable from CTFE/pure, 
this was one workaround to the problem.


Pretty sure @trusted only affect the use of @safe and never makes 
CTFE work.


TheFlyingFiddle, the question is about this chunk of code:

_data.arr = return _data.arr.ptr[0 .. 0];

Does this code run the risk of corrupting memory? The purpose of 
@trusted is to allow code which has been reviewed to be safe to 
be used in @safe code. It can do everything @system code can, but 
should be reserved for code that doesn't rely on the caller to 
provide proper data.


Since the compiler can't validate @system code to be safe, it 
relies on a human to tell it what is safe, this is where @trusted 
comes in.


Re: Undefined identifier error with enum in the separate module

2014-02-02 Thread Philippe Sigaud
 This is the ER I have opened, if you have comments or you don't agree with
 my ideas, please comment there:
 https://d.puremagic.com/issues/show_bug.cgi?id=12059

Well, I really thought it was forbidden (as in: the code will never
work). I guess you can define foo inside module foo.d and use it
internally... So OK for your proposal.


Re: How to embed data within an executable?

2014-02-02 Thread Gary Willoughby

On Sunday, 2 February 2014 at 19:57:11 UTC, Dicebot wrote:
On Sunday, 2 February 2014 at 17:45:37 UTC, Gary Willoughby 
wrote:

enum embed = getData();
}


I am pretty sure you want immutable variable here, not enum. 
Latter is placement constant.


I thought this was an idiomatic way to write eponymous templates? 
Manifest constants by their nature can not be mutated.


http://dlang.org/enum.html


Re: Undefined identifier error with enum in the separate module

2014-02-02 Thread bearophile

Philippe Sigaud:

Well, I really thought it was forbidden (as in: the code will 
never

work). I guess you can define foo inside module foo.d and use it
internally... So OK for your proposal.


You can use it externally too, if you qualify it correctly.

Bye,
bearophile


Re: How to embed data within an executable?

2014-02-02 Thread Dicebot

On Sunday, 2 February 2014 at 20:45:45 UTC, Gary Willoughby wrote:

On Sunday, 2 February 2014 at 19:57:11 UTC, Dicebot wrote:
On Sunday, 2 February 2014 at 17:45:37 UTC, Gary Willoughby 
wrote:

enum embed = getData();
}


I am pretty sure you want immutable variable here, not enum. 
Latter is placement constant.


I thought this was an idiomatic way to write eponymous 
templates? Manifest constants by their nature can not be 
mutated.


http://dlang.org/enum.html


At first glance I had impression that you intend to use it as a 
mixin. As a simple eponymous shortcut it will work, nevermind :)


Re: @trusted delegates all over std.array

2014-02-02 Thread Andrej Mitrovic
On 2/2/14, Jesse Phillips jesse.k.phillip...@gmail.com wrote:
 Pretty sure @trusted only affect the use of @safe and never makes
 CTFE work.

This is what I remember, it has a huge diff and has lots of these
trusted lambdas in it:
https://github.com/D-Programming-Language/phobos/pull/1337/files


Re: @trusted delegates all over std.array

2014-02-02 Thread Jonathan M Davis
On Sunday, February 02, 2014 22:46:02 Andrej Mitrovic wrote:
 On 2/2/14, Jesse Phillips jesse.k.phillip...@gmail.com wrote:
  Pretty sure @trusted only affect the use of @safe and never makes
  CTFE work.
 
 This is what I remember, it has a huge diff and has lots of these
 trusted lambdas in it:
 https://github.com/D-Programming-Language/phobos/pull/1337/files

I think that the fact that the pull is labeled as potentially @safe  pure 
formatting doesn't have anything to do with the @trusted lambda stuff that's 
part of it. I believe that the @trusted lambda is a trick that Kenj came up 
with to be able to mark an expression as @trusted rather than being forced to 
mark the whole function as @trusted or create a separate wrapper function to 
be able to mark part of it as @trusted. Rather, he creates a wrapper function 
in place and calls it.

We'd probably be better off figuring out how to add the ability to mark pieces 
of code as @trusted to the language rather than using this trick, since it's a 
bit clunky and probably incurs unnecessary overhead (though maybe Kenji added 
stuff to the compiler to optimize away the overhead of the lambda, or we're 
lucky and it already did). But in the meantime at least, it is a neat trick, 
since it allows us to restrict what code gets marked as @trusted within a 
function.

- Jonathan M Davis


Re: How to embed data within an executable?

2014-02-02 Thread Rikki Cattermole

On Sunday, 2 February 2014 at 16:48:17 UTC, Gary Willoughby wrote:
I'm using DMD to create an executable and wondered what is the 
best way to embed data into it.


For example, i want to embed base64 image data in the 
executable then access it during runtime. Is this possible? if 
so how?


Perhaps my Bin2D would help[0]. Basically creates a D array 
literal in a file you give it.


[0] https://github.com/rikkimax/Bin2D


Re: std.parallelism: How to wait all tasks finished?

2014-02-02 Thread Dan Killebrew
  // Next line will block execution until all tasks already in 
queue finished.

  // Almost all what I need, but new tasks will not be started.
  taskPool.finish(true);
}


Are you sure TaskPool.finish isn't what you're looking for?

Signals worker threads to terminate when the queue becomes 
empty.


It seems to me that worker threads will continue as long as the 
queue isn't empty. So if a task adds another task to the pool, 
some worker will process the newly enqueued task.