Re: Implicit conversions through purity

2014-04-12 Thread bearophile

Jonathan M Davis:

Honestly, I would have considered that to be a bug. Converting 
the return type
to a different level of mutability based on purity is one 
thing. Automatically
casting the return value just because the function is pure is 
another matter
entirely. Clearly, it can work, but it seems incredibly sloppy 
to me.


In foo1 D is working as designed, as this was a desired feature,
it has passed the Kenji and Walter review, and it was implemented
several months ago. It's a very handy way to create immutable
data with pure functions and it's safe, it's safer than
assumeUnique that is just a convention. Very recently Walter has
further improved this feature, allowing more implicit conversion
cases. So it's the opposite of a bug, it saves you from bugs in
three different ways.

But my question was about the successive foo functions :-)

Bye,
bearophile


Re: Implicit conversions through purity

2014-04-12 Thread Jonathan M Davis
On Saturday, April 12, 2014 21:26:01 bearophile wrote:
> Is it possible and a good idea to allow code like the function
> foo2?
> 
> 
> string foo1(in string s) pure nothrow {
>  auto s2 = s.dup;
>  s2[0] = 'a';
>  return s2; // OK.
> }

Honestly, I would have considered that to be a bug. Converting the return type 
to a different level of mutability based on purity is one thing. Automatically 
casting the return value just because the function is pure is another matter 
entirely. Clearly, it can work, but it seems incredibly sloppy to me.

- Jonathan M Davis


Re: string mixin only works once per block?

2014-04-12 Thread Vlad Levenfeld
These are excellent, thanks! I went ahead and swapped out the 
tags and I think I am going to implement something like that 
opDispatch method for all outside libraries now. Very useful!
That type validation is particularly cool. You could be rendering 
almost arbitrary data in like 2 steps...


Implicit conversions through purity

2014-04-12 Thread bearophile
Is it possible and a good idea to allow code like the function 
foo2?



string foo1(in string s) pure nothrow {
auto s2 = s.dup;
s2[0] = 'a';
return s2; // OK.
}

void foo2(in string s, ref string sOut) pure nothrow {
auto s2 = s.dup;
s2[0] = 'a';
sOut = s2; // Error: cannot implicitly convert
}

void foo3(in string s, out string sOut) pure nothrow {
auto s2 = s.dup;
s2[0] = 'a';
sOut = s2; // Error: cannot implicitly convert
}

void main() {}


Bye,
bearophile


Re: DList bug or programmer error?

2014-04-12 Thread monarch_dodra

On Saturday, 12 April 2014 at 19:34:11 UTC, Daniel K wrote:

This block of code fails the second assert, why?


Because DList is buggy. It should pass.

I have an open pull that fixes this, FYI.


Re: Local function overloading

2014-04-12 Thread Andrej Mitrovic
On 4/12/14, monarch_dodra  wrote:
> I know you can workaround it by putting
> your functions a static members of a dummy struct

You can also use a mixin template:

class A {}
class B {}

mixin template M()
{
void func2(A a) { }
void func2(B b) { }
}

void main()
{
mixin M!();

func2(new A);
func2(new B);
}


DList bug or programmer error?

2014-04-12 Thread Daniel K
This block of code fails the second assert, why? Using 
stableRemove{Front,Back} does not fix it.



import std.range;
import std.container;

unittest {
  auto dl2 = DList!int([2,7]);
  dl2.removeFront();
  //dl2.stableRemoveFront();
  assert(dl2[].walkLength == 1);
  dl2.removeBack();
  //dl2.stableRemoveBack();
  assert(dl2.empty, "not empty?!");
}


void main(){
}


rdmd -unittest file.d


Re: MS ODBC encoding issue

2014-04-12 Thread FrankLike



Sam


you should put th ODBC code to the github.com ,

let all world to see  your hard work!


Frank


Re: Associative arrays, multithreading, Rebindable

2014-04-12 Thread monarch_dodra

On Saturday, 12 April 2014 at 09:18:12 UTC, bearophile wrote:

Jack Applegame:


Should Rebindable to support associative arrays?


I guess so. But how to implement it?

Bye,
bearophile


Related: "Rebindable supports arrays (Which makes no sense)"
https://issues.dlang.org/show_bug.cgi?id=12046

Quote:
"It *might* make sense with associative arrays though... Not sure"


Re: alias this and constructor

2014-04-12 Thread monarch_dodra

