watson-developer-cloud / swift-sdk

:iphone: The Watson Swift SDK enables developers to quickly add Watson Cognitive Computing services to their Swift applications.
https://watson-developer-cloud.github.io/swift-sdk/
Apache License 2.0
877 stars 222 forks source link

Investigate using OptionSet #504

Closed glennrfisher closed 7 years ago

glennrfisher commented 8 years ago

An OptionSet (or something akin to [AlchemyLanguage.Options]) may be a useful way for users to declare various options without having an exhaustive set of arguments.

For example, instead of:

public func getRankedNamedEntities(
    forURL url: String,
    knowledgeGraph: QueryParam? = nil,
    disambiguateEntities: QueryParam? = nil,
    linkedData: QueryParam? = nil,
    coreference: QueryParam? = nil,
    sentiment: QueryParam? = nil,
    quotations: QueryParam? = nil,
    structuredEntities: QueryParam? = nil,
    failure: (NSError -> Void)? = nil,
    success: Entities -> Void)
{
    // ...
}

This could instead be expressed as:

public func getRankedNamedEntities(
    forURL url: String,
    options: [Options],
    failure: (NSError -> Void)? = nil,
    success: Entities -> Void)
{
    // ...
}

Which could be called as such:

alchemyLanguage.getRankedNamedEntities(
    forURL: "...",
    options: [
        .knowledgeGraph(true),
        .coreference(false),
        ...
    ]
)
glennrfisher commented 8 years ago

Hmm... this might not work well if only a single AlchemyLanguage.Options enum was defined. Some options are only valid for particular functions (e.g. knowledgeGraph is an acceptable parameter in many of the endpoints, but coreference is not). With only a single enum, that couldn't be expressed in code and checked by the compiler.