I'm trying to use the \Uparrow
unicode, but I get an error saying unknown unicode character '⇑'
. The same happens for \Downarrow
, but it works on the Left and Right cases. Both the Up and Down arrows are listed in the Unicode input documentation for Julia... Any ideas on what the problem might be?
They're reserved characters
Meaning that when the parser was written, we weren't sure if a good use-case for them would later arise, and so we made it so they can't be used as identifiers until we know what the possible use-case is
So I can't use these unicodes?
I was looking for an easy to use alternative to represent left, right, up and down. But I'm already using the normal arrows
Is it possible to use named functions as binary operators?
Like in Haskell :
x `myf` y
You could open a PR adding them to https://github.com/JuliaLang/julia/blob/master/src/julia-parser.scm#L13
I doubt it would be very controversial
You mean adding \Uparrow and \Downarrow?
yes
What would be the controversy?
Isn't having more unicode available better?
What would be the downside?
You can also attach unicode prefixes or suffixes to the regular arrow operators to make new versions of them:
julia> ↑(a, b) = (a, b);
julia> ↑ᵃ(a, b) = (b, a);
julia> 1 ↑ 2
(1, 2)
julia> 1 ↑ᵃ 2
(2, 1)
This is a great idea! Had not thought about it.
I'll use this. Thanks, @Mason Protter
Davi Sales Barreira said:
What would be the downside?
the downside I mentioned before. Someone might want that operator to live at a different precedence level or be for something else. But I doubt it'd be a problem in this case, since the pre-arrow category is pretty clear.
Ohh, now I undertand.
The problem is that the precedence is "fixed".
But then I'm confused again. I thought \Uparrow was unusable.
Davi Sales Barreira said:
Like in Haskell :
x `myf` y
Yeah, there is a hack to do this:
struct InfixFunction <: Function
name::Symbol
operator::Function
end
InfixFunction(operator::Function) = InfixFunction(gensym(), operator)
(infix::InfixFunction)(arg₁, arg₂) = infix.operator(arg₁, arg₂)
function Base.show(io::IO, infix::InfixFunction)
n_methods = length(methods(infix.operator))
_methods = n_methods == 1 ? "method" : "methods"
name = infix.name
println(io, "$name (generic infix function with $n_methods $_methods)")
end
Base.display(infix::InfixFunction) = show(infix)
function Base.:|(arg₁, infix::InfixFunction)
return InfixFunction(arg₂ -> infix.operator(arg₁, arg₂))
end
Base.:|(infix::InfixFunction, arg₂) = infix.operator(arg₂)
macro infix(operator::Symbol)
return quote
$operator::Function
function Base.:|(arg₁, infix::typeof($operator))
return $InfixFunction(arg₂ -> infix(arg₁, arg₂))
end
Base.:|(infix::typeof($operator), arg₂) = infix(arg₂)
@info "$($operator) has been infixified"
$operator
end |> esc
end
Mason Protter said:
Davi Sales Barreira said:
Like in Haskell :
x `myf` y
Yeah, there is a hack to do this:
struct InfixFunction <: Function name::Symbol operator::Function end InfixFunction(operator::Function) = InfixFunction(gensym(), operator) (infix::InfixFunction)(arg₁, arg₂) = infix.operator(arg₁, arg₂) function Base.show(io::IO, infix::InfixFunction) n_methods = length(methods(infix.operator)) _methods = n_methods == 1 ? "method" : "methods" name = infix.name println(io, "$name (generic infix function with $n_methods $_methods)") end Base.display(infix::InfixFunction) = show(infix) function Base.:|(arg₁, infix::InfixFunction) return InfixFunction(arg₂ -> infix.operator(arg₁, arg₂)) end Base.:|(infix::InfixFunction, arg₂) = infix.operator(arg₂) macro infix(operator::Symbol) return quote $operator::Function function Base.:|(arg₁, infix::typeof($operator)) return $InfixFunction(arg₂ -> infix(arg₁, arg₂)) end Base.:|(infix::typeof($operator), arg₂) = infix(arg₂) @info "$($operator) has been infixified" $operator end |> esc end
Any downside on using this?
It looks like a very nice feature to have.
julia> myf(x, y) = x => y
myf (generic function with 1 method)
julia> @infix myf
[ Info: myf has been infixified
myf (generic function with 1 method)
julia> 1 |myf| 2
1 => 2
Davi Sales Barreira said:
But then I'm confused again. I thought \Uparrow was unusable.
I'm not sure what you mean. It's currently not useable because it's not registered in the parser. If you add it to the parser as a registered infix operator, it will become usable.
hmm. I can add it to the parsers in my code?
No
oh, ok
You would need to modify julia itself
yeah, I'm not doing that.
it's a very easy PR, you literally would just add it to that list
Yep.
I guess I can add the PR.
Here's an example of what a PR for that would look like: https://github.com/JuliaLang/julia/pull/49623/files
And I think you wouldn't even need to do the latex completion stuff because it's already registered for latex completions
Oh wait, I just realized we use JuliaSyntax.jl now, not the scheme parser :face_palm:
So you would instead change this: https://github.com/JuliaLang/JuliaSyntax.jl/blob/main/src/kinds.jl#L147
Thanks!
Davi Sales Barreira has marked this topic as resolved.
Last updated: Nov 06 2024 at 04:40 UTC