# Is possible possible to define a type signature such that \`T\` is a subtype of \`S\` except type \`W\` like \`where {Union{} != T \<: InlineString}\`

**URL:** https://discourse.julialang.org/t/is-possible-possible-to-define-a-type-signature-such-that-t-is-a-subtype-of-s-except-type-w-like-where-union-t-inlinestring/63626
**Category:** New to Julia
**Created:** [June 27, 2021, 3:08am UTC](https://discourse.julialang.org/t/is-possible-possible-to-define-a-type-signature-such-that-t-is-a-subtype-of-s-except-type-w-like-where-union-t-inlinestring/63626 "2021-06-27T03:08:01Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)
#### Post date: [June 27, 2021, 3:08am UTC](https://discourse.julialang.org/t/is-possible-possible-to-define-a-type-signature-such-that-t-is-a-subtype-of-s-except-type-w-like-where-union-t-inlinestring/63626/1 "2021-06-27T03:08:01Z")

</div>

This `where` definition doesn’t work.

```julia
function meh(::Type{T}) where {Union{} != T <: InlineString}
    print("done")
end

```

Is it possible to specify the type `T` such that it cannot be `Union{}`?

This is related to

> [@Having trouble implementating a Tables.jl row-table; when using BadukGoWeiqiTools, DataFrame(tbl) no longer works!](https://discourse.julialang.org/t/having-trouble-implementating-a-tables-jl-row-table-when-using-badukgoweiqitools-dataframe-tbl-no-longer-works/63622/3):
>
> The ambiguity is caused by promote\_type(::Type{T}, ::Type{String}). Since every type is a supertype of Union (as it’s the bottom type in the type lattice), neither method is more specific than the other. I’d investigate/open an issue about why DataFrames calls that promotion in the first place, it [shouldn’t do that](https://youtu.be/TPuJsgyu87U?t=781). WeakRefStrings seems to be behaving correctly here, since their T is restricted to being subtypes of their own type.

---

<div class="post-metadata">

### Author: ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)
#### Post date: [June 27, 2021, 5:46am UTC](https://discourse.julialang.org/t/is-possible-possible-to-define-a-type-signature-such-that-t-is-a-subtype-of-s-except-type-w-like-where-union-t-inlinestring/63626/2 "2021-06-27T05:46:14Z")

</div>

Sadly, no - you can’t dispatch based on whether something is a Union or not. You’d dispatch on how the type is represented, instead of the type (or rather, the set of values the type represents) itself. There was some talk at juliacon some time ago where this [was mentioned](https://youtu.be/TPuJsgyu87U?t=781).

---

<div class="post-metadata">

### Author: ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)
#### Post date: [June 27, 2021, 6:19am UTC](https://discourse.julialang.org/t/is-possible-possible-to-define-a-type-signature-such-that-t-is-a-subtype-of-s-except-type-w-like-where-union-t-inlinestring/63626/3 "2021-06-27T06:19:21Z")

</div>

But what you _can_ do is write a more specific function:

```julia
f(x::Fruit) = println("x is not an apple")
f(x::Apple) = println("x is an apple")

```

---

<div class="post-metadata">

### Author: ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)
#### Post date: [June 27, 2021, 8:19am UTC](https://discourse.julialang.org/t/is-possible-possible-to-define-a-type-signature-such-that-t-is-a-subtype-of-s-except-type-w-like-where-union-t-inlinestring/63626/4 "2021-06-27T08:19:44Z")

</div>

```julia
function meh(::T) where {T <: InlineString}
    print("done")
end

```

In this case it is impossible for `T` to be `Union{}`, since `Union{}` does not have any instances.

---

<div class="post-metadata">

### Author: ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)
#### Post date: [June 27, 2021, 8:29am UTC](https://discourse.julialang.org/t/is-possible-possible-to-define-a-type-signature-such-that-t-is-a-subtype-of-s-except-type-w-like-where-union-t-inlinestring/63626/5 "2021-06-27T08:29:16Z")

</div>

> [@simeonschaub](#):
>
> ```julia
> function meh(::T) where {T <: InlineString}
> print("done")
> end
> 
> ```

This is a bad example this is better

```julia
function meh(::Type{T}) where {T <: AbstractString}

  print("done")

end

meh(Union{})

```

---

<div class="post-metadata">

### Author: ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)
#### Post date: [June 27, 2021, 8:38am UTC](https://discourse.julialang.org/t/is-possible-possible-to-define-a-type-signature-such-that-t-is-a-subtype-of-s-except-type-w-like-where-union-t-inlinestring/63626/6 "2021-06-27T08:38:57Z")

</div>

You could just add another method for that case:

```julia
meh(::Type{Union{}}) = throw(MethodError(meh, (Union{},))

```

---

<div class="post-metadata">

### Author: ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)
#### Post date: [June 27, 2021, 10:12am UTC](https://discourse.julialang.org/t/is-possible-possible-to-define-a-type-signature-such-that-t-is-a-subtype-of-s-except-type-w-like-where-union-t-inlinestring/63626/7 "2021-06-27T10:12:54Z")

</div>

that’s not a bad solution. but I think the fundamental issue is that to packages used together broke my packages’ code and it really shouldn’t happen.

---

<div class="post-metadata">

### Author: ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)
#### Post date: [June 27, 2021, 12:46pm UTC](https://discourse.julialang.org/t/is-possible-possible-to-define-a-type-signature-such-that-t-is-a-subtype-of-s-except-type-w-like-where-union-t-inlinestring/63626/8 "2021-06-27T12:46:57Z")

</div>

Yes, this is an issue with the package since it is committing type piracy. (Also, please don’t quote links to other posts instead of making them clickable, since copy-pasting is quite annoying on mobile)

---

<div class="post-metadata">

### Author: ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)
#### Post date: [June 27, 2021, 2:14pm UTC](https://discourse.julialang.org/t/is-possible-possible-to-define-a-type-signature-such-that-t-is-a-subtype-of-s-except-type-w-like-where-union-t-inlinestring/63626/9 "2021-06-27T14:14:01Z")

</div>

> [@simeonschaub](#):
>
> please don’t quote links

mistake. didn’t mean to
