Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I've recently used flatMap to map String? to Double? in Swift. Calling:
optionalString.flatMap(Double.init)
and
optionalString.flatMap(Double.init(_:))
produces the same result. I have two questions:
Is there a difference between Double.init and Double.init(_:)?
I did not think that you can use function signatures in real Swift code. What are some other examples where you can use these?
No, there is no difference between Double.init and Double.init(_:).
These are called first class functions in Swift.
For example:
extension UIView {
func addSubviews(_ views: UIView...) {
views.forEach(addSubview)
}
}
More info: First class functions in Swift
Edit: There is a difference between Double.init and Double.init(_:). ( check comments )
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
im trying to convert a scala list of eithers, as such:
List(Left(3),Left(4),Left(1),Left(5))
Into a either of a list Either[List[Int],Int] like this?
Left(3,4,1,5)
Only using flatmap, map or fold?
ive been hammering at it for a while now and can simply not make it work
Assumed:
val a = List(Left(1), Left(2), Left(3)) // for example
Then following will return a Left[List[Int]]:
Left(a.map(_.value))
// Left(List(1, 2, 3))
Then you can extract the values out of the list, which I don't thin is generally a good idea.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I want instance's array just like as follow;
var ExampleObj = List(10)(new Obj)
Then I want to use this as;
var (hoge0, hoge1) = ExampleObj(2)(foo0, foo1, foo2)
The (new Obj) makes type mismatch.
[error] found : unittest.Obj
[error] required: Int
I do package unittest for Obj class.
What I make misunderstand ?
Could you please point out that point and how to solve it ?
If I understood your question correctly, you're trying to create a List with 10 instances of Obj class?
It can be done using fill method, like that:
List.fill(10)(new Obj)
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 3 years ago.
Improve this question
The Swift Programming Guide (Swift 5.1 Edition) says that subscripts can be used to access members of “a collection, list, or sequence.” Collection and Sequence are defined protocols in Swift and are well documented. Do lists exist in Swift as a separate entity? If so, what is the syntax for a list subscript?
It would be better if The Swift Programming Language (the book) didn't say “ a collection, list, or sequence”. As you point out, Swift has standard Collection and Sequence types. It does not have any standard type named List.
The closest thing would be Array:
let words: Array<String> = ["Mark", "Cowan"]
// or words: [String]
let firstWord = words[0]
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 3 years ago.
Improve this question
According to https://docs.swift.org/swift-book/LanguageGuide/Functions.html
We can design our function as
With argument label
func someFunction(firstParameterName: Int, secondParameterName: Int) {
// In the function body, firstParameterName and secondParameterName
// refer to the argument values for the first and second parameters.
}
someFunction(firstParameterName: 1, secondParameterName: 2)
Or
Omitting argument label
func someFunction(_ firstParameterName: Int, _ secondParameterName: Int) {
// In the function body, firstParameterName and secondParameterName
// refer to the argument values for the first and second parameters.
}
someFunction(1, 2)
Is there any rule-of-thumb, or best practice we should follow? So that, we know when we should have argument label, and when we should omitting argument label, when come to designing function?
In terms of naming in Swift, you should check out API Design Guidelines. These guidelines will give you a general feel of how to name things in Swift. Fair warning though, these rules are rather theoretical and at the end you are to decide how to name your functions in specific cases. Don't get too hung up on this really, even the most experienced developers have troubles with this, so I've been told.
In your particular situation you definitely shouldn't omit argument labels because type information is very generic and doesn't provide any clue as to what you're passing in.
Hope this helps!
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I am beginner in Swift and learning swift from "The Swift Programming language(Swift 3 beta)". In this book, there is one example of creating generic function but I am getting the above mentioned error.
func makeArray<Item>(repeating item: Item, numberOfTimes: Int)->[Item]
{
var result = [Item]()
for _ in 0..<numberOfTimes {
return.append(item)//Error here.
}
return result
}
This line:
return.append(item)
should be:
result.append(item)
You cannot append to return, it only returns the value.