9

I am trying to find the right language to describe the following concept. Maybe someone can help me out.

This is a general question about programming but I'll use Python and R for examples.

In Python, we can put something in a dictionary like this

myData = {}
myData["myField"] = 14

In R, for example, using the data.table package, we could write something like

data = data.table(x = c(1, 2, 3))
data[,myField: = x^2]

These do different things but compare the second line of each of them. In Python, the "myField" is a string. In the R data.table example, there is no string. The R example is kinda nice because it saves you typing but then it gives you trouble if want to write a program where myField is a variable. In Python that is trivial because you can just do

myData[myVariable] = 14

with myVariable being defined as another string. In R, you can do this too but have to use a different syntax which means you have to know two completely different syntactical ways of programming it.

My question: What is this called? I know it has something to do with scoping rules, (perhaps meta programming?), but can't figure out the right language for it. Anyone?

embert
  • 7,336
  • 10
  • 49
  • 78
Dave31415
  • 2,846
  • 4
  • 26
  • 34
  • 3
    You do realize, don't you, that the behavior you describe in R is specific to the data.table package, and the way it parses its arguments. More basic data structures like data frames and lists handle character indexing just fine. – joran Jan 27 '14 at 16:25
  • Yes, I know. I am just using it as an example. – Dave31415 Jan 27 '14 at 16:27
  • 3
    It's what you get when you combine lexical scoping with the principle that expressions are first class objects. The latter isn't true in Python. – Matthew Plourde Jan 27 '14 at 16:33
  • 1
    Note that in data.table you can do this: `myFieldName <- "myField"; data[, c(myFieldName) := x^2]` . – G. Grothendieck Jan 27 '14 at 16:58
  • 2
    Also in R its called nonstandard evaluation. See http://developer.r-project.org/nonstandard-eval.pdf – G. Grothendieck Jan 27 '14 at 17:02
  • 1
    A better R equivalent might be `myData <- list(); myData$myVariable <- 14". You might find http://adv-r.had.co.nz/Computing-on-the-language.html informative. – hadley Jan 27 '14 at 23:45

3 Answers3

1

I believe this programming language "feature" is known as "bare strings".

PHP also has (deprecated) support for this: Why is $foo[bar] wrong?

It's widely considered to be a pretty terrible idea because of the risk of overlap with variable or constant names, and should definitely be avoided in production code. However, JavaScript has an interesting twist on the idea that avoids those issues:

var obj = { key: "value" };

You can define the keys on inline objects without using quotation marks. This works because you can't do it the other way - the key is parsed as a string, never a variable name. I've found this to be a pretty useful tradeoff in programs where you only use predefined keys in dictionaries. The Python version, for reference, requires the quotation marks, but allows you to use a variable, as you demonstrated:

obj = { "key": "value", key_var: "value" }
robbles
  • 2,729
  • 1
  • 23
  • 30
0

I'm not sure if this is what you're asking, but, in your Python example, the "myField" in myData["myField"] is a string literal -- i.e., you're hard-coding in the key value. Similarly, if you defined a Python function

def myFun(a):
    # magic happens

and you invoked the function as myFun("goober"), you'd be passing in a string literal ("goober"), as opposed to defining a variable b = "goober" then passing in the variable as myFun(b).

tinybike
  • 562
  • 2
  • 13
0

Your question seems related to the definition/use of (unevaluated) symbols in Lisp (and related languages, and also Mathematica), c.f. the answers here

Community
  • 1
  • 1
Dave
  • 7,555
  • 8
  • 46
  • 88