Re: fuction Return function

2014-07-26 Thread Meta via Digitalmars-d-learn

On Saturday, 26 July 2014 at 20:49:30 UTC, seany wrote:
Can a function return a function in D? Sorry if i missed the 
answer somewhere


Yup, you can return functions, delegates, or function pointers.

int function(int) returnsFunc1()
{
return function(int n) { return n; };
}

int function(int) returnsFunc2()
{
//Even works for nested functions.
//Make them static so they don't
//create a context pointer to
//returnsFunc2's frame
static int fun(int n)
{
return n;
}

return &fun;
}

int test(int n)
{
return n;
}

int function(int) returnsFunc3()
{
return &test;
}

int delegate(int) makeAdder(int numToAdd)
{
return delegate(int n) { return n + numToAdd; };
}

void main()
{
auto func1 = returnsFunc1();
assert(func1(1) == 1);

auto func2 = returnsFunc2();
assert(func2(2) == 2);

auto func3 = returnsFunc3();
assert(func3(3) == 3);

auto add3 = makeAdder(3);
assert(add3(1) == 4);
}


Re: fuction Return function

2014-07-26 Thread sigod via Digitalmars-d-learn

On Saturday, 26 July 2014 at 20:49:30 UTC, seany wrote:
Can a function return a function in D? Sorry if i missed the 
answer somewhere


Just alias your function signature:

```d
alias MyFunctionType = void function(int);
```

Example from my own code:
```d
alias DeserializeBody = TLObject function(InBuffer);

DeserializeBody[uint] registerAll(modules...)()
{
// ...
}
```



fuction Return function

2014-07-26 Thread seany via Digitalmars-d-learn
Can a function return a function in D? Sorry if i missed the 
answer somewhere


Re: myrange.at(i) for myrange.dropExactly(i).front

2014-07-26 Thread monarch_dodra via Digitalmars-d-learn

On Saturday, 26 July 2014 at 00:28:32 UTC, Ary Borenszweig wrote:
No, the OP said the meaning was `myrange.dropExactly(i).front`, 
which is not a random access.


Sometimes you *do* want the n-th element of a range even if the 
range is not a random access.


What he did also say is he wanted the equivalent of C++'s "at", 
which is the equivalent of "checked random-access" (or "checked 
dictionary access").


So the actual requirements aren't very clear. In terms of "C++ 
at" equivalent, I don't think we have anything equivalent to 
offer. That said, I've never seen anyone use "at" in C++ ever. 
I'd assume it's more of a java/C# thing to do checked accesses?




Re: Showing a user specified error message when no overloads match

2014-07-26 Thread H. S. Teoh via Digitalmars-d-learn
On Sat, Jul 26, 2014 at 05:14:44PM +, via Digitalmars-d-learn wrote:
> Hmmm... thinking about it, is this possible?
> 
> 1. Remove the constraints to match anything.
> 2. Inside the template, have some construct that enumerates all possible
> overloads and UFCS functions that are visible at the point of instantiation.
> 3. If this set contains only the current template, use a static assert
> to print the message.
> 4. Otherwise, make the template fail to compile somehow (for example,
> evaluate a semantically invalid expression), and hope that the
> compiler will then take the other overloads into consideration
> (SFINAE).
> 
> Could this work?

D does not have SFINAE.

This has been discussed before. I proposed the following solution:

- Sig constraints should match all types that the function *logically*
  accepts -- even if the current implementation does not support some of
  said types.

- In the function body, use a `static if` chain to implement
  specializations.

- In the final else clause, do a static assert(0) with a user-friendly
  error message.


T

-- 
In order to understand recursion you must first understand recursion.


Re: Showing a user specified error message when no overloads match

2014-07-26 Thread via Digitalmars-d-learn

Hmmm... thinking about it, is this possible?

1. Remove the constraints to match anything.
2. Inside the template, have some construct that enumerates all 
possible overloads and UFCS functions that are visible at the 
point of instantiation.
3. If this set contains only the current template, use a static 
assert to print the message.
4. Otherwise, make the template fail to compile somehow (for 
example, evaluate a semantically invalid expression), and hope 
that the compiler will then take the other overloads into 
consideration (SFINAE).


Could this work?


Re: dual with statement

2014-07-26 Thread Jay Norwood via Digitalmars-d-learn

On Friday, 25 July 2014 at 21:10:56 UTC, monarch_dodra wrote:

Functionally nothing more than an alias? EG:
{
alias baz = foo.bar;
...
}



Yes, it is all just alias.  So

with ( (d,e,a,b,c) as (ar.rm.a, ar.rm.b, ar.r.a, ar.r.b, ar.r.c)){
 d = a + c;
 e = (c==0)?0:(a+b)/c;
}

could be instead

{
  alias d = ar.rm.a;
  alias e = ar.rm.b;
  alias a = ar.r.a;
  alias b = ar.r.b;
  alias c = ar.r.c;

  d = a + c;
  e = (c==0)?0:(a+b)/c;

}

I guess this means I don't need WITH.



Re: Showing a user specified error message when no overloads match

2014-07-26 Thread Trass3r via Digitalmars-d-learn

Yeah that's the price we pay for the simplicity.
Also most constraints directly or indirectly consist of a complex 
boolean expressions and you don't get any hint which part failed 
and why.


ddoc and CODE_HIGHLIGHT macro

2014-07-26 Thread Nick Treleaven via Digitalmars-d-learn

Hi,
Ddoc doesn't seem to expand a macro near top of 
http://dlang.org/hash-map.html:


// The $(CODE_HIGHLIGHT KeyType) is string

Which is weird because it expands it for 'remove' on this line not far 
below it:


b.remove("hello");

Maybe a bug in dmd?

Also, I build some dlang.org docs individually and CODE_HIGHLIGHT is 
inserting a line break in the html. I think this is because the macro 
has an empty line after it:


CODE_HIGHLIGHT=$(B $(I $0))

MDASH= — 

I can't tell from git blame when it was introduced, the website html 
doesn't show the line break. Is there a workaround, or just remove the 
empty line?


using pipeprocess on non-applications

2014-07-26 Thread Sean Campbell via Digitalmars-d-learn
is there any way to detect if a file is a binary executable that 
is cross platform or a way to detect whether pipeprocss failed to 
execute a file if it wasn't executable.


Re: D JSON (WAT?!)

2014-07-26 Thread Kagamin via Digitalmars-d-learn
AFAIK, Variant is not transparent. You can't write 
parsed["field1"]["field2"], it should be 
parsed["field1"].get!(Variant[string])["field2"].