Re: The One-Letter Nested Function - a sample article for some kind of

2012-03-10 Thread bearophile
F i L: I sorta figured D would implicitly attribute static to nested functions if the function didn't use any variables outside it's scope. Is that not so? I don't think DMD does that. Before assuming DMD performs one optimization, go to read some of the asm it produces. Why are you

Re: The One-Letter Nested Function - a sample article for some kind of

2012-03-10 Thread F i L
On Saturday, 10 March 2012 at 12:35:14 UTC, bearophile wrote: F i L: I sorta figured D would implicitly attribute static to nested functions if the function didn't use any variables outside it's scope. Is that not so? I don't think DMD does that. Before assuming DMD performs one

Re: The One-Letter Nested Function - a sample article for some kind of

2012-03-10 Thread bearophile
F i L: Oh you crazy compiler developers. Thinking everyone can read ASM ;-) I'd love to be able to develop compilers, but I am not that good yet :-) Writing working asm is much simpler than writing efficient asm code (more efficient than compiler generated one, even for SIMD code where

Re: The One-Letter Nested Function - a sample article for some kind of

2012-03-10 Thread F i L
bearophile wrote: Writing working asm is much simpler than... Ya I've actually written ASM GPU shaders in the past. Not the same instruction set as x86 or anything, but I know the basic concept. Though, there *could* be area for automatic optimization here, right? Currently DMD devs

Re: The One-Letter Nested Function - a sample article for some kind of

