Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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,703 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