Reputation: 13414
In Swift the elements we manipulates all have types.
When we use theses types, we can add a '!', '?' or nothing to express their nullability.
A type decorator ? A decorator ? Operator ? Something else ?
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.
Upvotes: 6
Views: 672
Reputation: 40955
?
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.
Upvotes: 7
Reputation: 1789
The type created using '?' is an Optional and '!' is an Implicitly Unwrapped Optional
I think the answer here may help you out, they refer to both as decorations
Here's a larger explanation about exclamation marks
And here's one for question marks
Upvotes: 0
Reputation: 14
Optionals in Swift technically are completely different types, not variations of the same type. However, to a developer, they seem to be variations, so we'll treat them as such. The ? and the ! don't really have a set, specified name just yet, at least not that I know of. In a sense, you shouldn't be calling them type decorators, because optionals are really new types on their own. So to answer your question, the ? and the ! are parts of a type's name, more than anything else. And the new type created when using a ? or an ! is just that. A brand new type.
Upvotes: 0