- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Re: Re: Why NonLocalReturnException during java Method.invoke() doesn't result in a InvocationTargetException?
Sat, 2009-07-18, 00:18
I don't know if it's intentional, but the fact is that without the val you are passing a return statement that will be executed inside a method. If it's a val there's no guarantee.
If it's not intentional then way does it return from subcall and not from Function1.apply?
-------------------------------------
Trond Olsen wrote:
My problem isn't related to reflection, but in using a return statement in a
function literal that's given directly to a method call. If I define this
function literal as a variable then the compiler will give an error. Should
I report this as a missing error case for the compiler?
Problem example:
class A {
def call() {
println("Begin call")
subcall((x: Int) => {
println("block call")
return x
})
println("End call")
}
def subcall(block: (Int) => Int): Int = {
println("begin subcall")
var n = 0
for (i <- 0 until 10)
n += block(i)
println("end subcall")
return n
}
}
val a = new A
a.call()
Running this produces:
Begin call
begin subcall
block call
If I assign the above function literal to a variable then the compiler gives
an error:
val n = (x: Int) => { return x }
(fragment of InvokeException.scala):2: error: return outside method
definition
val n = (x: Int) => { return x }
^
On Fri, Jul 17, 2009 at 6:17 PM, Trond Olsen wrote:
> Does anyone know why this particular exception doesn't result in a
> InvocationTargetException with java method invokation? I can't catch it
> either if I add an exception wrapper around the invoke() call either.
>
Sat, 2009-07-18, 15:07
#2
Re: Re: Why NonLocalReturnException during java Method.invoke(
By not using return. It's rarely necessary.
2009/7/18 Trond Olsen :
> I think I get it now! :) The return will return from method scope it was
> defined and not from within it's own block scope during it's execution. If I
> drop the return and just give the object it'll work fine. How do you avoid
> getting fooled by this in general and how common is this pitfall?
>
> On Sat, Jul 18, 2009 at 1:17 AM, Naftoli Gugenheim
> wrote:
>>
>> I don't know if it's intentional, but the fact is that without the val you
>> are passing a return statement that will be executed inside a method. If
>> it's a val there's no guarantee.
>> If it's not intentional then way does it return from subcall and not from
>> Function1.apply?
>>
>> -------------------------------------
>> Trond Olsen wrote:
>>
>> My problem isn't related to reflection, but in using a return statement in
>> a
>> function literal that's given directly to a method call. If I define this
>> function literal as a variable then the compiler will give an error.
>> Should
>> I report this as a missing error case for the compiler?
>>
>> Problem example:
>>
>> class A {
>> def call() {
>> println("Begin call")
>> subcall((x: Int) => {
>> println("block call")
>> return x
>> })
>> println("End call")
>> }
>>
>> def subcall(block: (Int) => Int): Int = {
>> println("begin subcall")
>> var n = 0
>> for (i <- 0 until 10)
>> n += block(i)
>> println("end subcall")
>> return n
>> }
>> }
>>
>> val a = new A
>> a.call()
>>
>> Running this produces:
>> Begin call
>> begin subcall
>> block call
>>
>>
>> If I assign the above function literal to a variable then the compiler
>> gives
>> an error:
>>
>> val n = (x: Int) => { return x }
>>
>> (fragment of InvokeException.scala):2: error: return outside method
>> definition
>> val n = (x: Int) => { return x }
>> ^
>>
>> On Fri, Jul 17, 2009 at 6:17 PM, Trond Olsen wrote:
>>
>> > Does anyone know why this particular exception doesn't result in a
>> > InvocationTargetException with java method invokation? I can't catch it
>> > either if I add an exception wrapper around the invoke() call either.
>> >
>
>










On Sat, Jul 18, 2009 at 1:17 AM, Naftoli Gugenheim <naftoligug [at] gmail [dot] com> wrote: