您可以使用地图创建没有包装器的实例吗? [英] Can you use map to create instances without a wrapper?
问题描述
在Python中,您可以将类的名称作为 map
的参数,以创建该类的实例:
class Point(object):
def __init __(self,(x,y)):
self.x = x
self.y = y
coords = [(1.,2.),(3.,4.)]
pts = map(Point,coords)
这通常被证明是一个方便的模式,所以我想在Swift尝试一样的东西。
首先,我们设置了 Point
class:
import Cocoa
class Point
{
var x:Float
var y:Float
init(x:Float,y:Float){
self.x = x
self.y = y
}
}
var pt = Point(x:1,y:2)//工作正常
但是当我们尝试使用 .map
创建实例时,我们会收到一个错误:
let coords:(Float,Float)[] = [(1,2),(3,4)]
//(Point).Type不能转换为'(Float,Float) - > $ T3'
var pts = coords.map(Point)
//初始化程序无法引用无参数
var pts = coords.map(Point.init)
$对我有兴趣的是,如果我们首先定义一个包装函数,这个 工作: func wrapper(x:Float,y:Float) - >点{
return Point(x:x,y:y)
}
//这个*是*成功
var ptsWrapped = coords.map(wrapper)
好的,现在我很好奇是禁止在方法上使用 map
:
extension Point {
func newPointByAdding(x:Float,y:Float) - >点{
return Point(x:self.x + x,y:self.y + y)
}
}
//这样可以预期的b $ b var origin = Point(x:0,y:0)
var ptsAdded = coords.map(origin.newPointByAdding)
...不,工作正常。
我会自由地承认我还没有花很多时间快速,所以我可能会在规范中缺少一些禁止这个的内容。
可以使用 map
在swift中创建一个类/ struct的新实例?
如果不是,为什么不? / p>
- 是因为
init
不是 func ?
- 是否与某些上下文中不能转换为位置参数的命名参数有关?
解决方案
Swift 2的更新:
提交错误的工作!
在Swift 2中,现在可以使用 coor ds.map(Point.init)
:
旧答案:
- 是因为init不是一个func?
是的。在Swift中,函数类型由一个由箭头分隔的参数和返回类型组成( - >
)和 map
定义为 func map< U>(transform:(T)→U) - > [U]
,即它接收一个功能。在语法,函数声明和初始化器声明分别对待。后者没有返回类型,因为它不是真正的函数,只是一个用于初始化实例的代码块。如果您尝试通过 Point.init
,则会收到错误无法引用初始化程序。
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__(self, (x, y)):
self.x = x
self.y = y
coords = [(1., 2.), (3., 4.)]
pts = map(Point, coords)
This often proves to be a handy pattern, so I wanted to try the same thing in Swift.
First, we set up our Point
class:
import Cocoa
class Point
{
var x: Float
var y: Float
init(x: Float, y: Float) {
self.x = x
self.y = y
}
}
var pt = Point(x: 1, y: 2) // works fine
But when we try to use .map
to create instances, we get an error:
let coords: (Float,Float)[] = [(1, 2), (3, 4)]
// (Point).Type is not convertible to '(Float, Float) -> $T3'
var pts = coords.map(Point)
// Initializer cannot be referenced without arguments
var pts = coords.map(Point.init)
What's interesting to me is that if we first define a wrapper function, this does work:
func wrapper(x: Float, y: Float) -> Point {
return Point(x: x, y: y)
}
// This *is* successful
var ptsWrapped = coords.map(wrapper)
Ok, so now I'm curious whether this is a prohibition against using map
on methods:
extension Point {
func newPointByAdding(x: Float, y: Float) -> Point {
return Point(x: self.x + x, y: self.y + y)
}
}
// This works as expected
var origin = Point(x: 0, y: 0)
var ptsAdded = coords.map(origin.newPointByAdding)
...nope, that works fine.
I'll freely admit that I haven't yet spent much time with swift, so I may be missing something in the spec which prohibits this.
Is it possible to use map
to create new instances of a class/struct in swift?
If not, why not?
- is it because
init
is not a func
?
- is it something to do with named arguments not being convertible to positional arguments in certain contexts?
解决方案 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<U>(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".
这篇关于您可以使用地图创建没有包装器的实例吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!