scala-subscript / subscript

9 stars 2 forks source link

Catch variables in subscript code fragments cause compile error #25

Open AndreVanDelft opened 8 years ago

AndreVanDelft commented 8 years ago
script test = {* try     {doSomething}
                 catch   {case t:Throwable => t.printStackTrace()}
                 finally {doSomethingElse}
              *}

Yields:

[error] :1: missing parameter type for expanded function [error] The argument types of an anonymous function must be fully known. (SLS 8.5) [error] Expected type was: ? [error] {case t:Throwable => subscript.DSL._maybeVarCall("t.printStackTrace()")} [error] ^ [error] one error found

This issue may and should be solved when we move the preprocessor back to the compiler.

Temporary workaround:

catch   {case t:Throwable => t.printStackTrace()}: PartialFunction[Throwable, Unit]

Earlier reported here. Anatolii wrote:

Closed from this.

Due to the fact that everything in code fragments end up in various macros in small pieces, catch {case t:Throwable => t.printStackTrace()} becomes catch subscript.DSL._maybeVarCall("{case t:Throwable => subscript.DSL._maybeVarCall(\"t.printStackTrace()\")}") and hence is processed as a separate PartialFunction, outside try's context. Hence we need to specify its type explicitly, so that it rewrites to catch subscript.DSL._maybeVarCall("{case t:Throwable => subscript.DSL._maybeVarCall(\"t.printStackTrace()\")}"): PartialFunction[Throwable, Unit]. (...) So far, macros wrap statements non-selectively, they don't have the context information and can't see they operate in try-catch. This is not the most elegant solution, of course, but I couldn't think of anything better using only macros and the parser.