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
[SR-128] Pass array to variadic function #42750
Comments
This is a very early request for Swift, though its original form is "can't forward variadic arguments". :-) Most of our thoughts around this suggest using either As this is a language change, it will need to go through the Swift evolution process. |
Comment by Arthur Sabintsev (JIRA) Should I put together a Swift Evo proposal? |
Sure, that'd be great! |
Comment by Radek Pietruszewski (JIRA) arthur (JIRA User) FWIW, passing an array directly is a problem because it could be ambiguous in some cases when the argument is an array. Ruby solves it by using the same syntax as defining a var-arg: `*arr` — which deconstructs an array into a series of arguments. In Swift, `arr...` would do the job. |
Comment by Arthur Sabintsev (JIRA) Well, it shouldn't be ambiguous if the function is expecting `Int...`. It should be able to differentiate between an array of Ints, `[1,2,3]`, or a variable length of Ints `1,2,3` that can be turned into an array. |
Comment by Radek Pietruszewski (JIRA) No, but consider what would happen if the arg type was `[Any]...` |
Comment by Arthur Sabintsev (JIRA) Unless I'm missing something, I think the following should/would happen: For Any...,
Both yield an array of [Any]. For [Any]...,
Both yield an array of [[Any]]. If I'm wrong, please correct me. |
Radek is right, that first case is ambiguous.
Does this print |
Comment by Arthur Sabintsev (JIRA) Ah, ok. I see the error in my thought process. @belkadan Do you think this is something worth bringing up to the community as a whole? |
Comment by Arthur Sabintsev (JIRA) Proposal posted: apple/swift-evolution#52 |
Comment by Kostiantyn Koval (JIRA) The use-case from real project for me was that I wanted to pass a variadic parameter further. func format(params: Int ...) {
print(params)
// params are passed as a [Int]. The print function will receive only 1 argument an [Int]
} I like the proposal. The mentioned overloading solution is not always possible. |
Comment by Arthur Sabintsev (JIRA) Ah, so it's even more dire for you. For me, overloading worked, but it added unnecessary code: |
Comment by Raphael (JIRA) Any movement here? For the life of me, I can't figure out how to map a dictionary and get another one. Mapping a dictionary gives an array of key-value pairs (why?!) which I can't feed to any intializer of `Dictionary`. |
Reitzig (JIRA User) The problem you're describing isn't directly related to this SR. It's something Swift 4 solves with extension Dictionary {
func mapValues<T>(_ transform: (Value) throws -> T) rethrows -> Dictionary<Key, T> {
return try reduce([:]) {
var newDict = $0
newDict[$1.key] = try transform($1.value)
return newDict
}
}
} Also, in Swift 4 there's a new initializer on |
Comment by Mark Lewis (JIRA) arthur (JIRA User) wrote:
I know this comment is a bit dated, but if I found it someone else might too. There's no need to duplicate your code in the overload like that. You could have made the variadic function that takes https://gist.github.com/ThuggishNuggets/36674089d7d6ce2d00c007e32c332d9b |
Comment by Arthur Sabintsev (JIRA) ThuggishNuggets (JIRA User) - Good on you for following up. I actually did exactly that a while back, as it was the most sensible solution. For reference: https://github.com/ArtSabintsev/Zephyr/blob/master/Sources/Zephyr.swift#L79-L91 |
Comment by Raphael (JIRA) @ffried Thanks! I should have phrase my comment more carefully. Of course it's possible to write a function that does what I want; I meant how to do it using existing standard API. It seems as if the `dictionaryLiteral` initializer should work, but it doesn't due to this restriction (and maybe a couple of poor API design choices). |
Comment by Tadeas Kriz (JIRA) It seems that this makes overriding a method with variadic parameters impossible as we can't pass it down. Any ideas on how to workaround this without changing the superclass itself? Thanks. |
Comment by Jakub Jelínek (JIRA) Hi guys. I would appreciate Java-like style, so having an array Edit: I see you made the proposal already |
Comment by Ky (JIRA) is there a Swift Evolution proposal for this yet? |
Comment by Arthur Sabintsev (JIRA) I made one years ago - it went nowhere: https://github.com/ArtSabintsev/swift-evolution/blob/master/proposals/array-variadic-function.md |
Comment by Maarten Billemont (JIRA) Without a resolution for this issue, there is essentially no way to invoke external API that exposes only variadic functions in a non-literal manner. As an example, consider `os_log`, which takes a `StaticString` format string and a `CVarArg...` argument list but exposes no API to pass arguments in a non-literal manner, making OS Logging all but unavailable for programmatic logging (eg. from third-party loggers). |
any news? |
Any... news? |
@alex-cova I had to do this:
|
To my knowledge this topic did no go beyond the aforementioned forums discussions (not that it cannot), with the exception of that one premature proposal years before. If anyone is willing to write up a well-motivated evolution proposal, it must consider all the previous, as well as any new, feedback. |
Additional Detail from JIRA
md5: fe683ee214998d48375c8fbaf79fbfbc
is duplicated by:
relates to:
Issue Description:
I want to pass an array into a function that accepts a variadic argument, as within the scope of the function, the argument p, is treated as an array.
The text was updated successfully, but these errors were encountered: