3 ms·
This reminds me of a very cool property of the Lua language that allows for very neat "syntax extensions", and reminds of functional languages too: If you have
by kroltan 6y ago
This reminds me of a very cool property of the Lua language that allows for very neat "syntax extensions", and reminds of functional languages too:
If you have two expressions adjacent to eachother, it evaluates to calling the left-hand side with the right-hand side as a parameter.
At its simplest:
function foo(text)
print("Foo:", text)
end
foo "bar"
But you could implement a JSX-lite in it:
function Div(props)
return function(children)
return ViewLibrary.createElement("div", props or {}, children or {})
end
end
Then, you use it like this:
Div{id="outer"} {
Div{id="innerA"} (),
Div{id="innerB"} {
Div()()
}
}
No compiler extensions needed! And with a bit of library sugar, you could do away with the empty parameter lists too (calling any unresolved functions with empty parameters), but this would lengthen the demo a bit so I omitted it.
- kroltan 6y agoIn fact, since objects and arrays are one and the same in Lua, one can even do away with the nested calls, which also simplifies things visually: Div { id = "outer", Div {id = "innerA"}, Div { id = "innerB", Div {} } } This works because when you omit the key (id=) of a table item in Lua, it is assumed to have a incrementing numeric key.
- goatlover 6y agoThat's very neat. In the pure prototyping language IO, since the syntax was defined as messages to objects, you could define your own syntax to support xml or html directly in IO. I wonder if Netscape hadn't collaborated with Sun if JS would have had a more pure implementation of prototypes, and how those might have been leveraged more minus all the confusion from mixing in Java-like syntax on top of JS's object model.
- kroltan 6y agoI don't have the sources right now, but I once watched (not attended!) a presentation that talked about how JS was initially intended to be a language more like Lisp or Smalltalk, but was changed into an OO language to appease the heavy marketing at the time.
- kazinator 6y agoThe duo of Lisp and Smalltalk basically define what OO means. Javascript simulates some of the aspects of OOP in an ad hoc way by turning every object into a hash table so that properties can be tacked onto it. It's what you implement if it's Wednesday, and the boss asks for a some sort of working OOP system by Friday.
- kroltan 6y agoYou're right I was half asleep when I wrote the previous comment, I am very sorry. I meant a C(Java?)-like language. Which I am aware has nothing to do with OO, just syntax style.