So I was thinking about this extern(language) thing, the obvious ones are supported, but it would be really nice to be able to implement custom conventions for other languages/scripting languages.<div><br></div><div>For instance, I'm thinking about Android, I have JNI binding code everywhere, it's really ugly.</div>
<div>I'd love to be able to declare:</div><div>  extern(Java) int someJavaFunc(int x, float y)</div><div><br></div><div>And then use my function like any regular function, with the 'extern(Java)' bit handling the JNI business behind the scenes.</div>
<div>I also regularly interact with javascript, lua, C#/mono, and these could all be implemented the same way.</div><div><br></div><div>I'm imaging some mechanism to declare a calling convention (which would be resolved within the extern(...) statement), and define it with a template, something like:</div>
<div><div><br></div><div>callconv Java</div><div>{</div><div>  R call(T...)</div><div>  {</div><div>    // process tuple of args, make the call, return something?</div><div>  }</div><div><br></div><div>  R thisCall(Class, T...)</div>
<div>  {</div><div>    // also need a way to implementing methods... this might be enough.</div><div>  }</div><div>}</div><br class="Apple-interchange-newline"></div><div>Some fancy code in there could conceivably call into any foreign language, and this would be great!</div>
<div>Now when I: import java.jni;</div><div>I have the jni interface, but I also have access to extern(Java), and that's awesome! :)</div><div><br></div><div>The main benefit over using a template, for instance: jniCall!"functionName"(args...), would be the function name is not a string, or require custom code construct (facilitating later re-factoring or delegation to script without changing masses of existing code, something I have done often), and if it was seen by the language as a regular function call, you can mark it with all the usual stuff, const/pure/etc, and apply the expected set of optimisations to the call.</div>
<div><br></div><div>I'm sure this has been discussed before... so go on, tear it apart :)</div>