Segmentation Fault in Swift - swift

I get the following error when running swift run inside of an executable Swift Package:
zsh: segmentation fault swift run
I have been able to boil the code down to the following:
enum MyEnum {
case FirstCase
case SecondCase
case Other
}
struct MyEnumCollection {
private var enums: [MyEnum]
}
extension MyEnumCollection: RangeReplaceableCollection {
public init() {
self.enums = []
}
}
extension MyEnumCollection: Collection {
public var startIndex: Int {
0
}
public var endIndex: Int {
self.enums.count
}
public subscript(position: Int) -> MyEnum {
return self.enums[position]
}
public func index(after i: Int) -> Int {
return self.enums.index(after: i)
}
}
var collection = MyEnumCollection()
collection.append(MyEnum.FirstCase)
The segmentation fault happens on the last line, at the append statement.
Can someone help me understand why this is happening and how I should fix this?

Update
Thanks to a comment from #MartinR it turns out that a even better solution is to implement replaceSubrange(_:with:) from the RangeReplaceableCollection protocol instead of append
mutating func replaceSubrange<C>(_ subrange: Range<Int>, with newElements: C) where C : Collection, MyEnum == C.Element {
self.enums.replaceSubrange(subrange, with: newElements)
}
Old solution
You need to implement append() as well
public mutating func append(_ newElement: MyEnum) {
enums.append(newElement)
}
There is a default implementation of this function but of course it has no knowledge about your internal data source enums so it is not usable.
Going forward you might also need to implement other functions that has a default implementation.
Another thing, personally I would use the properties of the Array class as well when conforming to the protocol.
public var startIndex: Int {
return enums.startIndex
}
public var endIndex: Int {
return self.enums.endIndex
}

Related

How to create an instance that satisfies the generic requirements

I have created the following code and everything works fine except the definition of TestDBAPI.
When I want to create a type that conforms to the DBAPIProtocol protocol, it is always impossible to generate a type instance that satisfies the generic constraints
Please, how can I define TestNoteFetcher to satisfy the protocol requirement of DBAPIProtocol.
ps: I hope the flexibility of generic definitions can be maintained in DBAPIProtocol
thanks
import Combine
// For Value
public enum WrappedID: Equatable, Identifiable, Sendable, Hashable {
case string(String)
case integer(Int)
public var id: Self {
self
}
}
public protocol BaseValueProtocol: Equatable, Identifiable, Sendable {
var id: WrappedID { get }
}
public struct Note: BaseValueProtocol {
public var id: WrappedID
public var index: Int
public init(id: WrappedID, index: Int) {
self.id = id
self.index = index
}
}
// For Object
public protocol ConvertibleValueObservableObject<Value>: ObservableObject, Equatable, Identifiable where ID == WrappedID {
associatedtype Value: BaseValueProtocol
func convertToValueType() -> Value
}
public final class TestNote: ConvertibleValueObservableObject {
public static func == (lhs: TestNote, rhs: TestNote) -> Bool {
true
}
public var id: WrappedID {
.integer(1)
}
public func convertToValueType() -> Note {
.init(id: .integer(1), index: 0)
}
}
// For Fetcher
public protocol ObjectFetcherProtocol<Object,ConvertValue> {
associatedtype ConvertValue: BaseValueProtocol
associatedtype Object: ConvertibleValueObservableObject<ConvertValue>
var stream: AsyncPublisher<AnyPublisher<[Object], Never>> { get }
}
public final class TestNoteFetcher: ObjectFetcherProtocol {
public typealias ConvertValue = Note
public typealias Object = TestNote
public var stream: AsyncPublisher<AnyPublisher<[TestNote], Never>> {
sender.eraseToAnyPublisher().values
}
public var sender: CurrentValueSubject<[TestNote], Never>
public init(_ notes: [TestNote] = []) {
sender = .init(notes)
}
}
// For API
public protocol DBAPIProtocol {
var notesFetcher: () async -> any ObjectFetcherProtocol<any ConvertibleValueObservableObject<Note>, Note> { get set }
}
// get error in here . Cannot convert value of type 'TestNoteFetcher.Object' (aka 'TestNote') to closure result type 'any ConvertibleValueObservableObject<Note>'
public final class TestDBAPI: DBAPIProtocol {
public var notesFetcher: () async -> any ObjectFetcherProtocol<any ConvertibleValueObservableObject<Note>, Note> = {
TestNoteFetcher([])
}
}
Since your closure returns too many anys, the compiler got confused & is telling you that TestNoteFetcher does not conform any ObjectFetcherProtocol. Generics are your friend, you can use associatedtype to skip all this code & fix the issue:
public protocol DBAPIProtocol {
associatedtype Fetcher: ObjectFetcherProtocol
var notesFetcher: () async -> Fetcher { get set }
}
public final class TestDBAPI: DBAPIProtocol {
public var notesFetcher: () async -> TestNoteFetcher = {
TestNoteFetcher([])
}
}
Sometimes the answer is right in front of me, but I do turn a blind eye to it. follow the Xcode warning, I made a change to this part of the code and it works now.
public protocol ObjectFetcherProtocol<ConvertValue> {
associatedtype ConvertValue: BaseValueProtocol
var stream: AsyncPublisher<AnyPublisher<[any ConvertibleValueObservableObject<ConvertValue>], Never>> { get }
}
public final class TestNoteFetcher: ObjectFetcherProtocol {
public var stream: AsyncPublisher<AnyPublisher<[any ConvertibleValueObservableObject<Note>], Never>> {
sender.eraseToAnyPublisher().values
}
public var sender: CurrentValueSubject<[any ConvertibleValueObservableObject<Note>], Never>
public init(_ notes: [any ConvertibleValueObservableObject<Note>] = []) {
sender = .init(notes)
}
}
This way, I can still maintain the flexibility defined in DBAPIProtocol without having to introduce association types

