Re: [julia-users] Colon as an argument
On Thu, Jun 4, 2015 at 8:50 AM, Christoph Ortner christophortn...@gmail.com wrote: When implementing a function (overloading getindex to be precise) to allow : as an argument I expected that I would need to do the following: funA(_::Colon) = println(funA) but the following works as well funB(:) = println(funB) You are just defining a method with `:` being it's (weird) argument name. `:` is a global constant but it doesn't have any special meaning for the parser. You can override it with local variables just like any other globals. Here's how you can figure out the signature of that method. ```julia julia f(:) = 1 f (generic function with 1 method) julia methods(f, Tuple{Any}) 1-element Array{Any,1}: f(:) at none:1 julia methods(f, Tuple{Any})[1].sig Tuple{Any} ``` Why? When I type ?: in the REPL I find that : is an instance of Colon. This is unexpected for me, it does not seem to be the same as Value Types? Can somebody clarify for me what is happening? Many thanks, Christoph
Re: [julia-users] Colon as an argument
ah - extremely embarrassing mistake. Thank you. So was funA the correct way to implement this? Christoph
Re: [julia-users] Colon as an argument
On Thu, Jun 4, 2015 at 9:10 AM, Christoph Ortner christophortn...@gmail.com wrote: ah - extremely embarrassing mistake. Thank you. So was funA the correct way to implement this? IIRC, currently `a[:]` gets lowered to `a[1:endof(a)]` (see `expand(:(a[:]))`) so this won't work for get/set index. (I remember there's a PR about this lowering, maybe it will change soon?) But IMHO it is the right way if you want to dispatch on `:` in a function in general. Christoph
Re: [julia-users] Colon as an argument
On Thu, Jun 4, 2015 at 9:13 AM, Yichao Yu yyc1...@gmail.com wrote: On Thu, Jun 4, 2015 at 9:10 AM, Christoph Ortner christophortn...@gmail.com wrote: ah - extremely embarrassing mistake. Thank you. So was funA the correct way to implement this? IIRC, currently `a[:]` gets lowered to `a[1:endof(a)]` (see `expand(:(a[:]))`) so this won't work for get/set index. (I remember there's a PR about this lowering, maybe it will change soon?) I didn't follow it but this[1] is what I'm thinking about. [1] https://github.com/JuliaLang/julia/pull/10331#issuecomment-108682886 But IMHO it is the right way if you want to dispatch on `:` in a function in general. Oh. except that you don't need that dummy `_` if you don't need the argument. Just `f(::Colon)` should be enough Christoph
Re: [julia-users] Colon as an argument
great - thank you! On Thursday, 4 June 2015 14:16:42 UTC+1, Yichao Yu wrote: On Thu, Jun 4, 2015 at 9:13 AM, Yichao Yu yyc...@gmail.com javascript: wrote: On Thu, Jun 4, 2015 at 9:10 AM, Christoph Ortner christop...@gmail.com javascript: wrote: ah - extremely embarrassing mistake. Thank you. So was funA the correct way to implement this? IIRC, currently `a[:]` gets lowered to `a[1:endof(a)]` (see `expand(:(a[:]))`) so this won't work for get/set index. (I remember there's a PR about this lowering, maybe it will change soon?) I didn't follow it but this[1] is what I'm thinking about. [1] https://github.com/JuliaLang/julia/pull/10331#issuecomment-108682886 But IMHO it is the right way if you want to dispatch on `:` in a function in general. Oh. except that you don't need that dummy `_` if you don't need the argument. Just `f(::Colon)` should be enough Christoph