BLACK FRIDAY: 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      

Save 50% in my WWDC sale.

SAVE 50% All our books and bundles are half price for Black Friday, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

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.