On Monday, May 9, 2016 at 6:10:40 AM UTC, Tamas Papp wrote:
>
> Was there more recent discussion about switch? I think I missed it, last 
> thing I am aware of is #5410. And of course see Switch.jl.
>

I took a look, and it implements C-style switch with its pros/features 
(mainly if you are translating C-code line-by-line) and cons. While I find 
it great that implementing switch (and match) with macros in Julia is 
possible, I also see at:

https://github.com/JuliaLang/julia/issues/5410

that Go's switch fixed C's "software engineering"-mistakes (still OO 
polymorphism and Julia's multiple dispatch is also the alternative), with 
"fallback"-keywords still allowing "Duff's device" (fast code).

Something like:

https://github.com/kmsquire/Match.jl

should be emphasized over switch.

-- 
Palli.


> In any case, I find the proposed syntax a bit obscure and 
> complicated. I would prefer using the result of 
>
> findfirst(x->input % x == 0,[2,3,5,7]) 
>
> or if that does not help, then explicit currying, 
>
> input = 119 
> let d(x) = input % x == 0 
>     if d(2) 
>         # code 
>     elseif d(3) 
>         # code 
>     elseif d(5) 
>         # code 
>     elseif d(7) 
>         # code 
>     end 
> end 
>
> Best, 
>
> Tamas 
>
>
>
> On Mon, May 09 2016, Ford Ox wrote: 
>
> > I have a little suggestion: If julia is going to have switch, could we 
> make it a bit better? 
> > 
> > Basically the switch would take two parameters : function and variable. 
> On each case it would would 
> > call the function with those two params, and if the functions would 
> return true, it would evaluate the case 
> > block. 
> > 
> > Note: the function has to return boolean. 
> > 
> > Example 
> > 
> > function divides(a, b) 
> >   return a % b == 0 
> > end 
> > 
> > input = 119 
> > switch(divides, input) 
> >   case 2 # this can be translated as ~ if(divides(input, 2)) 
> >   case 3 # 3 divides input without remainder 
> >   case 5 # 5 divides input without remainder 
> >   case 7 # 7 divides input without remainder 
> > end 
> > 
> > Of course you could achieve the default switch behavior like this: 
> > switch(==, input) 
> > ... 
> > 
> > What do you think about it? 
>

Reply via email to