Re: Compile time foreach with switch

2017-04-21 Thread Johan Fjeldtvedt via Digitalmars-d-learn

On Friday, 21 April 2017 at 19:17:32 UTC, Adam D. Ruppe wrote:
On Friday, 21 April 2017 at 19:09:25 UTC, Johan Fjeldtvedt 
wrote:

void foo(string s) {
enum es = tuple("a", "b", "c");
switch (s) {
  foreach (e; es) {
case e:
writeln("matched ", e);
break;
  }


Let me remove some surrounding stuff and ask you what happens:

   foreach (e; es) {
 if(s == c) {
 writeln("matched ", e);
 break;
 }
   }


What does that break do?


Then realize that it does exactly the same thing when it is 
inside a switch... it always breaks the innermost thing, which 
happens to be this loop. (note that cases are not `breaked`, 
the switch is.)


*facepalm* Of course! Thanks.


Compile time foreach with switch

2017-04-21 Thread Johan Fjeldtvedt via Digitalmars-d-learn
I was a bit surprised to find out 
(https://forum.dlang.org/post/csiwyetjkttlxxnwn...@forum.dlang.org) that compile time foreach-loops can be used inside switch-statements. I tried the following:


import std.stdio;
import std.typecons;

void foo(string s) {
enum es = tuple("a", "b", "c");
switch (s) {
  foreach (e; es) {
case e:
writeln("matched ", e);
break;
  }
default:
writeln("no match");
break;
}
}

void main() {
foo("a");
}

However, this prints both "matched a" and "no match". It seems 
like either the break or the default: label is ignored. In this 
example I could have used return of course, but is this behavior 
correct?


Re: Forwarding calls to objects of another type

2017-04-19 Thread Johan Fjeldtvedt via Digitalmars-d-learn

On Tuesday, 11 April 2017 at 02:01:19 UTC, Nicholas Wilson wrote:

On Monday, 10 April 2017 at 21:27:34 UTC, Basile B. wrote:
2) This is about the reduce templates. As I've commented, I 
can't use a template lambda with reduce, but I can use a 
lambda taking ints as arguments. Why is this? The error 
message I get when using the template lambda is:


"template instance reduce!((a, b) => a + b) cannot use local 
'__lambda1' as parameter to non-global template reduce(alias 
fun)()"


No idea for this.


The use of the global identity template will fix this:

see 
https://blog.thecybershadow.net/2015/04/28/the-amazing-template-that-does-nothing/


Thanks, that did work. I think I understand the point about UFCS 
lookup rules, but it still seems strange (it was at least 
surprising) that I couldn't use the template (a, b) => a + b in 
place of (int a, int b) => a + b.


Re: Forwarding calls to objects of another type

2017-04-19 Thread Johan Fjeldtvedt via Digitalmars-d-learn

On Monday, 10 April 2017 at 21:27:34 UTC, Basile B. wrote:
On Monday, 10 April 2017 at 21:04:10 UTC, Johan Fjeldtvedt 
wrote:

[...]


One way:

[...]


Thanks for the reply. The traits way of doing it seems to be what 
I want. :)



[...]


[...]




Forwarding calls to objects of another type

2017-04-10 Thread Johan Fjeldtvedt via Digitalmars-d-learn
I have a couple of questions related to the following code: 
https://gist.github.com/Jaffe-/b027287a884fc2e173a65601ec242676


1) This is a very simplified example, but what I'm trying to do 
here is to call `foo` on each object in `Container.ss` contains 
when `foo` is called, and likewise for `bar`. To do this without 
having to repeat the foreach loop in every member function, I 
made the `call_for_each` template. However, I found no other way 
to pull this off (without using string mixins) than to also add 
the `call` template in the S struct. At first I thought it would 
be possible to write `s.func(args)` in `call_for_each`, but that 
will try to look up func as a member in the struct.


Is there a more common / idiomatic way of doing this? In C++ this 
would be solved by using a member function pointer as a template 
argument, but as far as I understand D uses delegates (which are 
already bound to an instance) instead?


2) This is about the reduce templates. As I've commented, I can't 
use a template lambda with reduce, but I can use a lambda taking 
ints as arguments. Why is this? The error message I get when 
using the template lambda is:


"template instance reduce!((a, b) => a + b) cannot use local 
'__lambda1' as parameter to non-global template reduce(alias 
fun)()"