Can you use map to create instances without a wrapper?

后端 未结 3 1437
半阙折子戏
半阙折子戏 2021-01-21 01:56

In Python, you can give the name of a class as an argument to map in order to create instances of that class:

class Point(object):
    def __init__(         


        
3条回答
  •  醉梦人生
    2021-01-21 02:33

    Update for Swift 2:

    Filing bugs works!

    In Swift 2 this is now possible with coords.map(Point.init):


    Old answer:

    • is it because init is not a func?

    Yep. In Swift, a function type "consists of a parameter and return type separated by an arrow (->)", and map is defined as func map(transform: (T) -> U) -> [U], i.e. it takes in a function. In the grammar, "function declaration" and "initializer declaration" are treated separately. The latter doesn't have a return type because it's not really a function, just a block of code used to initialize instances. And if you try to pass Point.init, you'll get the error "Initializer cannot be referenced without arguments".

    File a bug!

提交回复
热议问题