GO FURTHER, FASTER: Try the Swift Career Accelerator today! >>

Why can't I use an opaque return type here

Forums > 100 Days of SwiftUI

extension Numeric {
    func squared() -> Self {
        self * self
    }
}

In this code, the return type of our squared function is Self which according to Paul, is basically self's data type. However, why can't I use opaque return type so that Swift would figure out what the return type would be on its own?

Something like this

extension Numeric {
    func squared() -> some Numeric {
        self * self
    }
}

3      

Because it wouldn't know what the type 'self' is - here is an explanation of the difference between 'Self' and 'self'. The 'self' is just the value, and if you don't provide the type it is just a collection of binary digits that could represent or be anything.

3      

Hacking with Swift is sponsored by Essential Developer.

SPONSORED Transform your career with the iOS Lead Essentials. Unlock over 40 hours of expert training, mentorship, and community support to secure your place among the best devs. Click for early access to this limited offer and a FREE crash course.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.