# Can I make a named Union of Types

**URL:** <https://discourse.julialang.org/t/can-i-make-a-named-union-of-types/66177>\
**Category:** General Usage\
**Created:** [August 11, 2021, 5:52am UTC](https://discourse.julialang.org/t/can-i-make-a-named-union-of-types/66177 "2021-08-11T05:52:25Z")\
**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:** [August 11, 2021, 5:52am UTC](https://discourse.julialang.org/t/can-i-make-a-named-union-of-types/66177/1 "2021-08-11T05:52:25Z")

</div>

I am learning how to make one library compatible with another. They both have different type hierarchies for representing a file path.

One uses `AbstractString`, the other uses `SystemPath` for their abstract representation of paths. Their use of low-level interfaces like `open()` dispatch in a compatible way.

I don’t want to change the parameters of each high-level method to all be `Union{SystemPath,AbstractString}`

I’d like to give that Union Type a name and use that to type the appropriate parameters.

Can I do that? If not, how should I express this?

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [August 11, 2021, 5:55am UTC](https://discourse.julialang.org/t/can-i-make-a-named-union-of-types/66177/2 "2021-08-11T05:55:08Z")

</div>

`const myname = Union{...`

will do

---

<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:** [August 11, 2021, 5:57am UTC](https://discourse.julialang.org/t/can-i-make-a-named-union-of-types/66177/3 "2021-08-11T05:57:14Z")

</div>

And… it’s sort of distressing, because each library sort of shouldn’t have to know about each other.

I could almost make a mixin library which just contained functions to implement compatible dispatch.

Then I’d be making a patchwork. Which doesn’t seem great either.

---

<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:** [August 11, 2021, 6:14am UTC](https://discourse.julialang.org/t/can-i-make-a-named-union-of-types/66177/4 "2021-08-11T06:14:50Z")

</div>

I might not want the type restriction, in order to allow other pathlike types that haven’t been written yet.

---

<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:** [August 11, 2021, 6:36am UTC](https://discourse.julialang.org/t/can-i-make-a-named-union-of-types/66177/5 "2021-08-11T06:36:09Z")

</div>

Just making sure I understand…  
So rather than adding another type in the parameter - as the Union, remove the type specification altogether.  
That seems like a good idea, Then the peer library just provides methods to make itself work in the context - just like open(::PosixPath) does already?

That leaves both libraries not needing to reference each-other.

---

<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:** [August 11, 2021, 6:42am UTC](https://discourse.julialang.org/t/can-i-make-a-named-union-of-types/66177/6 "2021-08-11T06:42:56Z")

</div>

> [@banksiaboy](#):
>
> So rather than adding another type in the parameter - as the Union, remove the type specification altogether.

Right. Ideally julia would have a more flexible interface system so it could support something like a “pathlike” trait or specification, but that’s not currently available.

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [August 11, 2021, 6:48am UTC](https://discourse.julialang.org/t/can-i-make-a-named-union-of-types/66177/7 "2021-08-11T06:48:59Z")

</div>

if you don’t need to do specialized things, you don’t need to clamp down types

---

<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:** [August 11, 2021, 6:51am UTC](https://discourse.julialang.org/t/can-i-make-a-named-union-of-types/66177/8 "2021-08-11T06:51:15Z")

</div>

we used to call that a `protocol` in Smalltalk. A set of method names that instances had to understand to support a functionality. Like the `ReadStream` protocol e.g. `#(#next #next: #atEnd)`, which could be implemented by classes which were unrelated in the class hierarchy. Then any object expecting `ReadStream` could use them. No compiler support though 🙂
