flâneur — a map of the web's best reading

Opaque and Boxed Protocol Types | Documentation

docs.swift.org · 92 words · saved by 1 readers

Swift provides two ways to hide details about a value’s type: opaque types and boxed protocol types. 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. A function or method that returns an opaque 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. Opaque types preserve type identity — the compiler has access to the type information, but clients of the module don’t. A boxed protocol type can store an instance of any type that conforms to the given protocol. Boxed protocol types don’t preserve type identity — the value’s specific type isn’t known until runtime, and it can change over time as different values are stored. For example, suppose you’re writing a module that draws ASCII art shapes. The basic characteristic of an ASCII art shape i

To navigate the symbols, press Up Arrow, Down Arrow, Left Arrow or Right Arrow No items were found 1 item was found {number} items were found. Tab back to navigate through them. re looking for can't be found."},"color-scheme":{"select":"Select a color scheme preference","auto":"Auto","dark":"Dark","light":"Light"},"accessibility":{"strike":{"start":"start of stricken text","end":"end of stricken text"},"code":{"start":"start of code block","end":"end of code block"},"skip-navigation":"Skip Navigation","in-page-link":"in page link"},"select-language":"Select the language for this page","icons":

Explore this link on the map →

related reading