On Saturday, 12 April 2014 at 14:00:06 UTC, Jack Applegame wrote:

And this doesn't compile too
http://dpaste.dzfl.pl/1d48eed7e0fb

The same reason?


Looks like it's something different. I think I know the cause.
Could you file it in the bug tracker? I will fix it.


Re: Local function overloading

2014-04-12 Thread monarch_dodra

On Saturday, 12 April 2014 at 16:45:02 UTC, Philpax wrote:
While trying to overload a function in local/function scope, I 
ran into this behaviour: http://dpaste.dzfl.pl/b4e8b9ddf78a and 
I was wondering what the cause was.


As far as I can tell, this should be fine in global scope (and 
it is), but I'm curious as to why it doesn't work inside a 
function.


I *think* it has something to do with how name-mangling is done. 
I don't know the details. I know you can workaround it by putting 
your functions a static members of a dummy struct: 
http://dpaste.dzfl.pl/268e3d2d4427


class A {}
class B {}

void main()
{
static struct Dummy
{
static void func2(A a){}
static void func2(B b){}
}
alias func2 = Dummy.func2;
A a;
func2(a);
}


Local function overloading

2014-04-12 Thread Philpax
While trying to overload a function in local/function scope, I 
ran into this behaviour: http://dpaste.dzfl.pl/b4e8b9ddf78a and I 
was wondering what the cause was.


As far as I can tell, this should be fine in global scope (and it 
is), but I'm curious as to why it doesn't work inside a function.


Re: alias this and constructor

2014-04-12 Thread Jack Applegame

And this doesn't compile too
http://dpaste.dzfl.pl/1d48eed7e0fb

The same reason?



Re: alias this and constructor

2014-04-12 Thread monarch_dodra

On Saturday, 12 April 2014 at 13:08:48 UTC, Jack Applegame wrote:

On Saturday, 12 April 2014 at 12:47:02 UTC, MrSmith wrote:

Maybe, because compiler cannot distinguish between default 
constructor which takes int and your one, which also takes int.


I don't think so. Without alias this, but with constructor it 
compiles.


The issue is that a call that was essentially supposed to do 
nothing, accidentally calls your constructor. Because the call 
"that does nothing" was not expected to do much, it was marked as 
@safe, pure and nothrow.


This conflicts with your constructor, which is none of those.

That's the explanation for the behavior you are seeing, it 
shouldn't be happening. You can work around it by marking your 
constructor as @safe, pure and nothrow.


https://github.com/D-Programming-Language/phobos/pull/1529#discussion_r7175502


Re: alias this and constructor

2014-04-12 Thread Jack Applegame

On Saturday, 12 April 2014 at 12:47:02 UTC, MrSmith wrote:

Maybe, because compiler cannot distinguish between default 
constructor which takes int and your one, which also takes int.


I don't think so. Without alias this, but with constructor it 
compiles.


Re: alias this and constructor

2014-04-12 Thread monarch_dodra

On Saturday, 12 April 2014 at 11:48:36 UTC, Jack Applegame wrote:

Why this code doesn't want to compile?


Aye! That's my bad!

It's a bug I introduced, while fixing more bugs :/

The good news is it only affect 2.065.0. It's already fixed in 
HEAD.


1000 apologies.


Re: alias this and constructor

2014-04-12 Thread MrSmith

On Saturday, 12 April 2014 at 11:48:36 UTC, Jack Applegame wrote:

Why this code doesn't want to compile?

import std.algorithm;
import std.array;

struct Foo {
int a;
this(int v) {}
alias a this;
}

void main() {
immutable(Foo)[] foo;
auto arr = array(foo.filter!(o => true));
}

http://dpaste.dzfl.pl/25572b0f6d0b


Maybe, because compiler cannot distinguish between default 
constructor which takes int and your one, which also takes int.


Re: Last element of a forward range

2014-04-12 Thread monarch_dodra

On Saturday, 12 April 2014 at 10:27:00 UTC, bearophile wrote:

Steven Schveighoffer:


Interesting problem.


And it's not an academic one, I have already faced it two or 
three times :-)



Given that it is a forward range, a zip between it and a saved 
copy that is advanced by one may work.


This compiles and gives the expected output, but how do you 
extract the last item with this?


seq.zip(seq.dropOne).writeln;


For starters, you can't in the sense that writeln takes by value, 
and your zip has value semantics. So in fact, it is still 
unchanged. BTW: That UFCS "seq.zip(someOtherRange)": Ew.