RangeReplaceableCollection conformance doesn't require... actually anything

According to the documentation:
To add RangeReplaceableCollection conformance to your custom
collection, add an empty initializer and the replaceSubrange(_:with:)
method to your custom type.
But in practice it's not required! (except for empty initializer)
// Just stubs for minimal reproducible code
struct Category: Hashable {}
struct Product {}
struct ProductCollection {
typealias DictionaryType = [Category : [Product]]
// Underlying, private storage
private var products = DictionaryType()
// Enable our collection to be initialized with a dictionary
init(products: DictionaryType = DictionaryType()) {
self.products = products
}
}
extension ProductCollection: Collection {
// Required nested types, that tell Swift what our collection contains
typealias Index = DictionaryType.Index
typealias Element = DictionaryType.Element
// The upper and lower bounds of the collection, used in iterations
var startIndex: Index { return products.startIndex }
var endIndex: Index { return products.endIndex }
// Required subscript, based on a dictionary index
subscript(index: Index) -> Iterator.Element {
get { return products[index] }
}
// Method that returns the next index when iterating
func index(after i: Index) -> Index {
return products.index(after: i)
}
}
extension ProductCollection: ExpressibleByDictionaryLiteral {
init(dictionaryLiteral elements: (Category, [Product])...) {
self.init(products: .init(uniqueKeysWithValues: elements))
}
}
extension ProductCollection: RangeReplaceableCollection {
init() {
products = DictionaryType()
}
// func replaceSubrange<C: Collection, R: RangeExpression>(_ subrange: R, with newElements: C)
// where Self.Element == C.Element, Self.Index == R.Bound {
// }
}
The code is taken from a great (but not related to the post's topic) John Sundell article.
This code compiles even though replaceSubrange function is not provided.
One more question. Why should I provide an empty initializer explicitly in this situation? I can initialize the struct like ProductCollection() without having that initializer. I can do this for many reasons: 1) products property has initializing value provided 2) main initializer has default value provided 3) there is also a ExpressibleByDictionaryLiteral initializer which can be used to initialize an empty object.
So why I have to provide one more empty initializer explicitly?
But please, the first question about replaceSubrange function is more important :)
That is a bug which has also been discussed in the Swift forum:
SR-6501 RangeReplaceableCollection default implementations cause infinite recursion
Compiler lets me use incomplete RangeReplaceableCollection
Using Swift
The reason is that there is an overload of the replaceSubRange() method (taking a RangeExpression as the first argument) which the compiler erroneously accepts as satisfying the protocol requirement.
But note that even if the code compiles without implementing the required method, it does not work and leads to an infinite loop. Here is a short example:
struct MyCollection : MutableCollection {
private var storage: [Int] = []
init(_ elements: [Int]) { self.storage = elements }
var startIndex : Int { return 0 }
var endIndex : Int { return storage.count }
func index(after i: Int) -> Int { return i + 1 }
subscript(position : Int) -> Int {
get { return storage[position] }
set(newElement) { storage[position] = newElement }
}
}
extension MyCollection: RangeReplaceableCollection {
init() { }
}
var mc = MyCollection([0, 1, 2, 3, 4, 5])
mc.replaceSubrange(0..<3, with: [2, 3, 4])
Running that code leads to an “infinite” loop and eventually crashes with EXC_BAD_ACCESS due to a stack overflow.

