delegates, functions, and literals confusion

2013-07-03 Thread CJS
Confusion over delegates seems to be a somewhat common topic, 
judging from 
http://forum.dlang.org/thread/jbkahhlvevgectisd...@forum.dlang.org 
and 
http://stackoverflow.com/questions/6431884/function-and-delegate-literals-in-d. 
I'm also somewhat confused by functions vs. function literals and 
how to pass them around.


In my case I'm trying to figure out the best way to pass 
functions to other fundtions. So I wrote the following toy code 
to see what the compiler would do. I'm trying to understand why 
the compiler emitted errors on the code below (shown w/ 
comments). Any insight/suggestions on better ways to pass 
functions around would also be appreciated.


import std.datetime;
import std.stdio;


void f(int delegate(int) h){
writefln("h(0)=%d", h(0));
}

void g(int function (int) h){
writefln("h(0)=%d", h(0));
}

void main(){

int foo(int a){ return a+6;}
auto bah = function int(int b){ return b+7;};

f(foo); //error
f(&foo);
f(bah); //error
f(&bah); // error

g(foo); //error
g(&foo); //error
g(bah);
g(&bah); //error

}


std.conv.to vs. casting

2013-07-03 Thread CJS
I'm having trouble understanding the difference between casting 
and std.conv.to. Any help?


Re: reading a structure (eg header info) from file

2013-07-03 Thread Jonathan M Davis
On Wednesday, July 03, 2013 15:37:28 captaindet wrote:
> hi,
> 
> whilst converting some of my C code into D i got stuck.
> 
> in C:
> 
> typedef struct { /* info */ } INFO;
> INFO info;
> size_t checkio;
> // read INFO from data file:
> pf_datafile = fopen("datafile","rb");
> checkio = fread((char *) &info, sizeof(info), 1, pf_datafile);
> 
> how do i do this in D? i'd like to avoid falling back to calling C
> functions, but i cannot figure out how do this with phobos functions. mind
> you, eventually the INFO struct might be anywhere in the file, not only at
> the beginning.

You should probably look at std.stdio.File. It looks like its readf function 
might be what you're looking for. If not, there are quite a few functions on 
there, and it's likely that you could get one of them to do what you want.

Or you could always use std.mmfile.MmFile and operate on the file as an array 
and do it all that way (memory-mapped I/O is one of the coolest things ever 
IMHO).

- Jonathan M Davis


Re: reading a structure (eg header info) from file

2013-07-03 Thread Ali Çehreli

On 07/03/2013 01:37 PM, captaindet wrote:

> in C:
>
> typedef struct { /* info */ } INFO;
> INFO info;
> size_t checkio;
> // read INFO from data file:
> pf_datafile = fopen("datafile","rb");
> checkio = fread((char *) &info, sizeof(info), 1, pf_datafile);

Just a reminder: The operation above is not portable. There are 
endianness and struct member padding issues.


> how do i do this in D?

Assuming that the writer and the reader agree that it works, the closest 
in Phobos is std.stdio.rawRead.


> the INFO struct might be anywhere in the file, not only at the beginning.

seek() does that.

import std.stdio;

struct Info
{
size_t a;
double b;
byte c;
}

void main()
{
auto info0 = Info(1, 2.25, 3);
auto info1 = Info(4, 5.50, 6);

writeTest("test_file", [ info0, info1 ]);
readTest("test_file", 0);
readTest("test_file", 1);
}

void writeTest(string fileName, const(Info)[] infos)
{
auto file = File(fileName, "w");

file.rawWrite(infos);
}

void readTest(string fileName, size_t index)
{
auto file = File(fileName, "r");

auto offset = index * Info.sizeof;
file.seek(offset);

// More than 1 would read as many elements
auto info = new Info[1];
file.rawRead(info);

writefln("Read Info at index %s: %s", index, info);
}

Ali



Re: reading a structure (eg header info) from file

2013-07-03 Thread Rikki Cattermole

On Wednesday, 3 July 2013 at 20:37:24 UTC, captaindet wrote:

hi,

whilst converting some of my C code into D i got stuck.

in C:

typedef struct { /* info */ } INFO;
INFO info;
size_t checkio;
// read INFO from data file:
pf_datafile = fopen("datafile","rb");
checkio = fread((char *) &info, sizeof(info), 1, pf_datafile);

how do i do this in D? i'd like to avoid falling back to 
calling C functions, but i cannot figure out how do this with 
phobos functions. mind you, eventually the INFO struct might be 
anywhere in the file, not only at the beginning.


cheers,

det


Take a look at http://dlang.org/phobos/std_file.html#.read
There is also http://dlang.org/phobos/std_file.html#.readText if 
you wish to read a whole file as text.


When I tested it things like string didn't work but numbers did.
Maybe someone else here can help you if you need that.


inverse of std.demangle?

2013-07-03 Thread Timothee Cour
I'd like to have a function:
string mangle(string mangled_string);
unittest{
  void foo(int x){}
  assert(foo.mangleof.demangle.mangle == foo.mangleof);
}

is there such a functionality, even partially?


Re: Can't print inout parameter

2013-07-03 Thread gerleim

inout(int) foo (inout int a)
{
writeln(a);
return a;
}


I don't know if this is the official method, but
writeln(cast(const)x);
works.

Trying to get answers at: 
http://stackoverflow.com/questions/17460065/how-to-print-inout-parameters


stdout in binary mode

2013-07-03 Thread bearophile

How do you open stdout in binary mode with D/Phobos?

In C++ you use something like:

setmode(fileno(stdout), O_BINARY);

(I don't even know where to find O_BINARY in core.stdc).

Bye and thank you,
bearophile


Re: Address of overloaded functions

2013-07-03 Thread Tyro[17]

On 7/3/13 12:52 PM, Artur Skawina wrote:

import std.stdio;

void foo(int a){ writeln("overload int"); }
void foo(long b){ writeln("overload long"); }

auto pickOverload(alias FP, A...)() @property { typeof(FP(A.init)) function(A) 
fp = &FP; return fp;}

void main()
{
auto b = pickOverload!(foo, long);
b(2);
}


Often I see terse demonstrations of ingenious ways of doing things in 
the language but am at a lost as with regards to under what 
circumstances on would use such a feature. This happens to be one of 
those cases. Could you provide a couple circumstances where this would 
prove useful/handy?



Thanks,

--

Andrew Edwards

http://www.akeron.co
auto getAddress() {
string location = "@", period = ".";
return ("info" ~ location ~ "afidem" ~ period ~ "org");
}


Re: Address of overloaded functions

2013-07-03 Thread Artur Skawina
On 07/03/13 22:44, H. S. Teoh wrote:
> On Wed, Jul 03, 2013 at 10:10:08PM +0200, Artur Skawina wrote:
>> On 07/03/13 21:02, H. S. Teoh wrote:
>>> On Wed, Jul 03, 2013 at 06:52:56PM +0200, Artur Skawina wrote:
void main()
{
auto b = pickOverload!(foo, long);
>>>
>>> Now *that's* what I call coolness. Self-documenting and convenient
>>> to use (though in this case it's arguable whether it's actually
>>> better than native syntax).
>>
>> At some point somebody is going to ask for
>>
>>auto b = foo.pickOverload!(long)
>>
>> with a better name for 'pickOverload'. :)
>>
>>
>> Which actually is possible, but would need sane optional-() and
>> UFCS models. Ie not right now.
> [...]
> 
> I don't think UFCS applies to compile-time arguments? So this wouldn't
> work.

Like i said - not right now. Extending UFCS to be explicit can be done,
and is a good idea for other reasons. Once something like that exists
then the problem is the 'foo' symbol -- that's why the optional-parens
get in the way.

I'm just saying that doing that might be possible, not that it will
happen in the current D incarnation. For some reason some people
seem to like the optional parens. :^)

artur


Re: Address of overloaded functions

2013-07-03 Thread H. S. Teoh
On Wed, Jul 03, 2013 at 10:10:08PM +0200, Artur Skawina wrote:
> On 07/03/13 21:02, H. S. Teoh wrote:
> > On Wed, Jul 03, 2013 at 06:52:56PM +0200, Artur Skawina wrote:
> >>void main()
> >>{
> >>auto b = pickOverload!(foo, long);
> > 
> > Now *that's* what I call coolness. Self-documenting and convenient
> > to use (though in this case it's arguable whether it's actually
> > better than native syntax).
> 
> At some point somebody is going to ask for
> 
>auto b = foo.pickOverload!(long)
> 
> with a better name for 'pickOverload'. :)
> 
> 
> Which actually is possible, but would need sane optional-() and
> UFCS models. Ie not right now.
[...]

I don't think UFCS applies to compile-time arguments? So this wouldn't
work.


T

-- 
There's light at the end of the tunnel. It's the oncoming train.


reading a structure (eg header info) from file

2013-07-03 Thread captaindet

hi,

whilst converting some of my C code into D i got stuck.

in C:

typedef struct { /* info */ } INFO;
INFO info;
size_t checkio;
// read INFO from data file:
pf_datafile = fopen("datafile","rb");
checkio = fread((char *) &info, sizeof(info), 1, pf_datafile);

how do i do this in D? i'd like to avoid falling back to calling C functions, 
but i cannot figure out how do this with phobos functions. mind you, eventually 
the INFO struct might be anywhere in the file, not only at the beginning.

cheers,

det  





Re: Address of overloaded functions

2013-07-03 Thread Artur Skawina
On 07/03/13 21:02, H. S. Teoh wrote:
> On Wed, Jul 03, 2013 at 06:52:56PM +0200, Artur Skawina wrote:
>>void main()
>>{
>>auto b = pickOverload!(foo, long);
> 
> Now *that's* what I call coolness. Self-documenting and convenient to
> use (though in this case it's arguable whether it's actually better than
> native syntax).

At some point somebody is going to ask for

   auto b = foo.pickOverload!(long)

with a better name for 'pickOverload'. :)


Which actually is possible, but would need sane optional-() and
UFCS models. Ie not right now.

artur


Re: ref tuples

2013-07-03 Thread Artur Skawina
On 07/03/13 19:10, Brad Anderson wrote:
> On Wednesday, 3 July 2013 at 16:35:18 UTC, Artur Skawina wrote:
>> On 07/03/13 18:29, Brad Anderson wrote:
>>> On Wednesday, 3 July 2013 at 11:54:39 UTC, Artur Skawina wrote:
 On 07/03/13 02:22, Brad Anderson wrote:
> C++11's std::tuple includes a function std::tie that takes references to 
> the arguments and returns a tuple that maintains the references to the 
> arguments.
>
> Along with the usual cases where you'd want reference semantics it also 
> enables this interesting construct for unpacking tuples.
>
> int a, b;
> tie(a, b) = make_tuple(1, 2);
>
> assert(a == 1 && b == 2);
>
> Is there any way to do something similar with std.typecons.Tuple?

 Well, aliases can be used to get a similar effect.

template tie(A...) { alias tie = A; }
tie!(a, b) = tuple(1, 2);
>>>
>>> That won't work.  a and b aren't held as references (also you passed them 
>>> as type parameters :P).
>>
>> Try it...
>>
>> And, yes, the fact that 'A...' template parms accept symbols
>> is not exactly obvious. But it's much more useful that way.
>>
>> artur
> 
> Huh, I had no idea you could do something like that. I stand corrected. 
> Thanks.
> 
> That does get tie = working but doesn't work if you want to pass it around 
> which is actually more at the heart of what I'm interested in.
> 
> To get straight to the point, I was playing around with implementing 
> bearophile's enumerate() feature request (something I've wanted myself).  
> Both his posted solution [1] and my own quick testing hack (auto 
> enumerate(Range)(Range r) { return zip(sequence!"n"(), r); }) lose the 
> ability to do ref elements in foreach that can modify the source range:
> 
> ---
> auto a = ["a", "b", "c"];
> foreach(i, ref item; a.enumerate())
>   item = to!string(i);
> 
> assert(a == ["0", "1", "2"]); // fails, a is still ["a", "b", "c"]
> ---
> 
> They don't work because both the tuples he returns from front and the tuples 
> zip creates aren't references to the originals so you are just changing the 
> copy stored in the tuple.
> 
> Something like "alias RefIntTuple = Tuple!(ref int, ref int);" gives a 
> compiler error (Error: expression expected, not 'ref').
> 
> 1. http://d.puremagic.com/issues/show_bug.cgi?id=5550#c2

D does not yet have proper ref types, which means a lot of things
are not possible, at least not directly. And a lot of hacks are
required to achieve certain effects. Anyway, the following seems
to work - it's bearophiles code from the mentioned bugzilla entry
with some tweaks.

It's dirty enough, so I shouldn't be posting this on a 'learn'
ML... It's meant more as an illustration of the language deficiencies.
Please do not use anything like this. :)


   import std.stdio, std.algorithm, std.range, std.typecons, std.traits, 
std.array;

   struct RefHack(T) {
  T* ptr;
  ref get() @property { return *ptr; }
  alias get this;
   }
   auto refHack(T)(ref T a) { return RefHack!T(&a); }

   struct Enumerate(R) {
   R r;
   int i;

   @property bool empty() { return r.empty; }

   @property Tuple!(typeof(this.i), typeof(refHack(r.front))) front() {
   return typeof(return)(i, refHack(r.front));
   }

   void popFront() {
   this.r.popFront();
   this.i++;
   }
   }

   Enumerate!R enumerate(R)(R range, int start=0) if (isInputRange!R) {
   return Enumerate!R(range, start);
   }

   void main() {
   auto flags = [0, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1];

   flags.enumerate(2).filter!q{!a[1]}().map!q{a[0]}().writeln();

   {
  import std.conv;
  auto a = ["a", "b", "c"];

  foreach(i, ref item; a.enumerate())
 item = to!string(i);

  assert(a == ["0", "1", "2"]);
   }
   }


artur


Re: Error: this for needs to be type S not type MapResult!...

2013-07-03 Thread Michael

On Saturday, 29 June 2013 at 19:44:00 UTC, Peter Neubauer wrote:

Please explain why this error happens in the following code:

import std.algorithm;

struct S
{
  void foo ()
  {
int f1 (int a) { return conv(a); }
int delegate (int) f2 = &conv;

int[] x = [1, 2, 3];
x.map!conv;// ERROR
x.map!f1;  // fine
x.map!f2;  // also fine
  }

  int conv (int a)
  {
return a+1;
  }
}

--- compile output:

/usr/include/d/std/algorithm.d(404): Error: this for conv needs 
to be type S not type MapResult!(conv, int[])
/usr/include/d/std/algorithm.d(438): Error: this for conv needs 
to be type S not type MapResult!(conv, int[])
/usr/include/d/std/algorithm.d(450): Error: this for conv needs 
to be type S not type MapResult!(conv, int[])
/usr/include/d/std/algorithm.d(390): Error: template instance 
std.algorithm.MapResult!(conv, int[]) error instantiating
/home/peter/proggen/goliza.reduced/gtp.d(12):
instantiated from here: map!(int[])
/home/peter/proggen/goliza.reduced/gtp.d(12): Error: template 
instance std.algorithm.map!(conv).map!(int[]) error 
instantiating


---

Thanks,
-Peter


I am not sure if this behavior is intended. Since "conv" is a 
instance method of S, it has an implicit "this" which must be of 
type S. I suspect that the map template tries to invoke conv in 
the context of MapResult. I hope template masters can give you a 
more complete explanation though.


Re: Address of overloaded functions

2013-07-03 Thread H. S. Teoh
On Wed, Jul 03, 2013 at 06:52:56PM +0200, Artur Skawina wrote:
> On 07/03/13 18:24, H. S. Teoh wrote:
> > On Wed, Jul 03, 2013 at 06:07:07PM +0200, Artur Skawina wrote:
> >> The context dependence isn't ideal, but what's the alternative?...
> > [...]
> > 
> > Explicit syntax for specifying overloads? ;-) Not like that would
> > happen in D, though.
> 
> Real Programmers need no special syntax :)
> 
>import std.stdio;
> 
>void foo(int a){ writeln("overload int"); }
>void foo(long b){ writeln("overload long"); }
> 
>auto pickOverload(alias FP, A...)() @property { typeof(FP(A.init)) 
> function(A) fp = &FP; return fp;}

Wow. I didn't know you could use A.init for variadic A... !

That's amazing. D rawkz!!


>void main()
>{
>auto b = pickOverload!(foo, long);

Now *that's* what I call coolness. Self-documenting and convenient to
use (though in this case it's arguable whether it's actually better than
native syntax).


>b(2);
>}
[...]


T

-- 
What doesn't kill me makes me stranger.


Re: Stop to! rounding?

2013-07-03 Thread H. S. Teoh
On Wed, Jul 03, 2013 at 07:56:28PM +0200, Marco Leise wrote:
> Am Tue, 2 Jul 2013 22:21:52 -0700
> schrieb "H. S. Teoh" :
> 
> > On Tue, Jul 02, 2013 at 10:14:33PM -0700, Ali Çehreli wrote:
> > [...]
> > > import std.stdio;
> > > import std.conv;
> > > 
> > > void main()
> > > {
> > > auto a = to!double("151.42499");
> > > writefln("%.60f", a);
> > > }
> > 
> > I wouldn't write it like that; IMO it's better to write:
> > 
> > writefln("%.*f", double.dig, a);
> > 
> > So that you don't give the wrong impression that there are more digits
> > than are actually there. Using double.dig also lets you see all the
> > digits that *are* there, not a rounded value, that the OP was
> > complaining about.
> > 
> > 
> > T
> > 
> 
> *f ? I didn't know that you can format the format string like
> that. Thanks for the tip.
[...]

Basically, it's a wildcard precision. Usually you'd write %.5f for 5
digits following the decimal point, for example, but that requires
hardcoding the integer literal into the format string. In some cases you
need it to be variable, so '*' was introduced as a stand-in for the next
item in the argument list. So writefln("%.*f", 5, a) is equivalent to
writefln("%.5f", a).

The wildcard * can also be used in the field-width, so writefln("%*.*f",
5, 2, 1) is equivalent to writefln("%5.2f", 1).

This isn't D's innovation, BTW, the * width/precision specifier is
defined in the same way in C's printf().


T

-- 
He who does not appreciate the beauty of language is not worthy to bemoan its 
flaws.


Re: ref tuples

2013-07-03 Thread Simen Kjaeraas

On 2013-07-03, 02:22, Brad Anderson wrote:

C++11's std::tuple includes a function std::tie that takes references to  
the arguments and returns a tuple that maintains the references to the  
arguments.


Along with the usual cases where you'd want reference semantics it also  
enables this interesting construct for unpacking tuples.


int a, b;
tie(a, b) = make_tuple(1, 2);

assert(a == 1 && b == 2);

Is there any way to do something similar with std.typecons.Tuple?


Not that I know of. But, Philippe Sigaud's dranges[1] library includes
a RefTuple[2], which should do what you want.

[1]: https://github.com/dawgfoto/dranges/
[2]: https://github.com/dawgfoto/dranges/blob/master/reftuple.d

--
Simen


Re: Stop to! rounding?

2013-07-03 Thread Marco Leise
Am Tue, 2 Jul 2013 22:21:52 -0700
schrieb "H. S. Teoh" :

> On Tue, Jul 02, 2013 at 10:14:33PM -0700, Ali Çehreli wrote:
> [...]
> > import std.stdio;
> > import std.conv;
> > 
> > void main()
> > {
> > auto a = to!double("151.42499");
> > writefln("%.60f", a);
> > }
> 
> I wouldn't write it like that; IMO it's better to write:
> 
>   writefln("%.*f", double.dig, a);
> 
> So that you don't give the wrong impression that there are more digits
> than are actually there. Using double.dig also lets you see all the
> digits that *are* there, not a rounded value, that the OP was
> complaining about.
> 
> 
> T
> 

*f ? I didn't know that you can format the format string like
that. Thanks for the tip.

-- 
Marco



Re: How to get warnings about unused imports ?

2013-07-03 Thread Namespace
You have to parse the unnamed import files, list all their 
identifiers (global variables, public functions etc.) and search 
for them. ;)
That could be a bit complicated. ;) Therefore I don't want to do 
it. Even with named imports you can get false positives.


But if you like it, you could improve it. The Lexer is my own and 
not that good, that would be start. On the wiki you can find 
other Lexer / Parsers, which are really good, but I haven't the 
time to replace my own and adapt the source or to work much on 
the project.


Re: Ping qznc: Re: A little of coordination for Rosettacode

2013-07-03 Thread Marco Leise
Am Sat, 22 Jun 2013 23:27:00 +0200
schrieb "bearophile" :

> Ali Çehreli:
> 
> > The code compiles under 32-bit (e.g. with the -m32 compiler 
> > switch) where size_t is an alias of uint.
> 
> Oh, I see. I compile most of the code on a 32 bit system.
> 
> I asked Walter to warn d programmers against such mistakes, and 
> Walter closed it down. Someone else has opened the ER again...

That would be me. Let's see where it goes.
 
> Bye,
> bearophile

-- 
Marco



Re: ref tuples

2013-07-03 Thread Brad Anderson

On Wednesday, 3 July 2013 at 16:35:18 UTC, Artur Skawina wrote:

