3 ms·
> What is this ungodly mess? type API = "polls" :> Get '[JSON] [Poll] :<|> "polls" :> Capture "questio
by mrkeen 6y ago
> What is this ungodly mess?
type API = "polls" :> Get '[JSON] [Poll]
:<|> "polls" :> Capture "question_id" Int :> Get '[JSON] Poll
:<|> "polls" :> Capture "question_id" Int :> "results" :> Get '[JSON] PollResults
Delightful :D
The API is the route "/polls" accessed via GET, returning a list of Polls in JSON format
or the route "/polls/question_id" where question_id is an Int, accessed via GET, returning a Poll in JSON format
or the route "/polls/question_id/results" where question_id is an Int, accessed via GET, returning PollResults in JSON format
- runeks 6y agoSince we’re discussing syntax I’d like to question the necessity of the tick preceding [JSON]. Could we leave out this tick given that the kind of this parameter is [*]?
- pyrale 6y agoFor reasons that are not easy to explain, no. [JSON] and '[JSON] have different meanings. Here, '[JSON] does not refer to a list of json files, but to a type-level list of the mimetypes that can be returned. You could see '[JSON, PNG] instead, which would make no sense in the context of a list type declaration. The genius of Servant is that the DSL doesn't require you to know how '[] is implemented and why it's different from [], you just have to know the DSL.
- quickthrower2 6y agoYep as soon as you make it look like natural language, compilers magically become intelligent and context aware readers of prose, not the old-school pedantic expectors of syntax rules.