Source code is an object too
What is the shortest example code which shows great flaw in Ruby comparing to Java, for example? It is a literal “string”. That’s it. Every time interpreter hits the literal string, it creates a brand new object. The only useful thing is that it is already mutable, so you don’t have to copy it in rare cases when you want to modify it. But in reality you end up with such kind of code:
# Yeah, these constants look kind of tacky. Inside of tight loops,
# though, using them makes a small but measurable difference, and those
# small differences add up....
C_asterisk = '*'.freeze
C_empty = ''.freeze
C_header_close = 'HTTP/1.1 200 OK\r\nConnection: close\r\n'.freeze
C_header_keepalive = 'HTTP/1.1 200 OK\r\n'.freeze
C_slash = '/'.freeze
C_slashindex_html = '/index.html'.freeze
C1_0 = '1.0'.freeze
C1_1 = '1.1'.freeze
C_304 = "HTTP/1.1 304 Not Modified\r\n".freeze
Caos = 'application/octet-stream'.freeze
Cat = 'at'.freeze
Ccache_directory = 'cache_directory'.freeze
Ccache_extensions = 'cache_extensions'.freeze
...
The first thing “Better Language” should implement is source code being a first-class object with all the literals instantiated as immutable objects and referenced by this object. This helps to resolve a lot of issues with floats, strings and regexps.
However, the interesting things remain:
1. Complex literals should be recognized: like JSON (Arrays and Hashes) full of other literals should also be an immutable object attached to the source code object.
2. Literals which include variable references should turn into new objects creation on each method call, but only in a case when the variables are not already immutably binded to some values. This brings us to a whole world of functional programming from Lisp to Haskell.
