Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Terminology of Optionals in Swift or other languages

In Swift the elements we manipulates all have types.

When we use theses types, we can add a '!', '?' or nothing to express their nullability.

  1. What shall I call the '?' or '!' used to express this trait ?

A type decorator ? A decorator ? Operator ? Something else ?

  1. What shall I call the type created when using this character ?

Is it a new type ? Is it a decorated type ? A type variation ?

The swift compiler, seems to consider them as new types, However my question is not implementation or language dependent and therefor I tagged it as language agnostic.

Edit: I'm looking for a language agnostic name. I understand with pranjalsatija's comment optionals are defined as compound type. However, this is a language implementation detail. I could rephrase my questions as:

What do you call a character with a special meaning when used it a type definition, and how to call the derived type.

This term should probably apply to capital casing constants in ruby as the concept is similar.

like image 782
Antzi Avatar asked Jul 06 '15 12:07

Antzi


People also ask

What are the optionals in Swift?

An optional acts as a container for a value of a particular type. The container holds a value or it doesn't. Type safety is a cornerstone of the Swift language. As a result, variables and constants need to be initialized before they can be accessed.

Why do we have optionals in Swift?

Swift's optionals are one of its most powerful features, while also being one of the most confusing. Their core job is simple: they allow us to represent the absence of some data – a string that isn't just empty, but literally doesn't exist.

How are optionals implemented in Swift?

Optionals are such an important part of Swift that a special syntax has been baked into the language to make them easy to use. We can declare an optional wrapping any Swift type by simply postfixing the name of the type to be wrapped with a question mark ( ? ).

What is an optional String in Swift?

However there is another data type in Swift called Optional, whose default value is a null value ( nil ). You can use optional when you want a variable or constant contain no value in it. An optional type may contain a value or absent a value (a null value). Non technically, you can think optional as a shoe box.


1 Answers

? on the end of the type isn’t a decorator or operator. It’s hardcoded syntactic sugar in Swift that allows you to shorten Optional<Thing> to Thing?.

The ? doesn’t really have a name (at least I’ve never heard anyone on the Swift team use one), in the language reference it’s just described as “the postfix ?”. The language grammar doesn’t put it in a syntactic category.

Similarly, [Thing] is shorthand for Array<Thing>, but there isn’t a name for the square brackets in this context.

Describing Option<Int> as “derived from” Int would be to misuse the term “derived”. You can if you want describe it as “Optional specialized for Int”.

In fact you may be looking for the language-agnostic term for how Swift allows you to build types (like Optional<T> or Array<T>) that apply to any kind of type T without having to care what T actually is. In which case the term would probably be generics.

! is a little different. When applied to a type name as in Thing!, it’s shorthand for ImplicitlyUnwrappedOptional<Thing>, in the same manner as ?.

! when applied to a variable of type Thing? is equivalent to a postfix operator that tests the optional and if it is nil, terminates your program, something like this:

postfix operator !<T>(value: T?) -> T {
    if let unwrapped = value {
        return unwrapped
    }
    else {
       fatalError("unexpectedly found nil while unwrapping an Optional value")
    }
}

so in this context, ! can be described as an operator. But not in the first context.

For the terminology a given language uses to describe optionals, see the Option type wikipedia page.

like image 189
Airspeed Velocity Avatar answered Sep 20 '22 12:09

Airspeed Velocity