Grygori
Grygori

Reputation:

What can you NOT use an identifier for?

I'm trying to understand what identifiers represent and what they don't represent.

As I understand it, an identifier is a name for a method, a constant, a variable, a class, a package/module. It covers a lot. But what can you not use it for?

Upvotes: 0

Views: 955

Answers (8)

Ben Voigt
Ben Voigt

Reputation: 283793

In most languages, you can't use an identifier for infix arithmetic operations.

For example, plus is an identifier and you can make a function named plus. But write you can write a = b + c;, there's no way to define an operator named plus to make a = b plus c; work because the language grammar simply does not allow an identifier there.

Upvotes: 3

Nosredna
Nosredna

Reputation: 86306

Sort of a left-field thought, but JSON has all those quotations in it to eliminate the danger of a JavaScript keyword messing up the parsing.

Upvotes: 0

cjs
cjs

Reputation: 27271

An identifier allows you to assign a name to some data, so that you can reference it later. That is the limit of what identifiers do; you cannot "use" it for anything other than a reference to some data.

That said, there are a lot of implications that come from this, some subtle. For example, in most languages functions are, to some degree or another, considered to be data, and so a function name is an identifier. In languages where functions are values, but not "first-class" values, you can't use an identifier for a function in an place you could use an identifier for something else. In some languages, there will even be separate namespaces for functions and other data, and so what is textually the same identifier might refer to two different things, and they would be distinguished by the context in which they are used.

An example of what you usually (i.e., in most languages) cannot use an identifier for is as a reference to a language keyword. For example, this sort of thing generally can't be done:

let during = while;
during (true) { print("Hello, world."); }

Upvotes: 2

Pete Kirkham
Pete Kirkham

Reputation: 49331

Making soup out them is rather foul.

In languages such as Lisp, an identifier exists in its own right as an symbol, whereas in languages which are not introspective identifiers don't exist in the runtime.

You write a literal identifier/symbol by putting a single quote in front of it:

[1]> 'a
A

You can create a variable and assign a symbol literal to it:

[2]> (setf a 'Hello)
HELLO
[3]> a
HELLO
[4]> (print a)

HELLO 
HELLO

You can set two variables to the same symbol

[10]> (setf b a)
HELLO
[11]> b
HELLO
[12]> a
HELLO
[13]> (eq b a)
T
[14]> (eq b 'Hello)
T

Note that the values bound to b and a are the same, and the value is the literal symbol 'Hello

You can bind a function to the symbol

[15]> (defun hello () (print 'hello))
HELLO

and call it:

[16]> (hello)

HELLO 
HELLO

In common lisp, the variable binding and the function binding are distinct

[19]> (setf hello 'goodbye)
GOODBYE
[20]> hello
GOODBYE
[21]> (hello)

HELLO 
HELLO

but in Scheme or JavaScript the bindings are in the same namespace.

There are many other things you can do with identifiers, if they are reified as symbols. I suspect that someone more knowledgable than me in Lisp will be able to demonstrate any of the things that you 'can't do with identifiers' exist.

But even Lisp can not make identifier soup.

Upvotes: 0

Rorick
Rorick

Reputation: 8953

Say, in Java your cannot write something like:

Object myIf = if;
myIf (a == b) {
    System.out.println("True!");
}  

So, you cannot name some code statement, giving it an alias. While in REBOL it is perfectly possible:

myIf: if
myIf a = b [print "True!"]

What can and what can't be named depends on language, as you see.

Upvotes: 1

wiseguy
wiseguy

Reputation: 149

You could say it's used for everything that you'll want to refer to multiple times, or maybe even once (but use it to clarify the referent's purpose).

What can/can't be named differs per language, it's often quite intuitive, IMHO.

An "Anonymous" entity is something which is not named, although referred to somehow.

#!/usr/bin/perl
$subroutine = sub { return "Anonymous subroutine returning this text"; }

In Perl-speak, this is anonymous - the subroutine is not named, but it is referred to by the reference variable $subroutine.

PS: In Perl, the subroutine would be named like this:

sub NAME_HERE {
    # some code...
}

Upvotes: 1

Ender
Ender

Reputation: 213

as its name implifies, an identifier is used to identify something. so for everything that can be identified uniquely, you can use an identifier. But for example a literal (e.g. string literal) is not unique so you can't use an identifier for it. However you can create a variable and assign a string literal to it.

Upvotes: 0

Brian
Brian

Reputation: 118895

Every language differs in terms of what entities/abstractions can or cannot be named and reused in that language.

Upvotes: 3

Related Questions