The Problem
I have a piece of code that when compiled with Xcode 10.0 Beta 5 causes an unexpected error and a compiler crash. Isn't the implementaion of SE-0157 supposed to remedy this?
The Error
Scalar.swift: error: generic struct 'ScalarSpace' references itself
public protocol Scalar: Vector, ExpressibleByIntegerLiteral where ScalarType == Self, Space == ScalarSpace<Self> {
^
Scalar.swift: note: type declared here
public struct ScalarSpace<ScalarType> where ScalarType: Scalar {
^
error: Illegal instruction: 4
The Code
// Vector.swift
public protocol Vector {
associatedtype ScalarType: Scalar
associatedtype Space: VectorSpace where Space.VectorType == Self
static var identitySpace: Space { get }
}
public protocol VectorSpace {
associatedtype VectorType: Vector
associatedtype BasisVectors: RandomAccessCollection where BasisVectors.Element == VectorType
var basisVectors: BasisVectors { get }
}
// Scalar.swift
public protocol Scalar: Vector, ExpressibleByIntegerLiteral where ScalarType == Self, Space == ScalarSpace<Self> {
// The `Space == ScalarSpace<Self>` part of this declaration is causing the crash.
}
extension Scalar {
public static var identitySpace: Space {
return .init(scalar: 1)
}
}
public struct ScalarSpace<ScalarType> where ScalarType: Scalar {
public var scalar: ScalarType
}
extension ScalarSpace: VectorSpace {
public typealias VectorType = ScalarType
public typealias BasisVectors = CollectionOfOne<VectorType>
public var basisVectors: BasisVectors {
return .init(self.scalar)
}
}