# Down the Swift associated type rabbit hole

DevFeed: [Down the Swift associated type rabbit hole](<https://devfeed.tech/articles/down-the-swift-associated-type-rabbit-hole-20361.md>)

Original publisher: [Read original article](<https://engineering.ziffmedia.com/down-the-swift-associated-type-rabbit-hole-1f41ee6ceaf4?source=rss----d6bb34696ef5---4>)

Author: Adam Chalmers

Published: 2017-11-01T17:18:03Z

Content type: tutorial

Language: en

Sources: [RetailMeNot](<https://devfeed.tech/sources/retailmenot.md>)

Topics: [Swift](<https://devfeed.tech/topics/swift.md>), [Programming](<https://devfeed.tech/topics/programming.md>), [Data structures](<https://devfeed.tech/topics/data-structures.md>)

Tags: [array](<https://devfeed.tech/tags/array.md>), [arrays](<https://devfeed.tech/tags/arrays.md>), [associatedtype](<https://devfeed.tech/tags/associatedtype.md>), [computer-science](<https://devfeed.tech/tags/computer-science.md>), [generics](<https://devfeed.tech/tags/generics.md>), [languages](<https://devfeed.tech/tags/languages.md>), [memory](<https://devfeed.tech/tags/memory.md>), [programming](<https://devfeed.tech/tags/programming.md>), [programming-languages](<https://devfeed.tech/tags/programming-languages.md>), [swift](<https://devfeed.tech/tags/swift.md>), [types](<https://devfeed.tech/tags/types.md>)

## AI overview

This tutorial explains a Swift programming problem involving associated types, generic constraints, and where clauses. It works toward a function that combines two Arrays or ArraySlices while avoiding duplicate elements and keeping the inputs and output the same type. It also explains how ArraySlice views can avoid allocating additional memory.

## Source excerpt

If you want to make Swift programmer shudder, just whisper the words "associated types." They're one of the few Swift language typing features you're unlikely to find in other programming languages, so they can take some getting used to. Last week I tried to write a seemingly simple function, and ended up spending most of my day diving down the associated type rabbit hole. This post is going to explain how I (eventually) wrote the "simple" function, and hopefully teach you about associated types, generic constraints and "where clauses" along the way! What's the goal? I wanted a function that would: Take in two Arrays (or ArraySlices), base and newElems as parameters, and return a new array The output array should be made by adding each element of newElems to base, but only if it wasn't already in base All input and output array should be the same type T Note: in Swift, getting a subrange of an array returns an ArraySlice instead of another array. ArraySlices are views into part of the original array -- they're basically just two pointers into the start and end of the array subsection. They can be really helpful!Imagine you had an array with 2 billion elements, and wanted to examine the subarray from elements 5 to 2 billion. Without ArraySlices, examining that subarray would require allocating another 1.9 billion-length array, which would be a waste of space and time. Instead, we can just make an ArraySlice that tracks where the subarray starts and ends, without allocating any more memory. Hell yeah.Wrestling with type signatures ArraySlices are great. There's just one downside: ArraySlice is a different type to Array -- and remember, our function should take either type. So if you write a function like this: https://medium.com/media/c1502a3275e60376a082465bfdd57bb1/href Then Swift won't let you pass an ArraySlice into it. Sure, we can just convert our ArraySlices to Arrays first, but that's no fun. Instead, let's see if there's some protocol that both Array and ArrayS