On 07/03/13 18:29, Brad Anderson wrote:

On Wednesday, 3 July 2013 at 11:54:39 UTC, Artur Skawina wrote:

On 07/03/13 02:22, Brad Anderson wrote:
C++11's std::tuple includes a function std::tie that takes 
references to the arguments and returns a tuple that 
maintains the references to the arguments.


Along with the usual cases where you'd want reference 
semantics it also enables this interesting construct for 
unpacking tuples.


int a, b;
tie(a, b) = make_tuple(1, 2);

assert(a == 1 && b == 2);

Is there any way to do something similar with 
std.typecons.Tuple?


Well, aliases can be used to get a similar effect.

   template tie(A...) { alias tie = A; }
   tie!(a, b) = tuple(1, 2);


That won't work.  a and b aren't held as references (also you 
passed them as type parameters :P).


Try it...

And, yes, the fact that 'A...' template parms accept symbols
is not exactly obvious. But it's much more useful that way.

artur


Huh, I had no idea you could do something like that. I stand 
corrected. Thanks.


That does get tie = working but doesn't work if you want to pass 
it around which is actually more at the heart of what I'm 
interested in.


To get straight to the point, I was playing around with 
implementing bearophile's enumerate() feature request (something 
I've wanted myself).  Both his posted solution [1] and my own 
quick testing hack (auto enumerate(Range)(Range r) { return 
zip(sequence!"n"(), r); }) lose the ability to do ref elements in 
foreach that can modify the source range:


---
auto a = ["a", "b", "c"];
foreach(i, ref item; a.enumerate())
  item = to!string(i);

assert(a == ["0", "1", "2"]); // fails, a is still ["a", "b", "c"]
---

They don't work because both the tuples he returns from front and 
the tuples zip creates aren't references to the originals so you 
are just changing the copy stored in the tuple.


Something like "alias RefIntTuple = Tuple!(ref int, ref int);" 
gives a compiler error (Error: expression expected, not 'ref').


1. http://d.puremagic.com/issues/show_bug.cgi?id=5550#c2


Re: Address of overloaded functions

2013-07-03 Thread Artur Skawina
On 07/03/13 18:24, H. S. Teoh wrote:
> On Wed, Jul 03, 2013 at 06:07:07PM +0200, Artur Skawina wrote:
>> The context dependence isn't ideal, but what's the alternative?...
> [...]
> 
> Explicit syntax for specifying overloads? ;-) Not like that would happen
> in D, though.

Real Programmers need no special syntax :)

   import std.stdio;

   void foo(int a){ writeln("overload int"); }
   void foo(long b){ writeln("overload long"); }

   auto pickOverload(alias FP, A...)() @property { typeof(FP(A.init)) 
function(A) fp = &FP; return fp;}

   void main()
   {
   auto b = pickOverload!(foo, long);
   b(2);
   }

But using the context to automatically figure out the right
overload make some things easier; it's just that eg the lhs of
an assignment affecting the result of the rhs-expression doesn't
/feel/ right.

artur


Re: ref tuples

2013-07-03 Thread Artur Skawina
On 07/03/13 18:29, Brad Anderson wrote:
> On Wednesday, 3 July 2013 at 11:54:39 UTC, Artur Skawina wrote:
>> On 07/03/13 02:22, Brad Anderson wrote:
>>> C++11's std::tuple includes a function std::tie that takes references to 
>>> the arguments and returns a tuple that maintains the references to the 
>>> arguments.
>>>
>>> Along with the usual cases where you'd want reference semantics it also 
>>> enables this interesting construct for unpacking tuples.
>>>
>>> int a, b;
>>> tie(a, b) = make_tuple(1, 2);
>>>
>>> assert(a == 1 && b == 2);
>>>
>>> Is there any way to do something similar with std.typecons.Tuple?
>>
>> Well, aliases can be used to get a similar effect.
>>
>>template tie(A...) { alias tie = A; }
>>tie!(a, b) = tuple(1, 2);
> 
> That won't work.  a and b aren't held as references (also you passed them as 
> type parameters :P).

Try it...

And, yes, the fact that 'A...' template parms accept symbols
is not exactly obvious. But it's much more useful that way.

artur


Re: ref tuples

2013-07-03 Thread Brad Anderson

On Wednesday, 3 July 2013 at 11:54:39 UTC, Artur Skawina wrote:

On 07/03/13 02:22, Brad Anderson wrote:
C++11's std::tuple includes a function std::tie that takes 
references to the arguments and returns a tuple that maintains 
the references to the arguments.


Along with the usual cases where you'd want reference 
semantics it also enables this interesting construct for 
unpacking tuples.


int a, b;
tie(a, b) = make_tuple(1, 2);

assert(a == 1 && b == 2);

Is there any way to do something similar with 
std.typecons.Tuple?


Well, aliases can be used to get a similar effect.

   template tie(A...) { alias tie = A; }
   tie!(a, b) = tuple(1, 2);

artur


That won't work.  a and b aren't held as references (also you 
passed them as type parameters :P).


Re: Address of overloaded functions