So you'd need to start with a:
auto z = zip(seq, seq.dropOne);
while (!z.empty)
z.popFront();

That's step one. Step two would be actually extracting the 
remainaing range. In theory, you can't. In practice, you can hack 
around the implementation by:

zip.ranges[1].front;

Turns out "ranges" is not private. I wouldn't use this.

The second, is modifying the stopping policy on the fly:
zip.stoppingPolicy = StoppingPolicy.longest;
auto a = zip.front[1];

I think both are *very* hackish.
I'd do this:

auto bck = seq.save; seq.popFront();
while (!seq.empty)
{
bck.popFront();
seq.popFront();
}
auto last = bck.front;


Re: alias this and constructor

2014-04-12 Thread Jack Applegame

If you just comment out constructor, then it compiles.


alias this and constructor

2014-04-12 Thread Jack Applegame

Why this code doesn't want to compile?

import std.algorithm;
import std.array;

struct Foo {
int a;
this(int v) {}
alias a this;
}

void main() {
immutable(Foo)[] foo;
auto arr = array(foo.filter!(o => true));
}

http://dpaste.dzfl.pl/25572b0f6d0b



Re: Chris E. Miller ToolTip (D1)

2014-04-12 Thread hicman

will the library build for x64?


On Friday, 11 April 2014 at 12:33:27 UTC, FrankLike wrote:

On Thursday, 14 November 2013 at 16:17:53 UTC, Heinz wrote:
You have to manually set the tooltip's max width to a fixed 
value using the tooltip handle and Win32 API, by doing this 
you're telling the tooltip object it is a multiline tooltip 
and from now on it will accept \r\n as end of line:


   ttip = new ToolTip;
   SendMessageA(ttip.handle, TTM_SETMAXTIPWIDTH, 0, 250);
   ttip.setToolTip(find, "a=n (not approved)\r\no=n (not 
outlooked)\r\nt>0 (total > 0));


That's it, it works (i tested it).

By the way, the DFL version on Chris' site is for D1 but 
there's this version for D2 (i use both): 
https://github.com/Rayerd/dfl/tree/master/win32/dfl



Now, dfl can use the dmd 2.065
you can see the fork :
https://github.com/FrankLIKE/dfl

fork(from https://github.com/Rayerd/dfl/)

Waiting Miller commit it.




Re: Last element of a forward range

2014-04-12 Thread bearophile

Steven Schveighoffer:


Interesting problem.


And it's not an academic one, I have already faced it two or 
three times :-)



Given that it is a forward range, a zip between it and a saved 
copy that is advanced by one may work.


This compiles and gives the expected output, but how do you 
extract the last item with this?


seq.zip(seq.dropOne).writeln;

I have written I've added an ER to Phobos with some preliminary 
code:

https://issues.dlang.org/show_bug.cgi?id=12564

Bye,
bearophile


Re: Associative arrays, multithreading, Rebindable

2014-04-12 Thread Jack Applegame

On Saturday, 12 April 2014 at 09:18:12 UTC, bearophile wrote:

Jack Applegame:


Should Rebindable to support associative arrays?


I guess so. But how to implement it?

Bye,
bearophile

May be just like Rebindable for classes?


Re: Associative arrays, multithreading, Rebindable

2014-04-12 Thread bearophile

Jack Applegame:


Should Rebindable to support associative arrays?


I guess so. But how to implement it?

Bye,
bearophile


Associative arrays, multithreading, Rebindable

2014-04-12 Thread Jack Applegame
If I want to pass a dynamic array from one thread to another, 
then I use immutable(T)[] and it is safe.
But what if I need to pass an associative array? I suppose that 
the use of immutable(T)[U] is unsafe. Because one thread can 
delete the entry and this will have an impact also on the 
instance of the array in another thread.
So I have to use immutable(T[U]). But the immutability of the 
entire array makes the array reference immutable too:


immutable int[string] foo;
foo = assumeUnique(other); // error

For immutable classes we can use std.typecons.Rebindable:

Rebindable!(immutable Foo) foo;
foo = new immutable Foo; // fine

But for immutable associative arrays we can't:

Rebindable!(immutable int[string]) foo; // error
foo = assumeUnique(other);

Error: template instance 
std.typecons.Rebindable!(immutable(int[string])) does not match 
template declaration Rebindable(T) if (is(T == class) || is(T == 
interface) || isArray!T)


Should Rebindable to support associative arrays?