Thanks to both of you (and if others still wish to share experiences, please, don't hestiate). Your "guiding principle" does indeed simplify my thought process here. Indeed, the reason I asked in the first place is that I have some code where conciseness demands the use of if statements; to parametrize would require an undue amount of duplicated code. It's good to know I can trust that the unused branches will be eliminated at compile time.
On Tuesday, August 26, 2014 3:41:13 PM UTC-5, Iain Dunning wrote: > > My guiding principle of Julia type stuff is the following: a version of a > function is compiled for every type combination it is called with. Knowing > that, all types become "crystallized" and propagate through, eliminating > ifs and simplifying things. > However, its been my experience with compilers in many langauges that > there are often limits to how much is decided at compile time, normally due > to a heuristic tradeoff between compilation time and run time of the > function. > > For that reason, I try and use dispatch as I think it is cleaner than the > ifs, even if they go away in the end. But I think people are needlessly > afraid of using eltype because it seems like it'd be a run time check > (Python people would definitely assume this). > > Best example I know is: > https://github.com/IainNZ/GraphLayout.jl/blob/master/src/spring.jl#L42 > > if adj_matrix[i,j] != zero(eltype(adj_matrix)) || adj_matrix[j,i] != > zero(eltype(adj_matrix)) > > which compiles down to > > if adj_matrix[i,j] != 0.0 || adj_matrix[j,i] != 0.0 > > or > > if adj_matrix[i,j] != false || adj_matrix[j,i] != false > > etc. I don't think people are used to this. > > Anyway, thats my 1c. > > On Tuesday, August 26, 2014 4:32:15 PM UTC-4, Stefan Karpinski wrote: > > It's kind of a toss up. These days, as you've noticed, they tend to > produce identical results, which is nice. Once upon a time, the type > parameter versions tended to produce better code, but that's not the case > mostly anymore. So whichever is easier and clearer, I guess? If you need > the type parameter for dispatch, then I guess I'd favor using it. > >
