FAQ
I have a simple function I'm using to parse command line arguments. Given
an option name (e.g. "--foo"), if it finds a match, it returns both the
subsequent option value and the argument array with those two elements
removed. When I do this with the following one liner:

     return true, args[i+1], append(args[:i], args[i+2:]...)

I'm getting the argument at index [i+1] AFTER the removal of the two
elements, which is not what I intended. The following fixes the issue:

     value := args[i+1]
     return true, value, append(args[:i], args[i+2:]...)

Are arguments to return evaluated from right to left in general, or is this
a special case with `append`?

Thanks,

Nathan

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

  • Rui Ueyama at Jul 25, 2014 at 10:04 pm
    The evaluation order of functions is left-to-right but the order of
    function calls and indexing of an array is not defined.
    http://golang.org/ref/spec#Order_of_evaluation

    In your case, append is the only function call in the return statement, and
    that evaluation order to true and args[i+1] is not defined. If you really
    want to do that in one line, you could do

       return true, func() T { return args[i+1] }(), append(args[:i],
    args[i+2:]...)

    but that's probably too awkward.

    On Fri, Jul 25, 2014 at 2:44 PM, Nathan Clark wrote:

    I have a simple function I'm using to parse command line arguments. Given
    an option name (e.g. "--foo"), if it finds a match, it returns both the
    subsequent option value and the argument array with those two elements
    removed. When I do this with the following one liner:

    return true, args[i+1], append(args[:i], args[i+2:]...)

    I'm getting the argument at index [i+1] AFTER the removal of the two
    elements, which is not what I intended. The following fixes the issue:

    value := args[i+1]
    return true, value, append(args[:i], args[i+2:]...)

    Are arguments to return evaluated from right to left in general, or is
    this a special case with `append`?

    Thanks,

    Nathan

    --
    You received this message because you are subscribed to the Google Groups
    "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an
    email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Dan Kortschak at Jul 25, 2014 at 10:11 pm
    That is because the args[x+1] is not a function call.

    See here for your expected behaviour:

    http://play.golang.org/p/X2twv1IuHJ

    And in the spec[1] (last paragraph).

    ... when evaluating the operands of an expression, assignment, or return
    statement, all function calls, method calls, and communication
    operations are evaluated in lexical left-to-right order.

    For example, in the (function-local) assignment

    y[f()], ok = g(h(), i()+x[j()], <-c), k()

    the function calls and communication happen in the order f(), h(), i(),
    j(), <-c, g(), and k(). However, the order of those events compared to
    the evaluation and indexing of x and the evaluation of y is not
    specified.

    [1]http://golang.org/ref/spec#Order_of_evaluation:


    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Nathan Clark at Jul 29, 2014 at 12:22 am
    Makes sense. Thanks!
    On Friday, July 25, 2014 6:11:22 PM UTC-4, kortschak wrote:

    That is because the args[x+1] is not a function call.

    See here for your expected behaviour:

    http://play.golang.org/p/X2twv1IuHJ

    And in the spec[1] (last paragraph).

    ... when evaluating the operands of an expression, assignment, or return
    statement, all function calls, method calls, and communication
    operations are evaluated in lexical left-to-right order.

    For example, in the (function-local) assignment

    y[f()], ok = g(h(), i()+x[j()], <-c), k()

    the function calls and communication happen in the order f(), h(), i(),
    j(), <-c, g(), and k(). However, the order of those events compared to
    the evaluation and indexing of x and the evaluation of y is not
    specified.

    [1]http://golang.org/ref/spec#Order_of_evaluation:

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedJul 25, '14 at 9:44p
activeJul 29, '14 at 12:22a
posts4
users3
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase