LAST CHANCE: Save 50% on all my Swift books and bundles! >>

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 Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until July 28th.

Click to save your free spot now

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

Reply to this topic…

You need to create an account or log in to reply.

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.