0

No, the above does NOT answer this question! I read that post twice before posting this. Folks seem to think I am asking what an optional is. I do know what an optional is, that's why I am using it as an example to clarify wrapper terminology. Let me simplify yet further:

//-----------------------------------------------------------

In the case of an optional:

Is the optional itself a wrapper? When you declare a variable to be an optional, are you wrapping it? I'll be satisfied with answers to these queries.

//-----------------------------------------------------------

Answers regarding unwrapping here [on SO] are [more] of the "how" and "why" type. I'd like to know exactly "what"! It's probably best if I pose this as a hypothetical that can be confirmed or corrected.

Using an optional as an example only, for the sake of elucidating exactly what a wrapper is.

Is this correct:

The optional, in this example, is the wrapper. This is equivalent to a "package" that could contain a value or nil. When you unwrap the optional, you are accessing the value within. To continue the metaphor, "unwwrapping is opening the package to reveal the contents within".

And is this also correct:

The wrapper is necessary because the underlying contents might be nil.

What is an example of another wrapper and what does it wrap?

edit: So how about one of you [luk2302, Eric D., Daniel Storm, Undo, Himanshu] tell me where this is answered?

bpedit
  • 1,176
  • 8
  • 22
  • Do you want to know the concept of optionals or you want to know the details of terms "package" and "wrappers" only in this context? – Abhishek Dec 22 '15 at 18:06
  • Is the optional itself a wrapper? When you declare a variable to be an optional, are you wrapping it? That's all I need. – bpedit Dec 23 '15 at 05:02

1 Answers1

0

Another example of wrapping is the enum that wraps either a result or an error. E.g. this:

enum Result<T, E> {
    case Success(T)
    case Error(E)
}
0x416e746f6e
  • 9,872
  • 5
  • 40
  • 68
  • Anton, thanks. I'm little more than halfway through the Swift Programming Guide, not enough to make use of your example. If you could please respond to my two simple rephrased questions between the "comment lines" in my post, I'd be grateful. Thanks again. – bpedit Dec 23 '15 at 20:00
  • Frankly, I do not exactly understand the question `Is the optional itself a wrapper?`. First of all, what does term "wrapper" mean for you? It seems that you use this term in a different way than I understand it. – 0x416e746f6e Dec 23 '15 at 20:13
  • Well, that's what I'm unclear about! An optional can contain a value or nil. Is the optional a wrapper for, or containing, a value within? The guide talks extensively about wrapping and unwrapping but never really specifies what a wrapper is by example. Is your enum above the wrapper for the two cases? – bpedit Dec 23 '15 at 20:36
  • Do you mean to ask a question `Why optionals in Swift are considered to be "wrappers" in a sense that operation is called "unwrapping the optional"?` – 0x416e746f6e Dec 23 '15 at 21:23
  • Nope. Even easier. Are optionals wrappers? And: when you declare a variable as an optional, have you just wrapped it? I'm starting to feel like the answer to both is yes, just want to be sure. – bpedit Dec 23 '15 at 23:45
  • `Are optionals wrappers?` : In my understanding of "wrappers", not exactly. Wrapping is when you take something, like class A, and encapsulate it into another thing ("wrap") so that from outside it will conform to interface B. The key here is "take something", which in case of optionals is not applicable because the whole idea is that an optional can also hold "nothing". So in this sense it's "no". In the other sense, an optional takes something that is concrete, read always initialised, and "wraps" it into something that might be or might be not initialised. So, it's "yes", then. – 0x416e746f6e Dec 24 '15 at 06:51
  • `When you declare a variable as an optional, have you just wrapped it?` : No. You just declared the variable that is an optional. The closest to "wrapping" in such scenario that you can get is: `let foo = ClassFoo(); /* <- concrete instance here */ let bar: ClassFoo? = foo; /* bar now holds a "wrapped" foo */` But this example is very artificial. Why would you need it in real life, I have no idea. – 0x416e746f6e Dec 24 '15 at 06:55