I am new to Scala sequels and relatively new to Scala in general.
I tried playing with a sequel to Scala and wrote the following code:
case class MyException(msg:String) extends Exception def go:Int = reset { println("enter your input") val my_check = //try { val user_input = readLine() if (!user_input.matches("\\w+")) { throw new MyException("illegal string: " + user_input) } shift { k: (Boolean => Int) => { if (user_input == "true") { k(true) } else if (user_input == "false") { k(false) } else { // don't even continue 0 } } } } // catch { // case MyException(msg) => false // } if (my_check) { println("TRUE") 1 } else { println("FALSE") -1 } } println(go)
The code worked as expected: when the user enters not an alphanumeric string, but MyException is thrown, when the user enters "true", the code continues with my_check = true , when the user enters "false", the code continues with my_check = false , and when the user enters an alphanumeric string that is not true or false, the go function exits from 0.
Then I tried to wrap part of the code in a try-catch block (where there are comments), and the compilation failed with
error: found cps expression in non-cps position
val my_check = try
I understand that there is a problem with โinjectingโ the exception into the continuation, but why can't I just put the shifted call inside the try-catch block?
I need this in the structure that I plan, in which the programmer will not know that his code is used in the form of a continuation (he will name some function that he will consider "normal", but will actually perform a shift ).
Obviously, I need it so that it can call the function inside the try-catch block, even if the shifted call itself will not throw an exception.
Can this problem be solved with a ControlContext ? Did he help if I add some โtypingโ rules for values โโ(possibly with @cps [..])?
Iโve already thought about an alternative to using Actors, so you wonโt get any credit for this :)
Thanks,
(PS I am using Scala 2.9.2 and obviously using -P: continue: enable flag)