What's wrong with my enum?

(wdb) #1

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
        }
    }
}
0 Likes

(marc hoffman) #2

can you send me the whole project?

1 Like

(wdb) #3

I have mailed it to you :slight_smile:

0 Likes

(RemObjects) #4

Thanks, logged as bugs://82275

0 Likes

(wdb) #5

Sweet, any workaround for this?

0 Likes

(marc hoffman) #6

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

0 Likes

(wdb) #7

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
        }
    }
}
0 Likes

(RemObjects) #8

Thanks, logged as bugs://82283

0 Likes

(marc hoffman) #9

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

0 Likes

(marc hoffman) #10

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

0 Likes

(wdb) #11

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

0 Likes

(marc hoffman) #12

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 http://docs.elementscompiler.com/Projects/SharedProjects for more on this.

0 Likes

(wdb) #13

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?

0 Likes

(RemObjects) #14

bugs://82275 got closed with status fixed.

1 Like

(Carlo Kok) #15

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

1 Like

(wdb) #16

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()
0 Likes

(RemObjects) #17

bugs://82283 got closed with status fixed.

1 Like

(wdb) #18

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

0 Likes

(marc hoffman) #19

Yes, I’ll send you one later today.

1 Like

(wdb) #20

Great thank you, Marc!

1 Like