[
Date Prev][
Date Next][
Thread Prev][
Thread Next][
Date Index][
Thread Index]
[
List Home]
Re: [golo-dev] Return from main?
|
Vincent (2016-10-11 22:00):
> Hello,
>
> I've subscribed with a different e-mail address. Hopefully this webmail
> client will not completely mess up the formatting, unlike my primary
> account's webmail client. Thanks for the nice replies on my first mail,
> and Yannick, thanks for showing the more Golo-ish code :)
You're welcome.
>
> Another question: can I do an early return from the main function?
>
> The following code...:
>
> ------
>
> module Test
>
> function main = |args| {
> return
> }
>
> ------
>
> ...produces a "java.lang.VerifyError: Method expects a return value"
> traceback. I've also tried "return null" or return an integer, but with
> the same result.
>
> I know some people consider it bad/questionable design when having
> multiple exit paths in a single function, but especially for
> command-line driven tools that need to validate various parameters, etc.
> I think this can sometimes be justified.
The issue here is not having multiple exit path, since you can do it in
functions, but that the `main` function will ends as a
`public static void main(String[] args)` java method, and thus can't
return anything. This will be required by the JVM (AFAIK). Indeed, since
“nobody” is actually calling `main`, the returned value is meaningless
You can use `System.exit(code)` to early return (actually exit) from the
`main` function.
On your use case, if the parameters validation fails, you should
`System.exit(1)` (or anything > 0) to let the shell know that something
wrong happened.
> Kind regards,
> Vincent
> _______________________________________________
> golo-dev mailing list
> golo-dev@xxxxxxxxxxx
> To change your delivery options, retrieve your password, or unsubscribe from this list, visit
> https://dev.eclipse.org/mailman/listinfo/golo-dev
>
Attachment:
signature.asc
Description: PGP signature