2013-07-03 Thread H. S. Teoh
On Wed, Jul 03, 2013 at 06:07:07PM +0200, Artur Skawina wrote:
> On 07/03/13 17:43, H. S. Teoh wrote:
> > On Wed, Jul 03, 2013 at 05:41:25PM +0200, Artur Skawina wrote:
> >> On 07/03/13 17:27, H. S. Teoh wrote:
> >>> On Wed, Jul 03, 2013 at 05:15:48PM +0200, John Colvin wrote:
>  On Wednesday, 3 July 2013 at 15:03:46 UTC, Artur Skawina wrote:
> > On 07/03/13 16:52, John Colvin wrote:
> >> Is there any way to take the address of any of an overloaded set
> >> of functions?
> >>
> >> import std.stdio;
> >>
> >> void foo(int a){ writeln("overload int"); }
> >> void foo(long b){ writeln("overload long"); }
> >>
> >> void main()
> >> {
> >>auto b = &foo; //ambiguous => error
> >>b(2); //valid for either overload
> >> }
> >
> >void function(long) b = &foo;
> >
> > artur
> 
>  Thanks, that works
> >>>
> >>> This is interesting. How does C++ handle this? (Or does it?)
> >>
> >> The same - the context determines which overload is chosen, and 
> >> ambiguity is an error.
> > 
> > Oh, so it tells the difference by whether you write
> > 
> > void (*p)(int) = foo;
> > 
> > or
> > 
> > void (*p)(long) = foo;
> > 
> > ?
> 
> Yep. Things like
> 
>void c(void (*fp)(long), long a) { fp(a); }
>c(foo, 2);
> 
> work as expected too.
> 
> > I guess that makes sense.
> 
> The context dependence isn't ideal, but what's the alternative?...
[...]

Explicit syntax for specifying overloads? ;-) Not like that would happen
in D, though.


T

-- 
Error: Keyboard not attached. Press F1 to continue. -- Yoon Ha Lee, CONLANG


Re: Address of overloaded functions

2013-07-03 Thread Artur Skawina
On 07/03/13 17:43, H. S. Teoh wrote:
> On Wed, Jul 03, 2013 at 05:41:25PM +0200, Artur Skawina wrote:
>> On 07/03/13 17:27, H. S. Teoh wrote:
>>> On Wed, Jul 03, 2013 at 05:15:48PM +0200, John Colvin wrote:
 On Wednesday, 3 July 2013 at 15:03:46 UTC, Artur Skawina wrote:
> On 07/03/13 16:52, John Colvin wrote:
>> Is there any way to take the address of any of an overloaded set
>> of functions?
>>
>> import std.stdio;
>>
>> void foo(int a){ writeln("overload int"); }
>> void foo(long b){ writeln("overload long"); }
>>
>> void main()
>> {
>>auto b = &foo; //ambiguous => error
>>b(2); //valid for either overload
>> }
>
>void function(long) b = &foo;
>
> artur

 Thanks, that works
>>>
>>> This is interesting. How does C++ handle this? (Or does it?)
>>
>> The same - the context determines which overload is chosen, and 
>> ambiguity is an error.
> 
> Oh, so it tells the difference by whether you write
> 
>   void (*p)(int) = foo;
> 
> or
> 
>   void (*p)(long) = foo;
> 
> ?

Yep. Things like

   void c(void (*fp)(long), long a) { fp(a); }
   c(foo, 2);

work as expected too.

> I guess that makes sense.

The context dependence isn't ideal, but what's the alternative?...

artur


Re: Address of overloaded functions

2013-07-03 Thread H. S. Teoh
On Wed, Jul 03, 2013 at 05:41:25PM +0200, Artur Skawina wrote:
> On 07/03/13 17:27, H. S. Teoh wrote:
> > On Wed, Jul 03, 2013 at 05:15:48PM +0200, John Colvin wrote:
> >> On Wednesday, 3 July 2013 at 15:03:46 UTC, Artur Skawina wrote:
> >>> On 07/03/13 16:52, John Colvin wrote:
>  Is there any way to take the address of any of an overloaded set
>  of functions?
> 
>  import std.stdio;
> 
>  void foo(int a){ writeln("overload int"); }
>  void foo(long b){ writeln("overload long"); }
> 
>  void main()
>  {
> auto b = &foo; //ambiguous => error
> b(2); //valid for either overload
>  }
> >>>
> >>>void function(long) b = &foo;
> >>>
> >>> artur
> >>
> >> Thanks, that works
> > 
> > This is interesting. How does C++ handle this? (Or does it?)
> 
> The same - the context determines which overload is chosen, and 
> ambiguity is an error.

Oh, so it tells the difference by whether you write

void (*p)(int) = foo;

or

void (*p)(long) = foo;

?

I guess that makes sense.


T

-- 
There are 10 kinds of people in the world: those who can count in binary, and 
those who can't.


Re: Address of overloaded functions

2013-07-03 Thread Artur Skawina
On 07/03/13 17:27, H. S. Teoh wrote:
> On Wed, Jul 03, 2013 at 05:15:48PM +0200, John Colvin wrote:
>> On Wednesday, 3 July 2013 at 15:03:46 UTC, Artur Skawina wrote:
>>> On 07/03/13 16:52, John Colvin wrote:
 Is there any way to take the address of any of an overloaded set
 of functions?

 import std.stdio;

 void foo(int a){ writeln("overload int"); }
 void foo(long b){ writeln("overload long"); }

 void main()
 {
auto b = &foo; //ambiguous => error
b(2); //valid for either overload
 }
>>>
>>>void function(long) b = &foo;
>>>
>>> artur
>>
>> Thanks, that works
> 
> This is interesting. How does C++ handle this? (Or does it?)

The same - the context determines which overload is chosen, and 
ambiguity is an error.

artur


Re: Address of overloaded functions

