I was trying to name a function as %%
but I did not succeed. I got:
ERROR: LoadError: syntax: "%" is not a unary operator
Can we name a function in julia as %%
?
I was trying to name a function as %%
but I did not succeed. I got:
ERROR: LoadError: syntax: "%" is not a unary operator
Can we name a function in julia as %%
?
julia> var"%%"() = 1
%% (generic function with 1 method)
julia> var"%%"()
1
Why do you want a function named like this?
If you want to define a new unary operator, you have to use one of the function names reserved for this purpose (e.g. ++
).
because I have a function that is very close to mod (%), so I tried to rename it %%. After all, I used a text name. But I was wondering the reason why I could not use %% as a name.
No particular reason. Support would need to be added to the parser for %%
as an infix operator. There might, however, be objections or complications. Worth filing an issue though if you’re interested in this being available in a future version.
Given that %%
has no standardized meaning anyway (unlike eg +
, *
), and we have a zillion free Unicode operators for custom purposes, I am not sure I understand the motivation for complicating things just for the sake of %%
.
Yes, I know R has it (for mod), and a whole zoo of other operators sandwiched between %%
s. But they have no Unicode. Also, once operators end up taking up several characters, and aptly named function may just be less cryptic.
You can call it %′
(tab-complete %\prime
) or %̂
(%\hat
) or %̃
(%\tilde
), for example, or use a subscript or superscript after %
.
Also, it might be good to reserve %%
for mod
since %
means rem
.
I am not sure mod
deserves an infix operator. There are around 50 uses of it in Base
, and 40 more in the standard libraries (as opposed to %
as rem
, which has a few hundred).
If anything it’s rem
that doesn’t deserve an infix operator. mod
is by far the more useful of them whereas rem
is a latent source of bugs whenever someone is careless with the sign of the first argument. (Yes, I know that hardware prefers rem
, which is a historic mistake, and thus has its uses when performance is critical and you have full control of the signs.)
Out of which the large majority is x % Type
, where rem
and mod
are identical. Of the rest I didn’t at a quick glance see any case where either argument could be negative, so mod
would have worked as well as rem
.