Hacker News new | past | comments | ask | show | jobs | submit login

This is off topic. Is there a language that automatically generates code when the missing functions are referenced? It would be good for top-down style and test-driven style programming.

E.g. I start with a high level flow of a program.

  int main() {
    outputData( processData( getData() ) );
  }
When compiled, the compiler realizes all the called functions are missing and generates provisional stub code for them, with sensible default for parameters and types.

  int @getData() {
     return 0;
  }

  int @processData(int param1) {
  }

  void @outputData(int param1) {
  }
The program can compile and run. I then go ahead to fill out the content of the functions. The provisional functions are marked as such (the @ marker) where the compiler can run in a validation mode to flag all functions not implemented yet. When a function is done, the provisional marker can be removed.

As more function calls are added, each compilation generates more provisional functions, which can be filled out. Also as the completed function's parameters and types become concrete, the compiler can update the provisional caller or callee functions.

This kind of help from a compiler can really make test-driven development easy. I can write the tests first that make calls to yet-to-exist functions and the compiler generate the provisional version for me. And the tests run right the way.




In statically typed languages, that's usually avoided, because a typo in a function name can escalate to subtle bugs.

Also, working with partial information complicates the type inference; in your example, at best, it could infer () -> `a for getData, `a -> `b for processData, and `b -> () for outputData.

In some languages (such as Smalltalk, Lua, and Ruby), there is an explicit "message not understood" hook that is called when a nonexistent function is referenced, and you can define stub behavior there.


Good point about the function name typo in static type language. Provisional and complete functions are supposed to be different. The compiler can have a validation mode to flag all provisional functions, which would catch the wrong function name intended for a complete function.

The "message not understood" hook in dynamic languages is good for runtime handling of missing functions, but I actually want the compiler to generate the source code for the provisional functions, to make exploratory development easier.


IIRC, Haskell has some explicit marker for adding stub functions ("doThatStuff x y z = undefined" or something like that). You still write the names for the function and any arguments, though.

I tend to write code bottom-up rather than top-down, testing it in a REPL and/or with tests, but have Emacs functions to generate boilerplate for languages that need it.


If you use an IDE like eclipse and a method isn't defined, one of the auto-correct options will be to create a placeholder method stub for it, and it will flag it with a //TODO stub to keep track of autogenerated ones. Not quite as automated, but it's pretty convenient.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: