Better Programming

Advice for programmers.

Follow publication

Member-only story

Type Placeholders: A Look at the New Swift 5.6 Feature

Type placeholders were recently introduced in Swift 5.6. Get in touch with a new useful Swift feature.

Alex Dremov
Better Programming
Published in
2 min readApr 21, 2022

Type placeholders were recently introduced in Swift 5.6. And yes, they are a nice add-on to the 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

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

Yes, Swift can infer a 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

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

And what if we have some kind of complex object

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.

let result = Result.success(ohMy)

Note: Generic parameter Failure could not be inferred

Create an account to read the full story.

The author made this story available to Medium members only.
If you’re new to Medium, create a new account to read this story on us.

Or, continue in mobile web

Already have an account? Sign in

Responses (1)

Write a response

What I applaud in Mr. Carnevale's piece is an attempt to shift the attention away from affirmative action and college (now, there's a silo) to education reform of K-12 education. In my view, the problem starts when humans by and large support their…

Affirmative Action still around?

interesting