What Does the Word "Literal" Mean

What does the word literal mean?

A literal is "any notation for representing a value within source code" (wikipedia)

(Contrast this with identifiers, which refer to a value in memory.)

Examples:

  • "hey" (a string)
  • false (a boolean)
  • 3.14 (a real number)
  • [1,2,3] (a list of numbers)
  • (x) => x*x (a function)
  • /^1?$|^(11+?)\1+$/ (a regexp)

Some things that are not literals:

  • std::cout (an identifier)
  • foo = 0; (a statement)
  • 1+2 (an expression)

What is the meaning of literal in the phrase object literal notation?

About "complimenting JSON": He specified it.

The "literal" part: Googling "object literal" provides two top resources: MDN and Wikipedia. To quote the latter:

In computer science, a literal is a notation for representing a fixed value in source code. Almost all programming languages have notations for atomic values such as integers, floating-point numbers, and strings, and usually for booleans and characters; some also have notations for elements of enumerated types and compound values such as arrays, records, and objects.

Basically, all syntax constructs whose use lead to a defined type can be called a literal. (E.g., a string literal, "abc".) It's a technical term that denotes, that "literally" writing something in this or that way leads to a certainly typed variable exclusively (in contrast to constructs, that look like something else, like array() in PHP).

What is a literal in C++?

A literal is some data that's presented directly in the code, rather than indirectly through a variable or function call.

Here are some examples, one per line:

42
128
3.1415
'a'
"hello world"

The data constituting a literal cannot be modified by a program, but it may be copied into a variable for further use:

int a = 42;  // creates variable `a` with the same value as the literal `42`

This concept is by no means unique to C++.

The term "literal" comes from the fact that you've written data literally into your program, i.e. exactly as written, not "hidden" behind a variable name.

What means image literal or rather the word literal?

A literal is a value that is directly represented in the code.

Examples:

let i = 3                  // 3 is an Integer Literal
let arr = ["a", "b", "c"] // ["a", "b", "c"] is an Array Literal

Swift has other literals such as Color Literals and Image Literals. Those are
special compiler directives which directly encode the color or reference to an image.

This allows the Xcode editor to do special things such as show a color literal directly in the code as a color splotch instead of just showing confusing/meaningless floating point numbers. And it allows you to select the color by double clicking on the color splotch to bring up the standard color picker.

// Paste this into Xcode.  The color will be shown directly
let color = #colorLiteral(red: 0.3411764801, green: 0.6235294342, blue: 0.1686274558, alpha: 1)

colorLiteral in Playground

What is the difference between literals and non-literals, other than the fact that non-literals go into the heap?

Clearly literals can be mutable in Rust

First, you need to understand what a literal is. Literals are never mutable because they are literally written in the source code and compiled into the final binary. Your program does not change your source code!

An example showing that you cannot modify a literal:

fn main() {
1 += 2;
}
error[E0067]: invalid left-hand side expression
--> src/main.rs:2:5
|
2 | 1 += 2;
| ^ invalid expression for left-hand side

On the other hand, a literal can be copied into a variable and then the variable can be changed, but we still are not mutating the literal 1:

fn main() {
let mut a = 1;
a += 2;
}

To be honest, I don't know what I would call a "non-literal". A literal is a specific type of expression, but there are other types of things in a program besides expressions. It's kind of like saying "cats" and "non-cats" — does that second group include dogs, mushrooms, sand, and/or emotions?


the fact that literals go into the stack, while non-literals go into the heap

Those two qualities aren't really directly related. It's pretty easy to have non-literals on the stack:

fn main() {
let a = 1;
let b = 2;
let c = a + b;
}

All three variables are on the stack, but there is no literal 3 anywhere in the source code.

Right now, Rust doesn't allow for a literal value to have a heap-allocation, but that's a language-specific thing that might change over time. Other languages probably allow it.

In fact, you have to go out of your way in Rust to put something on the heap. Types like Box, Vec, and String all call functions to allocate space on the heap. The only way for your code to use heap memory is if you use these types, other types that use them, or types which allocate heap memory in some other way.


What is the reason we cannot use String literal data-type

There is no String literal — none. The source code "foo" creates a literal of type &'static str. These are drastically different types. Specifically, the Rust language can work in environments where there is no heap; no literal could assume that it's possible to allocate memory.

have to specifically use String::from()

String::from converts from &str to a String; they are two different types and a conversion must be performed.

Clearly, as per the example, in my code, both can be mutable

No, they cannot. It is impossible to start with let mut foo = "a" and modify that "a" to become anything else. You can change what that foo points to:

let mut foo = "a";
                foo
+-----------+
|
|
+---v---+
| |
| "a" |
| |
+-------+
foo = "b";
                  foo
+----------+
|
|
+-------+ +---v---+
| | | |
| "a" | | "b" |
| | | |
+-------+ +-------+

Neither "a" nor "b" ever change, but what foo points to does.

This isn't specific to Rust. Java and C# strings are also immutable, for example, but you can reassign a variable to point to a different immutable string.


See also:

  • What does the word "literal" mean?
  • What's the difference in `mut` before a variable name and after the `:`?
  • What are the differences between Rust's `String` and `str`?

What does a string literal mean in Coffeescript

name = "George"

"My Name: #{name}" ====> "My Name: George"
'My Name: #{name}' ====> "My Name: #{name}"

Literal in this case means that it is literally what you wrote in the string

For this reason (and a few others) I like to use a convention of double quotes when the string is natural language that is meaningful to an end user (for example an error message). And single quotes for symbols that are meaningful to the program (like property names, flags, module names, etc).

And no, that's not your fault, the word 'literal' has like 20 definitions in cs.

What is a function literal in Scala?

A function literal is an alternate syntax for defining a function. It's useful for when you want to pass a function as an argument to a method (especially a higher-order one like a fold or a filter operation) but you don't want to define a separate function. Function literals are anonymous -- they don't have a name by default, but you can give them a name by binding them to a variable. A function literal is defined like so:

(a:Int, b:Int) => a + b

You can bind them to variables:

val add = (a:Int, b:Int) => a + b
add(1, 2) // Result is 3

Like I said before, function literals are useful for passing as arguments to higher-order functions. They're also useful for defining one-liners or helper functions nested within other functions.

A Tour of Scala gives a pretty good reference for function literals (they call them anonymous functions).

Exact meaning of Function literal in JavaScript

A function literal is just an expression that defines an unnamed function.

The syntax for a function literal is much like that of the function statement, except that it is used as an expression rather than as a statement and no function name is required.

So When you give the method name then it can't be a method literal.

What does it mean to say that null is a literal in JavaScript?

I am no expert but I think you already have the answer: you said 3 and 4 are "integer-literal". Quoting wikipedia:

In computer science, a literal is a notation for representing a fixed value in source code.

So 3 and 4 are both literals, as well as 34 or "hello". null is a literal in the same way, but instead of representing a number or a string, is has a meaning of nothingness.

EDIT: As I said, I am no expert and it seems I am not quite right about literals. T.J. Crowder explains it much better than me, see his answer



Related Topics



Leave a reply



Submit