Swift: Abstract base class/protocol with private members

I've created an abstract base class-like structure in Swift, using protocol extensions, as per this answer. This is a simplified example:
protocol AbstractBase {
var _constant: Int { get }
func _operation(_ val: Int) -> Int
}
public class ConcreteSub: AbstractBase {
let _constant: Int = 42
func _operation(_ val: Int) -> Int {
return val + 2
}
}
extension AbstractBase {
func mainOperation(_ val: Int) -> Int {
return _operation(val + _constant)
}
}
So basically, ConcreteSub provides the implementation details needed by AbstractBase, namely _constant and _operation.
I would like to hide those details from clients, and only expose mainOperation. However, Swift does not allow me to make the members fileprivate on the protocol -- if I do the following
protocol AbstractBase {
fileprivate var _constant: Int { get }
// etc
I get "error: 'fileprivate' modifier cannot be used in protocols".
Nor can I apply the modifier on the subclass -- when I try
public class ConcreteSub: AbstractBase {
fileprivate let _constant: Int = 42
// etc
I get "error: property '_constant' must be declared internal because it matches a requirement in internal protocol 'AbstractBase'".
Lastly, when I make the whole protocol fileprivate, I get no compile errors, but I consistently run into Linking errors, which I guess is because the protocol is private, but the subclass is public.
Is there another way I'm missing?
When I need an abstract base with some properties/functions hidden I use class with some additional fatalErrors and asserts to crash whenever someone is trying to use Base instead of implementation.
public class AbstractBase {
init() {
assert(type(of: self) != AbstractBase.self, "Abstract class")
}
fileprivate var _constant: Int {
fatalError("Abstract class")
}
fileprivate func _operation(_ val: Int) -> Int {
fatalError("Abstract class")
}
func mainOperation(_ val: Int) -> Int {
return _operation(val + _constant)
}
}
public class ConcreteSub: AbstractBase {
fileprivate override var _constant: Int {
return 42
}
fileprivate override func _operation(_ val: Int) -> Int {
return val + 2
}
}
I actually just ran into this issue. As of Swift 5.1, you can do this instead:
protocol MyProtocol {
var someVisibleVar: String { get }
func someVisibleFunc()
}
fileprivate extension MyProtocol {
var someFilePrivateVar: String {
"whatever"
}
func someFilePrivateFunc() {
print("someFilePrivateFunc() was called with \(someVisibleVar)")
}
}
class SomeClass: MyProtocol {
var someVisibleVar: String { "whatever" }
func someVisibleFunc() {
if someFilePrivateVar == someVisibleVar {
someFilePrivateFunc()
}
}
}
class SomeOtherClass: MyProtocol {
var someVisibleVar: String { "something else" }
func someVisibleFunc() {
if someFilePrivateVar == someVisibleVar {
someFilePrivateFunc()
}
}
}

How to use a Set in Swift 3.0 without generic usage

First of all i have to say i come from Java programming and compared with Java everything in Swift 3.0 seems to be totally complicated. I thought what i want to do is easy but it turned out it is not.
I have two objects:
protocol Customer {
}
and:
class Consulter {
}
I want my Consulter class to hold a Set of Customer:
class Consulter {
var customers: Set<Customer>;
}
Ok here the first thing. The Compiler now is complaining that Customer has to implement Hashable... really? Swift isnt doing that for me? Ok. So lets go for it:
func ==(lhs: Customer, rhs: Customer) -> Bool {
return lhs.hashValue == rhs.hashValue;
}
protocol Customer: Hashable {
var: hashValue: Int {
return "123".hashValue;
}
}
And in my Consulter class i now would have to do the following:
class Consulter<T: Customer> {
var customers: Set<T>;
}
Ok this is working. But now i have another class:
func ==(lhs: Location, rhs: Location) -> Bool { // here the error!
return lhs.hashValue == rhs.hashValue;
}
class Location<T: Customer> : Hashable {
var customer: T;
....
}
For the Equatable of the class Location i now get the error:
Reference to generic type 'Location' requires arguments in <...>
So what argument is the compiler expecting here? I dont know any concrete types at this point.
EDIT
my Customer protocol will later have different concrete implementations. A Customer can for example be a Family or a Person. In the Consulter class i want to have a Set of Customer containing both: families and persons. I think this is a simple and logical approach.
Since you intend to use types conforming to Customer in applications where they must be Hashable (e.g. as members of a Set), there is no reason why not to add this Hashable constraint directly to the Customer protocol. This way you move the responsibility to conformance to Hashable to the actual types that you consider to be Customer's
protocol Customer: Hashable {}
class Consulter<T: Customer> {
var customers: Set<T>?
}
class Location<T: Customer>: Hashable {
var customer: T
init(customer: T) { self.customer = customer }
var hashValue: Int {
return customer.hashValue
}
}
func ==<T: Customer>(lhs: Location<T>, rhs: Location<T>) -> Bool {
return lhs.customer == rhs.customer /* && ... test other properties */
}
Also, be careful using X.hashValue == Y.hashValue for testing for equality, since there is no guarantee that hashvalues are unique (consider them mainly used for clever "bin" categorization).
Or, since Swift 3
// ... as above
class Location<T: Customer>: Hashable {
var customer: T
init(customer: T) { self.customer = customer }
var hashValue: Int {
return customer.hashValue
}
static func ==(lhs: Location<T>, rhs: Location<T>) -> Bool {
return lhs.customer == rhs.customer /* && ... test other properties */
}
}

Add sort to array wrapping class

The following code is from this answer: https://stackoverflow.com/a/28191539/4096655
public class SynchronizedArray<T> {
private var array: [T] = []
private let accessQueue = dispatch_queue_create("SynchronizedArrayAccess", DISPATCH_QUEUE_SERIAL)
public func append(newElement: T) {
dispatch_async(self.accessQueue) {
self.array.append(newElement)
}
}
public subscript(index: Int) -> T {
set {
dispatch_async(self.accessQueue) {
self.array[index] = newValue
}
}
get {
var element: T!
dispatch_sync(self.accessQueue) {
element = self.array[index]
}
return element
}
}
}
var a = SynchronizedArray<Int>()
a.append(1)
a.append(2)
a.append(3)
// can be empty as this is non-thread safe access
println(a.array)
// thread-safe synchonized access
println(a[0])
println(a[1])
println(a[2])
I am doing something very much like but am having trouble setting up a sort to pass to the array of generics. Ideally I'd like a sortInPlace but am not sure how to do it.
If you want to sort the wrapped array, then one way is to constrain T to a type conforming to Comparable. If you add this restriction, then a sorting function is easy to implement, simply ask the array to sort itself:
public class SynchronizedArray<T: Comparable> {
...
public func sortInPlace() {
array.sortInPlace(<)
}
For custom classes, you need to add an extension conforming to Comparable, and overload the == and < operators (reference here)
extension MyClass: Comparable {
}
func ==(lhs: MyClass, rhs: MyClass) -> Bool {
}
func <(lhs: MyClass, rhs: MyClass) -> Bool {
}
var a = SynchronizedArray<MyClass>()