2

Something that is bothering me is why the term 'literal' is used to refer to instances of classes like NSString and NSArray. I had only seen the term used in reference to NSString and being naive I thought it had something to do with it 'literally' being a string, that is between quotation markers. Sorry if that sounds pathetic, but that was how I had been thinking about it.

Then today I learned that certain instances of NSArray can also be referred to as literal instances, i.e. an instance of the class created using a 'literal syntax'.

jscs
  • 63,694
  • 13
  • 151
  • 195
cheznead
  • 2,589
  • 7
  • 29
  • 50
  • 3
    literal = hard-coded into the code at compilation time. E. g.: `@[ @"foo", @"bar" ]` is an array literal. Compare that with something you obtain dynamically, at run-time (e. g. the result of parsing some JSON, etc.) – The Paramagnetic Croissant Feb 04 '15 at 20:01

3 Answers3

2

A literal syntax or a literal is just an object that was created using a dedicated syntax built into the language instead of using the normal syntax for object creation (whatever that is).

Here I create a literal array:

NSArray* a = @[@"Hello", @"World"];

Which is, for all intents and purposes equivalent to this:

NSArray* a = [NSArray arrayWithObjects:@"Hello", @"World", nil];

The first is called a literal because the @[] syntax is built into the language for creating arrays, in the same way that the @"..." syntax is built in for creating NSStrings.

Linuxios
  • 34,849
  • 13
  • 91
  • 116
2

As @Linuxios notes, literal syntaxes are built into the language. They're broader than you think, though. A literal just means that an actual value is encoded in the source. So there are quite a few literal syntaxes in ObjC. For example:

  • 1 - int
  • 1.0 - double
  • 1.0f - float
  • "a" - C-string
  • @"a" - NSString
  • @[] - NSArray
  • ^{} - function

Yeah, blocks are just function literals. They are an anonymous value that is assignable to a symbol name (such as a variable or constant).

Generally speaking, literals can be stored in the text segment and be computed at compile time (rather than at run time). If I remember correctly, array literals are currently expanded into the equivalent code and evaluated at runtime, but @"..." string literals are encoded into the binary as static data (at least now they are; non-Apple versions of gcc used to encode an actual function call to construct static strings as I remember).

Rob Napier
  • 286,113
  • 34
  • 456
  • 610
  • Yes, `@[...]` is `arrayWithObjects:count:`, and this is the reason you can't use literals other than `NSString`s at the top level of a file. – jscs Feb 04 '15 at 20:53
  • Really? Blocks are function literals? I thought that they were fundamentally different at the language level due to C functions not being closures of any kind. – Linuxios Feb 04 '15 at 21:02
  • C functions are a kind of closure. They close over the current lexical scope that they are defined in. So a variable defined in the current file (but outside the function itself) will be closed over, just like in a block. Blocks are closely related to functions in C. I think what you mean is that function *pointers* are not closures of any kind, and I'd agree with that. – Rob Napier Feb 04 '15 at 21:19
  • @Linuxios: Blocks are [_implemented_ as a certain interesting kind of struct](http://stackoverflow.com/a/17819142/), but it's fair to say that they are indeed literal functions. – jscs Feb 04 '15 at 22:15
  • @JoshCaswell: Didn't realize that. It's kind of frightening and awesome that Objective-C is really just some macros and a library for C. – Linuxios Feb 04 '15 at 22:16
  • That's what makes it so much fun, @Linuxios! – jscs Feb 04 '15 at 22:18
  • @JoshCaswell: Agreed. It's like Ruby with segmentation faults. – Linuxios Feb 04 '15 at 22:19
  • @RobNapier: Thanks for such a detailed answer. Got it, save for the 'text segment' part! What do you mean by this? Text as opposed to number related data that go into compilation code? – cheznead Feb 04 '15 at 22:50
  • @JoshCaswell: Top level of the file? :/ – cheznead Feb 04 '15 at 22:55
  • You can't put method/function calls outside the context of a method/function body, @shinnyWhack. – jscs Feb 04 '15 at 22:56
  • 1
    @shinnyWhack "text segment" is where the code of the program goes in the executable. It's just an old term for that part of the file. https://en.wikipedia.org/wiki/Code_segment – Rob Napier Feb 04 '15 at 23:08
1

the term 'literal' is used to refer to instances of classes

It's not referring to the instance really; after the object is created, the way it was created doesn't matter:

NSArray * thisWasCreatedWithALiteral = @[@1, @2];
NSArray * butWhoCares = thisWasCreatedWithALiteral;

The "literal" part is just the special syntax @[@1, @2], and

it ha[s] something to do with it 'literally' being a string, that is between quotation markers.

is exactly right: this is a written-out representation of the array, as opposed to one created with a constructor method like arrayWithObjects:

jscs
  • 63,694
  • 13
  • 151
  • 195