DEV Community

Cover image for Opaque Types in Swift
Saif Ullah Sajid
Saif Ullah Sajid

Posted on

Opaque Types in Swift

A function or method with an opaque return type hides its return value’s type information. Instead of providing a concrete type as the function’s return type, the return value is described in terms of the protocols it supports. Hiding type information is useful at boundaries between a module and code that calls into the module, because the underlying type of the return value can remain private. Unlike returning a value whose type is a protocol type, opaque types preserve type identity, the compiler has access to the type information, but clients of the module don’t.

For example, the function in the following example returns a trapezoid without exposing the underlying type of that shape.

Source: https://docs.swift.org/swift-book/LanguageGuide/OpaqueTypes.html

swiftui #iosdevelopment #iosdevelopers #iosdeveloper #apple #programming #coding #applicationdevelopment #iosdev #appdevelopment #learning #knowledgesharing

Heroku

Simplify your DevOps and maximize your time.

Since 2007, Heroku has been the go-to platform for developers as it monitors uptime, performance, and infrastructure concerns, allowing you to focus on writing code.

Learn More

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay