Search code examples
compiler-constructionterminologysoftware-designlanguage-design

Terminology for this type of programming behavior


I have designed and implemented a programming language.

One of the features of this language is masquerading a function (in the sample code, I call it leftBind) as a variable such that any following dot notation is passed as an identifier.

Edit: This is similar as well to an accessor method for a class, except no class is being used here. Would the same term “getter” or “accessor” apply here?

The purpose of this language is to provide a fully functional language but jailed environment, to allow for complex configurations of a project, but protecting the project from unauthorized behavior as a whole.

An example of this:

/*leftbind aliases the identifier “sql” to the  function “quote”
It is called leftBind, since it’s binding the left side of the parse tree to a function*/ 
leftBind(“sql”, “quote”); 
q=“select * from foo where user=$sql.user”; // equivalent to quote(user)

This becomes very handy for more complex tasks or when you want to inline commonly used functions into strings without extra lines of code littering your program.

What is the terminology for this type of behavior?


Solution

  • If I understand you correctly, this could be a combination of string interpolation and dynamic member lookup:

    String Interpolation

    E.g. in Swift you can embed expressions in code:

    "This is \(getAdjective(), uppercasing: true) !"
    

    is equivalent to

    "This is ".append(getAdjective(), uppercasing: true).append(" !")
    

    By defining an append method on String that takes additional parameters (in this case the uppercasing: boolean) or accepts a different first argument, you can thus let people extend this placeholder syntax.

    You could also implement a method that returns a string so you can use that method inside the \(...).

    The important part here is that there is a syntax for embedding expressions in strings, the details of how Swift implements it (append method on String is just how Swift does it, not how string interpolation has to be.

    Dynamic member lookup

    In Swift, a class can define a function that is called when a member is referenced that doesn't exist as a property or function:

    @dynamicMemberLookup class MyClass {
        var dictionary: [String: String]
        subscript(dynamicMember: String) -> String {
            return dictionary[dynamicMember] ?? ""
        }
    }
    

    That lets you make up your own variables dynamically. When you write

    let foo = MyClass()
    let myString = foo.bar
    

    the compiler basically just generates code for

    let foo = MyClass()
    let myString = foo.subscript(dynamicMember: "bar")
    

    is that about what you're thinking of?