2013-07-03 Thread H. S. Teoh
On Wed, Jul 03, 2013 at 05:15:48PM +0200, John Colvin wrote:
> On Wednesday, 3 July 2013 at 15:03:46 UTC, Artur Skawina wrote:
> >On 07/03/13 16:52, John Colvin wrote:
> >>Is there any way to take the address of any of an overloaded set
> >>of functions?
> >>
> >>import std.stdio;
> >>
> >>void foo(int a){ writeln("overload int"); }
> >>void foo(long b){ writeln("overload long"); }
> >>
> >>void main()
> >>{
> >>auto b = &foo; //ambiguous => error
> >>b(2); //valid for either overload
> >>}
> >
> >void function(long) b = &foo;
> >
> >artur
> 
> Thanks, that works

This is interesting. How does C++ handle this? (Or does it?)


T

-- 
Debian GNU/Linux: Cray on your desktop.


Re: Address of overloaded functions

2013-07-03 Thread Artur Skawina
On 07/03/13 17:17, John Colvin wrote:
> On Wednesday, 3 July 2013 at 15:05:00 UTC, Dicebot wrote:
>> On Wednesday, 3 July 2013 at 14:52:32 UTC, John Colvin wrote:
>>> Is there any way to take the address of any of an overloaded set of 
>>> functions?
>>>
>>> import std.stdio;
>>>
>>> void foo(int a){ writeln("overload int"); }
>>> void foo(long b){ writeln("overload long"); }
>>>
>>> void main()
>>> {
>>>auto b = &foo; //ambiguous => error
>>>b(2); //valid for either overload
>>> }
>>
>> http://dpaste.dzfl.pl/1e705a3b
> 
> It's a pity that only work within an aggregate (the documentation actually 
> says only classes)

http://forum.dlang.org/thread/xamuenbcabnhrtqjj...@forum.dlang.org#post-mailman.1122.1332633715.4860.digitalmars-d-learn:40puremagic.com

artur


Re: Address of overloaded functions

2013-07-03 Thread John Colvin

On Wednesday, 3 July 2013 at 15:03:46 UTC, Artur Skawina wrote:

On 07/03/13 16:52, John Colvin wrote:
Is there any way to take the address of any of an overloaded 
set of functions?


import std.stdio;

void foo(int a){ writeln("overload int"); }
void foo(long b){ writeln("overload long"); }

void main()
{
auto b = &foo; //ambiguous => error
b(2); //valid for either overload
}


void function(long) b = &foo;

artur


Thanks, that works


Re: Address of overloaded functions

2013-07-03 Thread John Colvin

On Wednesday, 3 July 2013 at 15:05:00 UTC, Dicebot wrote:

On Wednesday, 3 July 2013 at 14:52:32 UTC, John Colvin wrote:
Is there any way to take the address of any of an overloaded 
set of functions?


import std.stdio;

void foo(int a){ writeln("overload int"); }
void foo(long b){ writeln("overload long"); }

void main()
{
   auto b = &foo; //ambiguous => error
   b(2); //valid for either overload
}


http://dpaste.dzfl.pl/1e705a3b


It's a pity that only work within an aggregate (the documentation 
actually says only classes)


Re: Address of overloaded functions

2013-07-03 Thread Dicebot

On Wednesday, 3 July 2013 at 14:52:32 UTC, John Colvin wrote:
Is there any way to take the address of any of an overloaded 
set of functions?


import std.stdio;

void foo(int a){ writeln("overload int"); }
void foo(long b){ writeln("overload long"); }

void main()
{
auto b = &foo; //ambiguous => error
b(2); //valid for either overload
}


http://dpaste.dzfl.pl/1e705a3b


Re: Address of overloaded functions

2013-07-03 Thread Artur Skawina
On 07/03/13 17:03, Artur Skawina wrote:
> On 07/03/13 16:52, John Colvin wrote:
>> Is there any way to take the address of any of an overloaded set of 
>> functions?
>>
>> import std.stdio;
>>
>> void foo(int a){ writeln("overload int"); }
>> void foo(long b){ writeln("overload long"); }
>>
>> void main()
>> {
>> auto b = &foo; //ambiguous => error
>> b(2); //valid for either overload
>> }
> 
> void function(long) b = &foo;

And if you meant for the overload resolution to happen at call-time,
that's obviously not directly possible - there's no address of a /set/
of functions. You can use an alias, though:

alias b = foo;

artur



Re: Address of overloaded functions

2013-07-03 Thread Artur Skawina
On 07/03/13 16:52, John Colvin wrote:
> Is there any way to take the address of any of an overloaded set of functions?
> 
> import std.stdio;
> 
> void foo(int a){ writeln("overload int"); }
> void foo(long b){ writeln("overload long"); }
> 
> void main()
> {
> auto b = &foo; //ambiguous => error
> b(2); //valid for either overload
> }

void function(long) b = &foo;

artur


Address of overloaded functions

2013-07-03 Thread John Colvin
Is there any way to take the address of any of an overloaded set 
of functions?


import std.stdio;

void foo(int a){ writeln("overload int"); }
void foo(long b){ writeln("overload long"); }

void main()
{
auto b = &foo; //ambiguous => error
b(2); //valid for either overload
}


Re: Stop to! rounding?

2013-07-03 Thread Jay Norwood

On Wednesday, 3 July 2013 at 08:23:40 UTC, monarch_dodra wrote:
On Wednesday, 3 July 2013 at 06:18:28 UTC, Jonathan M Davis 
wrote:

