# Dependency injection using \`typealias\` declarations

**URL:** <https://forums.swift.org/t/dependency-injection-using-typealias-declarations/18395>\
**Category:** Using Swift\
**Created:** [November 29, 2018, 4:44pm UTC](https://forums.swift.org/t/dependency-injection-using-typealias-declarations/18395 "2018-11-29T16:44:38Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![jjrscott](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jjrscott/32/12494_2.png) [@jjrscott](https://forums.swift.org/u/jjrscott)\
**Post date:** [November 29, 2018, 4:44pm UTC](https://forums.swift.org/t/dependency-injection-using-typealias-declarations/18395/1 "2018-11-29T16:44:38Z")

</div>

Hi folks, has anyone else used `typealias` declarations as their dependency injection mechanism? I haven't found anything so I wrote it up myself ([Dependency injection · Swift DI with type alias declarations](https://jjrscott.com/dependency-injection-with-typealias/)) but I feel like it should either be used, or there's a really good reason why it's even worse than all the other options.

Here's an example of what I mean:

Injection point declaration

```swift
#if TEST
typealias AnimalForUseInSomeSpecficContext = Cow
#else
typealias AnimalForUseInSomeSpecficContext = Sheep
#endif

```

Real type declaration

```swift
struct Cow { let name: String }
struct Sheep { let name: String }

```

Usage

```swift
let animal = AnimalForUseInSomeSpecficContext(name: "Daisy")

print(animal) // Cow(name: "Daisy")

```

Note that I don't _need_ protocols, or any other magic to make this work; the compile with fail something isn't correct.

Any ideas as to what's wrong with it?

---

<div class="post-metadata">

**Author:** ![mattt](https://avatars.discourse-cdn.com/v4/letter/m/46a35a/32.png) [@mattt](https://forums.swift.org/u/mattt)\
**Post date:** [November 29, 2018, 5:12pm UTC](https://forums.swift.org/t/dependency-injection-using-typealias-declarations/18395/2 "2018-11-29T17:12:21Z")

</div>

One downside with this approach is that the type system can't make any guarantees about what `animal` is when your compilation context changes. If, for example, `Cow` were to change its `name` property to `nickname`, you wouldn't necessarily find out that your code calling `animal.name` broke until you tried compiling with `TEST` enabled.

A better approach might be to use a combination of protocols, type erasure, and conditional compilation like so:

```swift
protocol Animal {
    var name: String { get }
}

struct Cow: Animal { let name: String }
struct Sheep: Animal { let name: String }

enum DI {
    static var daisy: Animal {
        #if TEST
        return Sheep(name: "Daisy")
        #else
        return Cow(name: "Daisy")
        #endif
    }
}

let animal = DI.daisy

print(animal) // Cow(name: "Daisy")

```

---

<div class="post-metadata">

**Author:** ![jjrscott](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jjrscott/32/12494_2.png) [@jjrscott](https://forums.swift.org/u/jjrscott)\
**Post date:** [November 29, 2018, 5:51pm UTC](https://forums.swift.org/t/dependency-injection-using-typealias-declarations/18395/3 "2018-11-29T17:51:05Z")

</div>

I've toyed around with using protocols to keep the two types in line, but I seem to always end up with miles of boilerplate code. I also found that - at least for mock objects - I might actually _want_ the two to diverge slightly so that I wasn't writing stubs merely to keep the compiler happy.

With code like the example above, I've had to move the initialisation to a different location, away from its point of use which I then find difficult to track 🙁.

If I did want type erase I played around with:

```swift
#if TEST
let AnimalForUseInSomeSpecficContext : Animal.Type = Cow.self
#else
let AnimalForUseInSomeSpecficContext : Animal.Type = Sheep.self
#endif

```

It did have the (small) downside of requiring `init` to be called explicitly, producing this

```swift
let animal = AnimalForUseInSomeSpecficContext.init(name: "Daisy")
print(animal) // Cow(name: "Daisy")

```

Do you find things normal get moved out of context when using DI?

---

<div class="post-metadata">

**Author:** ![jrose](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jrose/32/408_2.png) [@jrose](https://forums.swift.org/u/jrose)\
**Post date:** [November 29, 2018, 6:01pm UTC](https://forums.swift.org/t/dependency-injection-using-typealias-declarations/18395/4 "2018-11-29T18:01:13Z")

</div>

I'm not sure how I feel about the whole setup in the first place, but you can use generic typealiases to enforce constraints:

```swift
protocol Animal {
  var name: String { get }
}

typealias DIAnimal<T: Animal> = T

struct Truck {}
typealias AnimalForUseInSomeSpecficContext = DIAnimal<Truck> // error

```

Alternately, you could have Mattt's DI enum conform to a protocol with constrained associated types, and then your typealiases get validated.

---

<div class="post-metadata">

**Author:** ![jjrscott](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jjrscott/32/12494_2.png) [@jjrscott](https://forums.swift.org/u/jjrscott)\
**Post date:** [November 29, 2018, 7:00pm UTC](https://forums.swift.org/t/dependency-injection-using-typealias-declarations/18395/5 "2018-11-29T19:00:26Z")

</div>

That's really cool, thank you. I (obviously) like the idea of keeping the call-site unchanged.

Is there a way to genericize that further to allow an arbitrary protocol? Something that looks like this (but successfully compiles):

```swift
typealias Constrain<T: P, P> = T 

```

---

<div class="post-metadata">

**Author:** ![DevAndArtist](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/devandartist/32/287_2.png) [@DevAndArtist](https://forums.swift.org/u/DevAndArtist)\
**Post date:** [November 29, 2018, 7:03pm UTC](https://forums.swift.org/t/dependency-injection-using-typealias-declarations/18395/6 "2018-11-29T19:03:54Z")

</div>

Yes this is once been pitched by @anandabits as ["Generalized supertype constraint"](https://forums.swift.org/t/pitch-generalized-supertype-constraints/7121).

---

<div class="post-metadata">

**Author:** ![anandabits](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/anandabits/32/355_2.png) [@anandabits](https://forums.swift.org/u/anandabits)\
**Post date:** [November 29, 2018, 7:08pm UTC](https://forums.swift.org/t/dependency-injection-using-typealias-declarations/18395/7 "2018-11-29T19:08:12Z")

</div>

You beat me to it @DevAndArtist. This is still one of my most desired generics enhancements. @Douglas_Gregor has said it shouldn’t be too hard to support but the idea hasn’t gotten much attention. I think it is mainly waiting for someone with the time and ability to work on implementation. I would be happy to drive the SE proposal if somebody was willing to do that. 🙂

---

<div class="post-metadata">

**Author:** ![DevAndArtist](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/devandartist/32/287_2.png) [@DevAndArtist](https://forums.swift.org/u/DevAndArtist)\
**Post date:** [November 29, 2018, 7:10pm UTC](https://forums.swift.org/t/dependency-injection-using-typealias-declarations/18395/8 "2018-11-29T19:10:35Z")

</div>

Yeah I'm in the same boat, I really love the idea of that pitch because it will make generics so much more flexible.

---

<div class="post-metadata">

**Author:** ![jjrscott](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jjrscott/32/12494_2.png) [@jjrscott](https://forums.swift.org/u/jjrscott)\
**Post date:** [November 29, 2018, 7:24pm UTC](https://forums.swift.org/t/dependency-injection-using-typealias-declarations/18395/9 "2018-11-29T19:24:41Z")

</div>

I too would love that kind of thing, I think I've hit that issue a few times as it seems so natural to expect it to work already. Fortunately for me in this context, it's merely a nice-to-have.

---

<div class="post-metadata">

**Author:** ![jjrscott](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jjrscott/32/12494_2.png) [@jjrscott](https://forums.swift.org/u/jjrscott)\
**Post date:** [November 29, 2018, 7:38pm UTC](https://forums.swift.org/t/dependency-injection-using-typealias-declarations/18395/10 "2018-11-29T19:38:56Z")

</div>

Although the code is welcome, I am probably more interested in your concerns that prompted this:

> [@jrose](#):
>
> I'm not sure how I feel about the whole setup in the first place

I don't want to be using something that disrespects Swift, Swiftiness, or might break somewhere down the road.

---

<div class="post-metadata">

**Author:** ![jrose](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jrose/32/408_2.png) [@jrose](https://forums.swift.org/u/jrose)\
**Post date:** [November 29, 2018, 7:43pm UTC](https://forums.swift.org/t/dependency-injection-using-typealias-declarations/18395/11 "2018-11-29T19:43:53Z")

</div>

Eh, I don't think I can put it into useful feedback. I don't like "dependency injection" as a pattern in general, and using a static configuration as described in your blog post means you're more likely to end up testing something that's different from what you ship. But being for or against dependency injection is different from thinking something is Swifty or not.

(I do think that having a protocol here is a good thing, even with the extra maintenance.)

---

<div class="post-metadata">

**Author:** ![jjrscott](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jjrscott/32/12494_2.png) [@jjrscott](https://forums.swift.org/u/jjrscott)\
**Post date:** [November 29, 2018, 9:06pm UTC](https://forums.swift.org/t/dependency-injection-using-typealias-declarations/18395/12 "2018-11-29T21:06:05Z")

</div>

I'm generally with you regarding dependency injection. I didn't see the need/benefit for it until UI testing became a bigger thing in iOS. If I have to though I want it to be approximately transparent in the code where it's used, and also in the stack-trace (which seems to be ignored far too much).

Being able to enforce protocol conformance (if/when I want it) in a way that is transparent to the call-site will really help.
