I may miss the obvious, but why is there no method assigned to the negation operator?
I want to do the following:
function check(number)
if ! number >= 0
error("number must be non-negative")
end
end
not sure what exactly you're asking. There certainly are methods assigned to it:
julia> methods(!)
# 4 methods for generic function "!" from Base:
[1] !(f::ComposedFunction{typeof(!)})
@ operators.jl:1089
[2] !(f::Function)
@ operators.jl:1088
[3] !(x::Bool)
@ bool.jl:35
[4] !(::Missing)
@ missing.jl:101
Your example just needs parentheses
Well, not to an integer. I already tried to put parens around it, but that didnt help. I tried now once again, and obviously, it works :P
I may have had the old state saved in the REPL, now it works. :D
ShalokShalom has marked this topic as resolved.
I do sometimes feel that python's not x
is more legible sometimes than our !x
. I guess I could always just do
macro not(x)
Expr(:call, !, esc(x))
end
I tend to think unary operators are just not worth it
I'm very very thankful we have '
for adjoint.
I kinda wish we had more postfix unary operators
Yeah I just meant the prefix ones. I even have some postfixes in mind.
Mason Protter said:
I do sometimes feel that python's
not x
is more legible sometimes than our!x
. I guess I could always just do
Someone at jolin.io seems to have had similar thoughts: https://github.com/jolin-io/NotMacro.jl/blob/main/src/NotMacro.jl
Yes, this is why I opened https://github.com/JuliaLang/julia/issues/51485
You might help with emojicons and arguments :D
Mason Protter said:
I do sometimes feel that python's
not x
is more legible sometimes than our!x
.
Names are always understood by anyone. Show me anyone, who doesn't know what not
means. On the other hand, no one, who hasn't programmed yet already known that ! is supposed to has the meaning of no
So, particularly to introduce new people to programming, this seems to show, that Julia is currently not trying its best to onboard them.
Jar's point isn't really about not
vs !
. It's more just that not x
isn't much of a win vs not(x)
.
Whining that any syntax you don't like is julia "not trying its best to onboard them" is not really productive or helpful
Sundar R said:
Mason Protter said:
I do sometimes feel that python's
not x
is more legible sometimes than our!x
. I guess I could always just doSomeone at jolin.io seems to have had similar thoughts: https://github.com/jolin-io/NotMacro.jl/blob/main/src/NotMacro.jl
# macroexpand solves `@not @not true && false` case
I'm not even sure what I'd want that to mean. That's a case where !
seems much clearer
yeah, that seems totally unnecessary and not such a good idea
They likely thought there was a performance advantage to it because the code_llvm
for @not @not x && y
looks shorter than the code_llvm
for !(!(x && y))
, but at least on x86, they still compile to identical code_native
.
julia> using NotMacro
julia> code_native(Tuple{Bool, Bool}) do x, y
@not @not x && y
end
.text
.file "#1"
.globl "julia_#1_750" # -- Begin function julia_#1_750
.p2align 4, 0x90
.type "julia_#1_750",@function
"julia_#1_750": # @"julia_#1_750"
; ┌ @ REPL[13]:5 within `#1`
.cfi_startproc
# %bb.0: # %top
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset %rbp, -16
movq %rsp, %rbp
.cfi_def_cfa_register %rbp
movl %edi, %eax
; │┌ @ bool.jl:35 within `!`
andb $1, %al
negb %al
andb %sil, %al
; │└
# kill: def $al killed $al killed $eax
popq %rbp
.cfi_def_cfa %rsp, 8
retq
.Lfunc_end0:
.size "julia_#1_750", .Lfunc_end0-"julia_#1_750"
.cfi_endproc
; └
# -- End function
.section ".note.GNU-stack","",@progbits
julia> code_native(Tuple{Bool, Bool}) do x, y
!(!(x && y))
end
.text
.file "#1"
.globl "julia_#1_752" # -- Begin function julia_#1_752
.p2align 4, 0x90
.type "julia_#1_752",@function
"julia_#1_752": # @"julia_#1_752"
; ┌ @ REPL[14]:4 within `#1`
.cfi_startproc
# %bb.0: # %top
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset %rbp, -16
movq %rsp, %rbp
.cfi_def_cfa_register %rbp
movl %edi, %eax
; │┌ @ bool.jl:35 within `!`
andl %esi, %eax
; │└
andb $1, %al
# kill: def $al killed $al killed $eax
popq %rbp
.cfi_def_cfa %rsp, 8
retq
.Lfunc_end0:
.size "julia_#1_752", .Lfunc_end0-"julia_#1_752"
.cfi_endproc
; └
# -- End function
.section ".note.GNU-stack","",@progbits
nothing
Main.B
Mason Protter said:
Whining that any syntax you don't like is julia "not trying its best to onboard them" is not really productive or helpful
I did indeed mean not
vs !
and I gave reasoning, why I think one is more approachable.
I indeed quoted the wrong person, and meant my statement towards you (and simply agreed with you) :big_smile:
Last updated: Nov 22 2024 at 04:41 UTC