Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Swift 2.0 Enumerations and Optionals Introduction to Optionals Working with Optional Types

Brendan Whiting
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Brendan Whiting
Front End Web Development Techdegree Graduate 84,738 Points

Why do they use the word "some" for optionals?

enum Optional<T> {
    case Some(T)
    case None
}

Why "some"? I understand how optionals work, I'm trying to understand why they chose that word.

1 Answer

Michael Reining
Michael Reining
10,101 Points

Hi Brendan,

An Optional is a type all of its own. It is an enum with two possible values.

enum Optional<T> {
    case Some(T)
    case None
}

None and Some(T), where T is an associated value of the actual type.

An optional doesn't mean "this is an Int, which is optional". It says, "this is an Optional, which may or may not hold some Int."

If you extract that out to generics it means. "This is an Optional, which may or may not hold some Type."

Why go with the label "Some?" My guess would be that it is short for "something". It implies the opposite of not being nil. There is something there. Could they have picked another word? Sure but I think it is actually quite fitting given what optionals are.

If you think of optionals as their own type, then it also becomes very clear why optionals have to be unwrapped.

var possibleInt: Int?

possibleInt = 3
if let actualInt = possibleInt {
    print(actualInt)
}

I hope that helps a little.

Mike

PS: Thanks to the awesome resources on Team Treehouse, I just launched my first app. :-)

Code! Learn how to program with Swift