I am reading some books about the functional programming in swift. When I see some books said the function in swift is "first-class function" and I confused what is the meaning of the "first-class". Can anybody answer me or give me some example code?
相关问题
- “Zero out” sensitive String data in Swift
- SwiftUI: UIImage (QRCode) does not load after call
- Get the NSRange for the visible text after scroll
- UIPanGestureRecognizer is not working in iOS 13
- What does a Firebase observer actually do?
相关文章
- Using if let syntax in switch statement
- Enum with associated value conforming to CaseItera
- Swift - hide pickerView after value selected
- Is there a Github markdown language identifier for
- How can I vertically align my status bar item text
- Adding TapGestureRecognizer to UILabel in Swift
- Attempt to present UIAlertController on View Contr
- Swift - Snapshotting a view that has not been rend
Please see my answer in this question. And I will add one more example (please check the example in that question first):
And the usage
Functions are first-class citizens in Swift because you can treat a function like a normal value. For example, you can …
Its just fancy terminology that is currently trendy. It just means that you can have a variable that holds a reference to a function.
Its been in programming forever; in C and Fortran you have pointers to functions. Typical use in both these languages is to pass a compare function into a sort function so that the sort function can sort any data type.
In some languages, e.g. Java, it appears that you don't have pointers to functions, but you do have pointers to interfaces. So you define a Comparable interface with a compare method and pass an instance of Comparable to your sort.
Nothing new, just confusing terminology for a familiar concept. Presumably to try and make the feature sound new and sexy.