On Wednesday, July 03, 2013 08:11:50 Josh wrote:


Long story short, I think both would be a great addition to 
phobos/D. I'd personally really want to play with decimal 
floats: They are slower, less precise and have less range, but 
are more "exact" in base 10 than traditional floats. And when 
printed in Base 10, are *always* exact.


This arbitrary precision c++ arithmetic package has a flexible 
feature of allowing selection of the internal base.  The default 
base 10.  Much of it is implemented in c++ templates. I put a 
small excerpt below.


http://www.hvks.com/Numerical/arbitrary_precision.html

/// Description:
///   Add operator for float_precision + 
///   no const on the lhs parameter to prevent ambigous overload
///
template  inline float_precision operator+( 
float_precision& lhs, const _Ty& rhs )

   {
   float_precision c(rhs);

   if( lhs.precision() > c.precision() )
  c.precision( lhs.precision() );

   return c += lhs;
   }


Re: ref tuples

2013-07-03 Thread Dicebot

On Wednesday, 3 July 2013 at 11:54:39 UTC, Artur Skawina wrote:

Well, aliases can be used to get a similar effect.

   template tie(A...) { alias tie = A; }
   tie!(a, b) = tuple(1, 2);

artur


Which is actually already in Phobos:

TypeTuple!(a, b) = tuple(1, 2);


Re: ref tuples

2013-07-03 Thread Artur Skawina
On 07/03/13 02:22, Brad Anderson wrote:
> C++11's std::tuple includes a function std::tie that takes references to the 
> arguments and returns a tuple that maintains the references to the arguments.
> 
> Along with the usual cases where you'd want reference semantics it also 
> enables this interesting construct for unpacking tuples.
> 
> int a, b;
> tie(a, b) = make_tuple(1, 2);
> 
> assert(a == 1 && b == 2);
> 
> Is there any way to do something similar with std.typecons.Tuple?

Well, aliases can be used to get a similar effect.

   template tie(A...) { alias tie = A; }
   tie!(a, b) = tuple(1, 2);

artur


Re: Stop to! rounding?

2013-07-03 Thread bearophile

Josh:


Is there any way I would be able to hold that number then?


One way to do that is with a simple rationals library, 
represented as pairs of bigints.


Bye,
bearophile


Re: Stop to! rounding?

2013-07-03 Thread monarch_dodra

On Wednesday, 3 July 2013 at 08:27:52 UTC, Maxim Fomin wrote:

On Wednesday, 3 July 2013 at 08:23:40 UTC, monarch_dodra wrote:


My brother in law writes financial apps, and in that field, 
using floating points type is *legally* forbidden.




Really? What kind of apps?


I meant "apps" as in short for program, not phone "apps". It's 
complicated to explain, but in a word, He basically programs 
distributed servers to do stock trading. I'm not allowed to talk 
about it much, especially on some random board :(


About the whole "legally forbidden" thing: I'm not sure it is the 
entire "financial" field, but definitly in the accounting field.


Re: How to get warnings about unused imports ?

2013-07-03 Thread Gabi

On Wednesday, 3 July 2013 at 06:12:46 UTC, Namespace wrote:

On Tuesday, 2 July 2013 at 21:49:37 UTC, Gabi wrote:

Hi,

How to find unused imports ?

It seems the compiler doesn't do it, but is there any other 
tool for that?
This seems like small issue, but those unused imports pile up 
pretty quickly


Regards,
Gabi


I'm working on something like that for a few days. It is still 
not completely finished, but it works so far. But currently 
only for named imports. Everything else was far too much work 
for me.


https://github.com/Dgame/SimpleDAT


This is great. Maybe point me to the right direction how to 
implement finding unnamed imports too ? It could be great 
exercise for me to learn D :)


Re: Stop to! rounding?

2013-07-03 Thread Maxim Fomin

On Wednesday, 3 July 2013 at 08:23:40 UTC, monarch_dodra wrote:


My brother in law writes financial apps, and in that field, 
using floating points type is *legally* forbidden.




Really? What kind of apps?


Re: Stop to! rounding?

2013-07-03 Thread monarch_dodra

On Wednesday, 3 July 2013 at 06:18:28 UTC, Jonathan M Davis wrote:

On Wednesday, July 03, 2013 08:11:50 Josh wrote:
Jonathan, do you know of any fixed point D library? If not, 
would

it be worth me making one for phobos?


I am unaware of one, and I don't really know why anyone would 
really want fixed
point rather than floating point, so I don't know what use 
cases it would be
valid for or how much interest there would really be in adding 
something like

that to Phobos.

- Jonathan M Davis


Said the man who then went on to state "I really don't like 
dealing

with floating point numbers." ;)

That said, fixed point types, and/or "decimal floating point 
types" are both very important features to specific domains: Just 
the same way certain fields (gaming) have weird requirements, so 
do certain other fields.


My brother in law writes financial apps, and in that field, using 
floating points type is *legally* forbidden.


My wife writes the embeded code for a machine here company 
manufactures. Ditto: no floating point types allowed. She's told 
me that their code is *riddled* with things like "mv = v * 1000": 
because they don't have a way to handle fractinal numbers.


Long story short, I think both would be a great addition to 
phobos/D. I'd personally really want to play with decimal floats: 
They are slower, less precise and have less range, but are more 
"exact" in base 10 than traditional floats. And when printed in 
Base 10, are *always* exact.