我有一个有两个通用对象数组的类:
public class Scene {
objects:[Geometry<BSDF>]
lights:[Geometry<Emitter>]
...
我有一个函数,我想从这些数组中的任何一个返回一些元素:
public func hit<M>(origin:Vec, direction:Vec) -> (Geometry<M>?,CGFloat) {
var minDist = CGFloat.greatestFiniteMagnitude
var minGeometry:Geometry<M>? = nil
for g in objects {
let d = g.intersect(origin: origin, direction: direction)
if (d != 0 && d < minDist) {
minDist = d
minGeometry = g
}
}
// Do the same for lights
return (minGeometry,minDist)
}
然而,对于行minGeometry = g
,我收到错误'无法指定类型的值'几何&lt; BSDF&gt;'输入Geometry&lt; _&gt;'我是否误解了仿制药的工作原理以及如何创建这样的功能?