3 ms·
What I often wonder is why, despite all kinds of languages experimenting with syntax, there isn't more innovation with function/method signatures. Seems to me,
by xg15 5y ago
What I often wonder is why, despite all kinds of languages experimenting with syntax, there isn't more innovation with function/method signatures.
Seems to me, a lot of "DSL"/"API design" work is really about passing some compile-time data structure to a function that is too complex to be represented as a simple list of parameters.
A lot of times, this is done by abusing language features or by setting up a runtime data structure that is then immediately unpacked again inside the function.
In the most extreme cases, you design a custom DSL, have the caller pass expressions in that DSL as a string or file handle to the function and then parse the string inside the function.
All of that causes a lot of runtime complexity and overhead for what is essentially static data.
So wouldn't it make more sense if you could define a DSL in a function signature and connect it with preprocessor/macro statements inside the function? This way, the compiler could parse the DSL during build and we could get rid of all the runtime overhead.
Example: A fictional function definition could look like this: (where # indicates a keyword destinied for the preprocessor)
public Response fetch(String url, Map headers, (#keyword method=GET) #or (#keyword method=POST, byte[] body)) {
// ... common code
#switch (method) {
#case GET:
// ... GET-specific code
#case POST:
// ... POST-specific code
}
}
A compiler would generate two separate methods from this definition. (To avoid C macro madness, you'd probably first generate an AST, then have the preprocessor modify that AST.)
A call site could look like this:
result = fetch("example.com/", {}, GET);
result = fetch("example.com/", {}, POST, data);
Which would call one of the two methods in a fashion analogous to method overloading.
- wizzwizz4 5y agoRust has this with its macro_rules! macros. It's not as good of a solution as it initially seems.
- xg15 5y agoInteresting to know. What were the issues?
- wizzwizz4 5y agoMostly the syntax is bad, I think. Also, exposing the AST like this means implementation details about the parser are now locked in.
- xg15 5y agoI can understand the syntax bit. For that feature it's really important the syntax is accessible. (I have no illusions how bad my syntax above was. I just threw it together to illustrate the point) Disagreed on the AST though. I think a lot of problems can be solved if a programming language has a well-defined publicly available grammar and if access to its AST is easy. As an example, a lot of innovation in modern IDEs comes from the IDE's ability to actually parse the code files and build object graphs. Knowing details about the AST is necessary for that.
- wizzwizz4 5y agoKnowing details about the grammar is necessary for that. Rust's macro_rules! (and, to a lesser extent, your syntax) exposes implementation details about the compiler as parts of the standard, so it's difficult to use (e.g.) a different architecture of parser.
- xg15 5y ago> and, to a lesser extent, your syntax That was a fantasy syntax I just made up. How can I expose implementation details of a compiler that doesn't exist?