tags:

views:

214

answers:

7

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?

+2  A: 

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

Brian
A: 

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.

Ender
Why string literal is not unique?
because you can sayvar a = "Hello";var b = "Hello";
Ender
ah yes. thank you.
It's a very common practice to use identifiers instead of literals in code.
Pete Kirkham
you can use identifiers instead of literals but you can not use literals instead of identifiers thus literals are not identifiers
Ender
So that's something you can't use literals for, not something you can't use and identifier for.
Pete Kirkham
Hm? It's perfectly possible to use a literal in place of an identifier: instead of "let x = 3 in x + 1", just "3 + 1". And string literals are unique in some language implementations where strings are immutable; The address of '"foo"' will be the same as the address of '"foo"' because they automatically detect that both are the same string.
Curt Sampson
A: 

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...
}
wiseguy
A: 

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.

Rorick
A: 

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.

Pete Kirkham
A: 

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."); }
Curt Sampson
A: 

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.

Nosredna