# Function definitions with literal parameters

**URL:** <https://discourse.julialang.org/t/function-definitions-with-literal-parameters/71538>\
**Category:** General Usage\
**Tags:** type, symbolics\
**Created:** [November 16, 2021, 4:41am UTC](https://discourse.julialang.org/t/function-definitions-with-literal-parameters/71538 "2021-11-16T04:41:23Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![banksiaboy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/banksiaboy/32/27032_2.png) [@banksiaboy](https://discourse.julialang.org/u/banksiaboy)\
**Post date:** [November 16, 2021, 4:41am UTC](https://discourse.julialang.org/t/function-definitions-with-literal-parameters/71538/1 "2021-11-16T04:41:23Z")

</div>

I’m tinkering with groups, bitstrings and XOR. I don’t care about efficiency - but it’s always nice…

I create the type then I want functions dispatching on value 🙂

```julia
@enum Bit zero one

⨁(zero, zero) = zero
⨁(zero, one) = one
⨁(one, zero) = one
⨁(one, one) = zero

```

In some universes this would work.

Is there a way of doing this in Julia?  
If not can I express this neatly without an awful nested if-statement?

I think I could make zero = false, and one = true, in the @enum and use the built-in xor. But I wanted to see if I can do it as proposed.

Actually - shouldn’t I be doing it like:

```julia
⨁(a::𝔹, b::𝔹) = a == b ? zero : one

```

Advice please…

Cheers…

---

<div class="post-metadata">

**Author:** ![banksiaboy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/banksiaboy/32/27032_2.png) [@banksiaboy](https://discourse.julialang.org/u/banksiaboy)\
**Post date:** [November 16, 2021, 5:21am UTC](https://discourse.julialang.org/t/function-definitions-with-literal-parameters/71538/2 "2021-11-16T05:21:55Z")

</div>

⨁ isn’t working as infix…

```julia
zero ⨁ one
ERROR: syntax: extra token "⨁" after end of expression
Stacktrace:
 [1] top-level scope
   @ none:1

```

---

<div class="post-metadata">

**Author:** ![tchr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tchr/32/15138_2.png) [@tchr](https://discourse.julialang.org/u/tchr)\
**Post date:** [November 16, 2021, 5:24am UTC](https://discourse.julialang.org/t/function-definitions-with-literal-parameters/71538/3 "2021-11-16T05:24:12Z")

</div>

You cannot dispatch on values, only on types.

The main way to do what you want would be to push your booleans to the type domain by wrapping them in `Val` (but then there’s no need for the Enum) or just constructing your own `True`/`False` singleton types. This is [what Static.jl does](https://github.com/SciML/Static.jl/blob/master/src/bool.jl).

(I’d just quickly caution that you should not follow this approach unless you know for sure that all input types (i.e. values here) are known statically - otherwise everything will be horribly type-unstable)

---

<div class="post-metadata">

**Author:** ![banksiaboy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/banksiaboy/32/27032_2.png) [@banksiaboy](https://discourse.julialang.org/u/banksiaboy)\
**Post date:** [November 16, 2021, 5:31am UTC](https://discourse.julialang.org/t/function-definitions-with-literal-parameters/71538/4 "2021-11-16T05:31:02Z")

</div>

@tchr thanks…

This is what happens when the unschooled watch people using proof-assistants :-). I was hoping I would break just enough to learn something.

I’ll go simple and use Booleans

---

<div class="post-metadata">

**Author:** ![jzr](https://avatars.discourse-cdn.com/v4/letter/j/eb9ed0/32.png) [@jzr](https://discourse.julialang.org/u/jzr)\
**Post date:** [November 16, 2021, 6:16am UTC](https://discourse.julialang.org/t/function-definitions-with-literal-parameters/71538/6 "2021-11-16T06:16:08Z")

</div>

MLStyle’s pattern matching and ADTs may help you

[https://thautwarm.github.io/MLStyle.jl/latest/syntax/pattern.html](https://thautwarm.github.io/MLStyle.jl/latest/syntax/pattern.html)

[https://thautwarm.github.io/MLStyle.jl/latest/syntax/adt.html](https://thautwarm.github.io/MLStyle.jl/latest/syntax/adt.html)

---

<div class="post-metadata">

**Author:** ![banksiaboy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/banksiaboy/32/27032_2.png) [@banksiaboy](https://discourse.julialang.org/u/banksiaboy)\
**Post date:** [November 16, 2021, 6:29am UTC](https://discourse.julialang.org/t/function-definitions-with-literal-parameters/71538/7 "2021-11-16T06:29:49Z")

</div>

Thanks very much I’ll have a go. Looks like I’ll learn something useful there…  
Reminding me of Haskell

---

<div class="post-metadata">

**Author:** ![banksiaboy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/banksiaboy/32/27032_2.png) [@banksiaboy](https://discourse.julialang.org/u/banksiaboy)\
**Post date:** [November 16, 2021, 6:12pm UTC](https://discourse.julialang.org/t/function-definitions-with-literal-parameters/71538/8 "2021-11-16T18:12:25Z")

</div>

I think I was also having some problems with unicode…  
Now working OK. I’m happy with the type and the function, it is a clear expression of xor.

```julia
> @enum 𝔹 zero one

> ⊻(a::𝔹, b::𝔹) = a == b ? zero : one
⊻ (generic function with 1 method)

> @show (zero ⊻ one, one ⊻ one)
(zero ⊻ one, one ⊻ one) = (one, zero)

```

---

<div class="post-metadata">

**Author:** ![banksiaboy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/banksiaboy/32/27032_2.png) [@banksiaboy](https://discourse.julialang.org/u/banksiaboy)\
**Post date:** [November 16, 2021, 6:28pm UTC](https://discourse.julialang.org/t/function-definitions-with-literal-parameters/71538/9 "2021-11-16T18:28:41Z")

</div>

I’m remembering seeing dependent types from Idris: [Gentle Introduction to Dependent Types with Idris](https://www.researchgate.net/publication/341780951_Gentle_Introduction_to_Dependent_Types_with_Idris)
