# Proposal for ∈ isa type syntax

**URL:** <https://discourse.julialang.org/t/proposal-for-isa-type-syntax/24667>\
**Category:** Internals & Design\
**Tags:** proposal, syntax\
**Created:** [May 28, 2019, 12:56am UTC](https://discourse.julialang.org/t/proposal-for-isa-type-syntax/24667 "2019-05-28T00:56:10Z")\
**Posts on this page:** 12\
**Page:** 2

<div class="post-metadata">

**Author:** ![tkluck](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkluck/32/15769_2.png) [@tkluck](https://discourse.julialang.org/u/tkluck)\
**Post date:** [May 30, 2019, 9:48pm UTC](https://discourse.julialang.org/t/proposal-for-isa-type-syntax/24667/21 "2019-05-30T21:48:37Z")

</div>

> [@Tamas\_Papp](#):
>
> (This is orthogonal to the proposal in this topic).

On the contrary. The topic is whether the is-element-of-set operator should be the same as the is-of-type operator. I was arguing that it shouldn’t by giving an example (again, within Julia) of an alternative reasonable meaning: is-convertible-to. That seems quite on-topic.

To emphasize the point a bit more, let me paste this snippet:

```julia
julia> 1//1 ∈ Set([1,2,3])
true

```

Under this line of reasoning, `1//1 ∈ Integer` should be `true`.

Just to be clear – neither of these interpretations to me is _sufficiently_ reasonable, or even _sufficiently more reasonable than the other_, that I think julia should provide `in(needle, haystack::Type)` by default.

@tamas_papp I did feel you were a bit more dismissive of my point than I deserved.

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [May 30, 2019, 10:26pm UTC](https://discourse.julialang.org/t/proposal-for-isa-type-syntax/24667/22 "2019-05-30T22:26:36Z")

</div>

Agreed. Perhaps an even clearer example is:

```julia
julia> 1//1 ∈ typemin(Int8):typemax(Int8)
true

```

That range contains all the elements “in” `Int8`. Even if you might want to describe `1//1` as an integer value, I certainly don’t think it’s very helpful to describe it as an `Int8`.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [May 31, 2019, 5:40am UTC](https://discourse.julialang.org/t/proposal-for-isa-type-syntax/24667/23 "2019-05-31T05:40:54Z")

</div>

> [@tkluck](#):
>
> Just to be clear – neither of these interpretations to me is _sufficiently_ reasonable, or even _sufficiently more reasonable than the other_

Again, I think the key question is not philosophical or mathematical reasonableness/elegance, but

1. whether there is a scenario where your version of `x in T` would be _useful_,
2. whether it can be implemented in a practical way (I doubt this, but I keep an open mind).

> [@tkluck](#):
>
> @tamas_papp I did feel you were a bit more dismissive of my point than I deserved.

I simply don’t agree with your proposal, and it is not fully clear to me how it would work. Perhaps if you provided an implementation, it would clarify things (I already understand that it is different from aliasing `isa`, which is the original proposal in this topic).

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [May 31, 2019, 1:42pm UTC](https://discourse.julialang.org/t/proposal-for-isa-type-syntax/24667/24 "2019-05-31T13:42:41Z")

</div>

But it’s not a proposal — it’s simply pointing out the disconnect between our current `∈` semantics and `isa`.

- If `∈` means that the first argument is equal to something in the collection in the second
- and if we think of a type as a set containing all values `x::T`
- then the behavior that results from `x ∈ T` is _not_ `isa`, but rather we end up with the strange and unhelpful behavior @tkluck posted.

> [@tkluck](#):
>
> To be clear, I’m not advocating this (at all). Just showing why `∈ == isa` is not so natural.

---

<div class="post-metadata">

**Author:** ![chakravala](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chakravala/32/6832_2.png) [@chakravala](https://discourse.julialang.org/u/chakravala)\
**Post date:** [May 31, 2019, 1:55pm UTC](https://discourse.julialang.org/t/proposal-for-isa-type-syntax/24667/25 "2019-05-31T13:55:12Z")

</div>

> [@mbauman](#):
>
> But it’s not a proposal — it’s simply pointing out the disconnect between our current `∈` semantics and `isa` .

To settle the issue on what my intention is, I actually was thinking more along the lines of assigning an `in` function for specific types used in mathematics.

Ideally, each type would have its own parameters for the definition design of `in`. This means that some types in `Base` would need explicit definitions, and external packages could do it for their own types (if mathematically applicable).

The global example was good for stirring up the conversation though. Every object doesn’t need `getindex`, they also don’t all need `in`. However, sometimes it would be beneficial to have defined.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [May 31, 2019, 1:57pm UTC](https://discourse.julialang.org/t/proposal-for-isa-type-syntax/24667/26 "2019-05-31T13:57:19Z")

</div>

Thanks for clarifying, I have apparently misunderstood.

---

<div class="post-metadata">

**Author:** ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)\
**Post date:** [May 31, 2019, 2:09pm UTC](https://discourse.julialang.org/t/proposal-for-isa-type-syntax/24667/27 "2019-05-31T14:09:22Z")

</div>

> [@chakravala](#):
>
> To settle the issue on what my intention is, I actually was thinking more along the lines of assigning an `in` function for specific types used in mathematics.

Could you clarify why you’d want this behavior? Is it just for elegant notation or is there some context where you need to be using sets and types interchangeably?

I think this sort of behavior is unlikely to make it into base, but you can make your own Unicode modified version of `\in` that has the behavior you want. For instance, you can define `\in\prime` as follows:

```julia
(∈′)(a, B::Type) = a isa B 

julia> 1 ∈′ Integer 
true

```

---

<div class="post-metadata">

**Author:** ![chakravala](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chakravala/32/6832_2.png) [@chakravala](https://discourse.julialang.org/u/chakravala)\
**Post date:** [May 31, 2019, 2:17pm UTC](https://discourse.julialang.org/t/proposal-for-isa-type-syntax/24667/28 "2019-05-31T14:17:41Z")

</div>

> [@Mason](#):
>
> Could you clarify why you’d want this behavior? Is it just for elegant notation or is there some context where you need to be using sets and types interchangeably?

It’s only for the sake of notation, for me at least.

I wanted to see what people have to say about it.

---

<div class="post-metadata">

**Author:** ![dlfivefifty](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlfivefifty/32/1959_2.png) [@dlfivefifty](https://discourse.julialang.org/u/dlfivefifty)\
**Post date:** [May 31, 2019, 6:43pm UTC](https://discourse.julialang.org/t/proposal-for-isa-type-syntax/24667/29 "2019-05-31T18:43:17Z")

</div>

My usage would be for defining functions defined on all values of a type. For example, it would be reasonable to write

```julia
f = Fun(x-> exp(-x^2), Float64)

```

And have an approximation to a Gaussian that is guaranteed to be valid for all `Float64`. In this setting, if you want to know before calling if a function is defined at a point one can call `1 in domain(f)`.

Though wrappers around types give a perfectly fine implementation of the above behaviour, so there would only be minor benefit to types supporting in.

---

<div class="post-metadata">

**Author:** ![abulak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abulak/32/28314_2.png) [@abulak](https://discourse.julialang.org/u/abulak)\
**Post date:** [June 4, 2019, 12:57pm UTC](https://discourse.julialang.org/t/proposal-for-isa-type-syntax/24667/30 "2019-06-04T12:57:14Z")

</div>

I think You would be much better by providing a singleton `Floats{Float64}()` for this use;

A solution to _Does type represent a set/algebraic object?_ problem was implemented in `AbstractAlgebra.jl` by exactly introducing singleton parent objects, as types (as they are in julia) are not enough to encompass whole algebraic structure on a set. Or maybe they are enough at the price of performance, which nobody likes to pay 😉

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [June 4, 2019, 1:00pm UTC](https://discourse.julialang.org/t/proposal-for-isa-type-syntax/24667/31 "2019-06-04T13:00:05Z")

</div>

> [@abulak](#):
>
> Or maybe they are enough at the price of performance, which nobody likes to pay 😉

I am not sure I understand, is there a performance cost to eg

```julia
struct Floats{T} end

Base.in(x, ::Floats{T}) where T = x isa T

```

?

---

<div class="post-metadata">

**Author:** ![abulak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abulak/32/28314_2.png) [@abulak](https://discourse.julialang.org/u/abulak)\
**Post date:** [June 4, 2019, 2:54pm UTC](https://discourse.julialang.org/t/proposal-for-isa-type-syntax/24667/32 "2019-06-04T14:54:51Z")

</div>

no, to this one there is no performance cost;

However if Your algebraic structure is e.g. parametrized by an integer `N` (e.g. think modulus) and the algorithm will use a different set of `N`s (depending on value of arguments), then compilation time stops being constant…

[Previous page](https://discourse.julialang.org/t/proposal-for-isa-type-syntax/24667.md?page=1)
