Skip to content

Variadic parameters don't work if some of the args are expended and some aren't #2873

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
gopherbot opened this issue Feb 3, 2012 · 8 comments

Comments

@gopherbot
Copy link
Contributor

by ficoos:

Before filing a bug, please check whether it has been fixed since
the latest release: run "hg pull", "hg update default", rebuild, and
retry
what you did to
reproduce the problem.  Thanks.

What steps will reproduce the problem?
// Compiles
func RfoldInt(op func(a, b int) int, ints ...int) int {
    if len(ints) == 0 {
        return 0
    }
    if len(ints) == 1 {
        return ints[0]
    }
    return op(a, RfoldInt(ints[1:]))
}

// Doesn't
func LfoldInt(op func(a, b int) int, ints ...int) int {
    if len(ints) == 0 {
        return 0
    }
    if len(ints) == 1 {
        return ints[0]
    }
    a = ints[0]
    b = ints[1]
    return Lfold(op(a, b), ints...)
}


What is the expected output?
I would expect the LfoldInt function to compile.

What do you see instead?
Compiler prints:
too many arguments in call to LfoldInt

Which compiler are you using (5g, 6g, 8g, gccgo)?
6g

Which operating system are you using?
Linux

Which revision are you using?  (hg identify)
9f2be4fbbf69 weekly/weekly.2012-01-20


Please provide any additional information below.
God speed!
@gopherbot
Copy link
Contributor Author

Comment 1 by ficoos:

There is a type in the code I pasted.
This:
    return op(a, RfoldInt(ints[1:]))
Should be:
    return op(a, RfoldInt(ints[1:]...))

@gopherbot
Copy link
Contributor Author

Comment 2 by ficoos:

There is a typo in the code I pasted.
This:
    return op(a, RfoldInt(ints[1:]))
Should be:
    return op(a, RfoldInt(ints[1:]...))

@gopherbot
Copy link
Contributor Author

Comment 3 by ficoos:

Also a bug Lfold itself, gosh this is embarrassing, but this is what you get when you
make shift a piece of code in the  middle of the night. But 
This:
    return Lfold(op(a, b), ints...)
Should be:
    return Lfold(op(a, b), ints[2:]...)
But it should compile non the less

@rsc
Copy link
Contributor

rsc commented Feb 3, 2012

Comment 4:

I'm having a hard time following this.
Please use play.golang.org to get the code how you
want it for the report, so that clicking Compile shows
the error you believe is a bug, and then paste the
Share link here.
Thanks.

Status changed to WaitingForReply.

@gopherbot
Copy link
Contributor Author

Comment 5 by ficoos:

Sorry about that.
http://play.golang.org/p/-KLRKJqXoa
Expected output is:
Rfold 15
Lfold 15

@rsc
Copy link
Contributor

rsc commented Feb 3, 2012

Comment 6:

To avoid confusion about what slice is being passed,
the ... syntax only applies when it is providing the entire
... argument; you can't splice in additional parameters
with it.
You can use an explicit append instead:
    return LfoldInt(op, append([]int{op(a, b)}, ints[2:]...)...)
However, for this specific case, I'd suggest writing foldl and foldr this way:
http://play.golang.org/p/C6d3gM44FF
Russ

Status changed to WorkingAsIntended.

@rsc
Copy link
Contributor

rsc commented Feb 3, 2012

Comment 7:

Sorry, typo: http://play.golang.org/p/A4DypCG_cV

@gopherbot
Copy link
Contributor Author

Comment 8 by ficoos:

Thanks for the quick response

@golang golang locked and limited conversation to collaborators Jun 24, 2016
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants