# How to define a function negation?

**URL:** <https://discourse.julialang.org/t/how-to-define-a-function-negation/31483>\
**Category:** General Usage\
**Tags:** question\
**Created:** [November 25, 2019, 10:58am UTC](https://discourse.julialang.org/t/how-to-define-a-function-negation/31483 "2019-11-25T10:58:20Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![singularitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/singularitti/32/17678_2.png) [@singularitti](https://discourse.julialang.org/u/singularitti)\
**Post date:** [November 25, 2019, 10:58am UTC](https://discourse.julialang.org/t/how-to-define-a-function-negation/31483/1 "2019-11-25T10:58:20Z")

</div>

For example,

```julia
julia> f(::Int) = [1, 1]
f (generic function with 1 method)

```

Now I want to define the negation function of `f`:

```julia
julia> (!f)(::Int) = 2
ERROR: syntax: invalid function name "!(f)"
Stacktrace:
 [1] top-level scope at REPL[6]:1

```

However, this is not a valid syntax.

---

<div class="post-metadata">

**Author:** ![pfitzseb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pfitzseb/32/45566_2.png) [@pfitzseb](https://discourse.julialang.org/u/pfitzseb)\
**Post date:** [November 25, 2019, 11:05am UTC](https://discourse.julialang.org/t/how-to-define-a-function-negation/31483/2 "2019-11-25T11:05:20Z")

</div>

```julia
julia> f(x) = [x,x]
f (generic function with 1 method)

julia> Base.:!(::typeof(f)) = x -> f(x).^2

julia> f(3)
2-element Array{Int64,1}:
 3
 3

julia> (!f)(3)
2-element Array{Int64,1}:
 9
 9

```

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [November 25, 2019, 11:29am UTC](https://discourse.julialang.org/t/how-to-define-a-function-negation/31483/3 "2019-11-25T11:29:39Z")

</div>

By golly, this sounds like a bad idea, but out of sick curiosity, I would like to find out what you’re using this for 🙂.

---

<div class="post-metadata">

**Author:** ![singularitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/singularitti/32/17678_2.png) [@singularitti](https://discourse.julialang.org/u/singularitti)\
**Post date:** [November 25, 2019, 11:54am UTC](https://discourse.julialang.org/t/how-to-define-a-function-negation/31483/4 "2019-11-25T11:54:03Z")

</div>

It’s the following case:

```julia
struct MyType1
    a::MyType2
    b::MyType2
    c::MyType2
    d::MyType2
    e::MyType3
    f::MyType3
    g::MyType3
end

available_mytype2(y::MyType1) = (:a, :b, :c, :d)
compulsory_mytype2(::MyType1) = (:a, :b)
optional_mytype2(::MyType1) = (:c, :d)

```

I don’t want to apply these methods onto `MyType3`, and getting the optional `MyType2` can’t be just `setdiff(fieldnames(MyType1), compulsory_mytype2(x))`. But defining an `optional_mytype2` seems to be overkill. So…

In summary, I don’t want the `!compulsory_mytype2` to be the negation of `(:a, :b)` (It can’t be!), but a new behavior: the semantic negation/complement of `compulsory_mytype2`.

---

<div class="post-metadata">

**Author:** ![singularitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/singularitti/32/17678_2.png) [@singularitti](https://discourse.julialang.org/u/singularitti)\
**Post date:** [November 25, 2019, 11:55am UTC](https://discourse.julialang.org/t/how-to-define-a-function-negation/31483/5 "2019-11-25T11:55:41Z")

</div>

Just for fun, I find those also work:

```julia
julia> Base.:!(::typeof(!)) = x -> 2x

julia> (!!)(2)
4

julia> Base.:!(::typeof(!=)) = x -> 3x

julia> (!!=)(3)
9

```

Though it is too easy to be confused with `!(!x)`. I can define new functions applied to arguments in a more general case.
