Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Abiguous use of operator/function with enum #103

Closed
BenchR267 opened this issue Mar 8, 2017 · 4 comments
Closed

Abiguous use of operator/function with enum #103

BenchR267 opened this issue Mar 8, 2017 · 4 comments

Comments

@BenchR267
Copy link

BenchR267 commented Mar 8, 2017

Hi!

I experienced a small problem when using enums as ValueTypes. I have an enum in a project defined as the following:

enum Week: Int {
    case all = 0, even, odd
}

I implemented the ValueType extension as the following:

extension Week: ValueType {
    public static func value(from object: Any) throws -> Week {
        guard let raw = object as? RawValue else {
            throw MarshalError.typeMismatch(expected: RawValue.self, actual: type(of: object))
        }
        guard let week = Week(rawValue: raw) else {
            throw Error.outOfBounds(raw)
        }
        return week
    }
}

When using this in a type that has a Week property, I get the compiler error 'ambiguous use of X' where X is either <| or value(for:).

I fixed it by using this code in my type that is using Week, but the solution is not that satisfiying. Do you have any other ideas?

let rawWeek: Int = try object <| "week"
guard let week = Week(rawValue: rawWeek) else {
    throw Week.Error.outOfBounds(rawWeek)
}
self.week = week

Thanks in advance!

@bwhiteley
Copy link
Contributor

@BenchR267 enums with raw values are automatically ValueTypes.

Look for RawRepresentable in MarshaledObject.swift to see how this is done.

@BenchR267
Copy link
Author

Ah thanks, that explains the message. In this case this works great, but I have another enum with a slightly more complex mapping logic. Is it somehow possible to overwrite the behaviour?

@bwhiteley
Copy link
Contributor

I can't think of a way to do that short of what you're already doing (pull out the raw value and do the mapping). We would need to add something like this to achieve what you want:
#73 (comment)

@BenchR267
Copy link
Author

Yeah, that would solve this case. Thanks for your answers! I'll close this issue since my original question is answered. 😸

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants