---
title: "Type Placeholders: New Swift 5.6 Feature"
slug: "swift-type-placeholders"
date: "2022-04-21T04:00:00.000Z"
description: "Type placeholders were recently introduced in Swift 5.6. Get in touch with new useful Swift feature."
---

# Type Placeholders: New Swift 5.6 Feature

Type placeholders were recently introduced in Swift 5.6. And yes, they are a nice add-on to powerful Swift type inference system. If you are familiar with C++, you must know about an `auto` keyword. Type placeholders are _almost_ the same.

## Generics and type placeholder

```swift
let number: _ = 42 // Type placeholder
let anotherNumber = 42
```

Yes, Swift can infer variable's type, but type placeholders mean to be used for a type with **multiple types in it**. Generics. That's where they really shine.

Consider regular `Result` enum

```swift
enum Result<Success, Failure> where Failure : Error {
    case success(Success)
    case failure(Failure)
}
```

And what if we have some kind of complex object

```swift
var ohMy = [1: [3: (1, 2, 3, "That's a long tuple")]]
```

If you will try to create a `Result`  from `ohMy`, you'll see compilation error.

```swift
let result = Result.success(ohMy)
```

😡

Generic parameter `Failure` could not be inferred

Bruh. So I need to write...

```swift
let result = Result<[Int : [Int : (Int, Int, Int, String)]], Error>.success(ohMy)
```

💡

Use type placeholders to omit type that Swift can infer

Thanks to type placeholders, no. Swift can infer object's type by itself. So, we need to provide `Failure` type only.

```swift
let result = Result<_, Error>.success(ohMy) // Nice
```

Subscribe and don't miss posts!

## Collections and type placeholder

This feature also useful with collections. What if we need a dictionary with enum keys?

```swift
enum Foo {
	case bizz
	case bonk
}

let results = [
	.bizz: ohMy,
	.bonk: ohMy
]
```

😡

Reference to member `bizz` cannot be resolved without a contextual type

So, let's provide this _contextual type,_ but you remember how `ohMy`'s type is bad-looking? Let's use type placeholder.

```swift
// 🚫
let results:[Foo: [Int : [Int : (Int, Int, Int, String)]]] = [
	.bizz: ohMy,
	.bonk: ohMy
]

// ✅
let results:[Foo: _] = [
	.bizz: ohMy,
	.bonk: ohMy
]
```

## More examples

Examples of types containing placeholders are:

```swift
Array<_> // array with placeholder element type
[Int: _] // dictionary with placeholder value type
(_) -> Int // function type accepting a single type placeholder argument and returning 'Int'
(_, Double) // tuple type of placeholder and 'Double'
_? // optional wrapping a type placeholder
```

## Final notes

That's a great feature and broadens Swift’s type inference capabilities. For now, it's some kind of less-known, but I think it will be more used in the future.

You can check out other less-known Swift features in my previous post:

[

Top 7 Subtle Swift Features | Alex Dremov

Here, I collected Swift features that are less known and can be useful when you prepare for interviews or want to deepen your Swift knowledge.

![](https://alexdremov.me/assets/icons/apple-touch-icon.png)Alex DremovAlex Dremov

![](https://alexdremov.me/content/images/2022/04/Artboard-1-1.png)

](https://alexdremov.me/top-7-subtle-swift-features/)

## References

[

swift-evolution/0315-placeholder-types.md at main · apple/swift-evolution

This maintains proposals for changes and user-visible enhancements to the Swift Programming Language. - swift-evolution/0315-placeholder-types.md at main · apple/swift-evolution

![](https://github.com/fluidicon.png)GitHubapple

![](https://opengraph.githubassets.com/c17082dded0015da2efcc475a488d2710ae6bf2f831faac7614e539d5739e9a2/apple/swift-evolution)

](https://github.com/apple/swift-evolution/blob/main/proposals/0315-placeholder-types.md)