2012-03-09 Thread Jos van Uden
On 13-2-2012 15:14, bearophile wrote: Zach the Mystic: void setRandomColorPair( ref ColorPair cp ) { import std.random; ubyte u(int a, int b) { return cast(ubyte) uniform(a,b); } Where possible it's good to add static to nested functions: Why?

Re: The One-Letter Nested Function - a sample article for some kind of

2012-03-09 Thread Timon Gehr
On 03/09/2012 02:29 PM, Jos van Uden wrote: On 13-2-2012 15:14, bearophile wrote: Zach the Mystic: void setRandomColorPair( ref ColorPair cp ) { import std.random; ubyte u(int a, int b) { return cast(ubyte) uniform(a,b); } Where possible it's good to add static to nested functions: Why?

Re: The One-Letter Nested Function - a sample article for some kind of

2012-03-09 Thread bearophile
Jos van Uden: On 13-2-2012 15:14, bearophile wrote: Where possible it's good to add static to nested functions: Why? For optimization, to be sure there's no closure allocation or a second pointer. But also for code correctness, because static functions can't use automatic variables

Re: The One-Letter Nested Function - a sample article for some kind of

2012-03-09 Thread F i L
On Monday, 13 February 2012 at 14:14:38 UTC, bearophile wrote: Zach the Mystic: void setRandomColorPair( ref ColorPair cp ) { import std.random; ubyte u(int a, int b) { return cast(ubyte) uniform(a,b); } Where possible it's good to add static to nested functions: static ubyte u(in

The One-Letter Nested Function - a sample article for some kind of D gems website

2012-02-13 Thread Zach the Mystic
I wrote this article because I felt like helping other people coming to D, but I'm not sure where the appropriate place to make such a contribution is. Maybe a Learning Articles or an Idioms section. The One-Letter Nested Function As a programmer new to D I wanted to share an idiom I've been

Re: The One-Letter Nested Function - a sample article for some kind of D gems website

2012-02-13 Thread Andrej Mitrovic
With 2.058 the single-letter function can become: auto u = (int a, int b) = cast(ubyte)uniform(a, b); It's not much of savings in typing. The only problem is I can't seem to make it static: static u = (int a, int b) = cast(ubyte)uniform(a, b); Error: non-constant nested delegate literal

Re: The One-Letter Nested Function - a sample article for some kind of D gems website

2012-02-13 Thread David Nadlinger
On 2/13/12 2:43 PM, Andrej Mitrovic wrote: auto u = (a, b) = cast(ubyte)uniform(a, b); Which would make 'u' a template. I'm not sure what the exact syntax was that was requested though. This could never work without major changes to the language, because 'u' cannot be assigned a type.

Re: The One-Letter Nested Function - a sample article for some kind of

2012-02-13 Thread bearophile
Zach the Mystic: void setRandomColorPair( ref ColorPair cp ) { import std.random; ubyte u(int a, int b) { return cast(ubyte) uniform(a,b); } Where possible it's good to add static to nested functions: static ubyte u(in int a, in int b) pure nothrow { return cast(ubyte)

Re: The One-Letter Nested Function - a sample article for some kind of D gems website

2012-02-13 Thread Andrej Mitrovic
On 2/13/12, David Nadlinger s...@klickverbot.at wrote: This could never work without major changes to the language, because 'u' cannot be assigned a type. Yeah, the syntax is wrong. I found bear's post and the syntax: alias (x = x ^^ 2) sqrTemplate; So it would be: alias ((a, b) =

Re: The One-Letter Nested Function - a sample article for some kind of D gems website

2012-02-13 Thread Nick Treleaven
On 13/02/2012 14:21, Andrej Mitrovic wrote: On 2/13/12, David Nadlingers...@klickverbot.at wrote: This could never work without major changes to the language, because 'u' cannot be assigned a type. Yeah, the syntax is wrong. I found bear's post and the syntax: alias (x = x ^^ 2)

Re: The One-Letter Nested Function - a sample article for some kind of

2012-02-13 Thread Zach the Mystic
On 2/13/12 9:14 AM, bearophile wrote: Where possible it's good to add static to nested functions: static ubyte u(in int a, in int b) pure nothrow { return cast(ubyte) uniform(a,b); } You're right. The only advantage to the way I wrote it is, possibly, it's easier for new people (like

Re: The One-Letter Nested Function - a sample article for some kind

2012-02-13 Thread bearophile
Zach the Mystic: But I'm pretty sure uniform is NOT a pure function. In fact, generating random numbers is about as far opposite a pure function as you can get, right? :-) Right, and sorry, I didn't see the function contents. If I don't run the D code you have to assume it's wrong code.

Re: The One-Letter Nested Function - a sample article for some kind of D gems website

2012-02-13 Thread Andrei Alexandrescu
On 2/13/12 7:46 AM, David Nadlinger wrote: On 2/13/12 2:43 PM, Andrej Mitrovic wrote: auto u = (a, b) = cast(ubyte)uniform(a, b); Which would make 'u' a template. I'm not sure what the exact syntax was that was requested though. This could never work without major changes to the language,

Re: The One-Letter Nested Function - a sample article for some kind

2012-02-13 Thread Zach the Mystic
On 2/13/12 11:21 AM, bearophile wrote: Zach the Mystic: Regarding pure random generators, I have asked it too: http://d.puremagic.com/issues/show_bug.cgi?id=5249 Aren't pure and random diametrically opposed in a fundamental way?

Re: The One-Letter Nested Function - a sample article for some kind

2012-02-13 Thread Iain Buclaw
On 13 February 2012 17:01, Zach the Mystic reachzachatgooglesmailserv...@dot.com wrote: On 2/13/12 11:21 AM, bearophile wrote: Zach the Mystic: Regarding pure random generators, I have asked it too: http://d.puremagic.com/issues/show_bug.cgi?id=5249 Aren't pure and random diametrically

Re: The One-Letter Nested Function - a sample article for some kind

2012-02-13 Thread Timon Gehr
On 02/13/2012 06:01 PM, Zach the Mystic wrote: On 2/13/12 11:21 AM, bearophile wrote: Zach the Mystic: Regarding pure random generators, I have asked it too: http://d.puremagic.com/issues/show_bug.cgi?id=5249 Aren't pure and random diametrically opposed in a fundamental way? They are. It is