# Can I “couple” two types?

**URL:** <https://discourse.julialang.org/t/can-i-couple-two-types/640>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [November 29, 2016, 9:53pm UTC](https://discourse.julialang.org/t/can-i-couple-two-types/640 "2016-11-29T21:53:24Z")\
**Posts on this page:** 1\
**Showing post:** 8

<div class="post-metadata">

**Author:** ![akis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/akis/32/156_2.png) [@akis](https://discourse.julialang.org/u/akis)\
**Post date:** [November 30, 2016, 7:47pm UTC](https://discourse.julialang.org/t/can-i-couple-two-types/640/8 "2016-11-30T19:47:18Z")

</div>

Here is the promised code for the **parameter** approach.

First the “coupling” interface:

```julia
abstract A{L, N} # could also be an intermediate subtype of A

f{L}(p::A{L, 1}, q::A{L, 2})::A{L, 1} = # calculation goes here

```

Function purposely named `f`, cause it’s not the one in the question. But can be used if `q` is not a `Vector`. We are going to add support for `Vector`s soon.

Next an example implementation, e.g. for B types:

```julia
abstract B # could avoid that by using directly a symbol, e.g. :B

type B1 <: A{B, 1}
# fields
end

type B2 <: A{B, 2}
# fields
end

```

We can use current implementation like: ` f(B1(args), B2(args))`

Now for `Vector` support. As pointed out in the [manual](http://docs.julialang.org/en/release-0.5/manual/types/#parametric-composite-types):

```julia
julia> B2 <: A{B, 2} # by definition
true

julia> Vector{B2} <: Vector{A{B, 2}} # invariant
false

```

So something like `f{L}(p::A{L, 1}, q::Vector{A{L, 2}})` won’t work. To overcome that, we can add an abstract type to the interface and use it in the general function:

```julia
abstract VectorOfA{L, N}

quiteGeneral{L}(p::A{L, 1}, q::VectorOfA{L, 2})::A{L, 1} = # calculation with q.v goes here

```

Then update the example implementation with a simple `Vector` wrapper:

```julia
immutable VectorOfB2 <: VectorOfA{B, 2}
  v::Vector{B2}
end

```

Now we can make calls like `quiteGeneral(B1(args), VectorOfB2([B2(args), B2(args), ...]))`.

That covers the question to the best of my understanding. So if that’s not enough, I will need extra information about the needs.

---

_[View the full topic](https://discourse.julialang.org/t/can-i-couple-two-types/640)._
