What's wrong with my enum?

I am trying to implement a Result-enum as frequently seen in Swift-based code. I am having the code listed below but when I try to compile this in a shared project I am getting the error Duplicate identifier "error" [/Users/Development/Projects/version-3/config-app/SharedProject/Result.swift (40)] but I am failing to see where duplicate use of error is?

/// An enm representing a typical error for a Result<T>
public enum ResultError: Error {
    case invalid(String)
    case parseError
    case networkError
    case notFound
    case cancelled
}

/// An enum representing either a failure with an explanatory error, or a success with a result value.
public enum Result<Value> {
    case success(Value)
    case failure(ResultError)
    
    // MARK: Constructor
    
    /// Constructs a success wrapping a `value`.
    public init(value: Value) {
        self = .success(value)
    }
    
    /// Constructs a failure wrapping an `error`.
    public init(error: ResultError) {
        self = .failure(error)
    }
    
    // MARK: Helper methods
    public func dematerialize() throws -> Value {
        switch self {
            case let .success(value):
                return value
            case let .failure(error):  // <--- ERROR HERE
                throw error
        }
    }
}

can you send me the whole project?

1 Like

I have mailed it to you :slight_smile:

Thanks, logged as bugs://82275

Sweet, any workaround for this?

No idea, it does look like a compiler bug, but I don’t see what’s causing it. @ck might.

If I am trying to the code listed below snippet I am getting the error:

Member "<special>.success" of type "<unknown type>" is a variable but is used as a method [/blah/SharedProject/SensorService.swift (55)]

As far as I know this code should just work as I basically copied it from an existing Swift project but now it’s in a Shared Project and uses Sugar’s Http classes.

/// An enm representing a typical error for a Result<T>
public enum ResultError: Error {
    case invalid(String)
    case parseError
    case networkError
    case notFound
    case cancelled
}

/// An enum representing either a failure with an explanatory error, or a success with a result value.
public enum Result<Value> {
    case success(Value)
    case failure(ResultError)
    
    // MARK: Constructor
    
    /// Constructs a success wrapping a `value`.
    public init(value: Value) {
        self = .success(value)
    }
    
    /// Constructs a failure wrapping an `error`.
    public init(error: ResultError) {
        self = .failure(error)
    }
}


typealias LocationList = [Location]

final class SensorService {

    public static var sharedInstance: SensorService = {
        let sensorService = SensorService()
        return sensorService
    }()

    private init() {
    }

    func getAvailableSensors(completion completion: (Result<LocationList>) -> ()) {
        let request = HttpRequest(Url.UrlWithString("https://www.mocky.io/v2/5c699176370000a90a07fd6f"))
        Http.ExecuteRequest(request) { response in
            let locations = LocationList()
            completion(.success(locations)) // <-- line 55
        }
    }
}

Thanks, logged as bugs://82283

Don’t use Sugar, it’s deprecated. use Elements RTL.

Btw, I believe Result is already declared in SwiftBaseLibrary, these days?

Yes, they added a Result-type to Swift 5 but as far as I remember SwiftBaseLibrary is a RO library and it doesn’t include this Result-type.

Regarding the use of Elements RTL. I think I am already using it as I think it’s out-of-the-box included in a Swift-project in Fire when creating a Shared Project? My shared project I am having doesn’t seem to allow to add references like this. My iOS project does have libElements as reference

it does, I just double-checked.

public enum Result<Value, Error: Swift.Error> {
    case value(Value), error(Error)
}

It’s an option win the New Project dialog, yes.

Shared Projects can not have references, they only hold files. the references are on the “real” projects. Check out Shared Projects for more on this.

Cool, thanks. I was looking for a file Result.swift :slight_smile:

I don’t think there is a workaround for the compiler issue of bugs://82283, right? Anyway to access the bug centre?

bugs://82275 got closed with status fixed.

1 Like

Do you have a complete testcase for the .Success issue?

1 Like

In Xcode/Swift I would use the class listed above as listed below with the following function definition:

public static func fetchDocument(_ id: String, completion: @escaping (Result<CCommDocument>) -> ())

        session.dataTask(with: urlRequest) { data, response, error in
            guard let data = data, let _ = response as? HTTPURLResponse else { completion(.failure(ResultError.networkError)); return }
            
            do {
                guard
                    let json = try JSONSerialization.jsonObject(with: data, options: .allowFragments) as? JSONDictionary,
                    let jsonData = json["d"] as? JSONDictionary
                else {
                    completion(.failure(ResultError.parseError))
                    return
                }
                
                // TODO: Assumes that documents fetched by ID are always email templates. This will need to change based on a param in the response.
                let documentItem = XXXCommTemplate()
                documentItem.type = .emailTemplate
                documentItem.applyData(jsonData)
                completion(.success(documentItem))
            } catch {
                completion(.failure(ResultError.parseError))
            }
        }.resume()

bugs://82283 got closed with status fixed.

1 Like

Would it be possible to get a build with these fixes? :slight_smile:

Yes, I’ll send you one later today.

1 Like

Great thank you, Marc!

1 Like