haskell-servant / haskell-servant/servant

Question: Dependent return types of routes

Open
#1,294 7 comments 0 reactions 0 assignees View on GitHub
Dominant language
Haskell
Stars
2k
Forks
427
Avg merge
2d 23h
Merged PRs (30d)
5

Description

Is it possible to do something like this?

```haskell
type API b = "other-stuff" :> Capture "bool" (SBool b) :> Get '[PlainText] (IntOrString b)

type familty IntOrString (b :: Bool) :: Type where
IntOrString 'False = Int
IntOrString 'True = String

thing :: Server (API b)
thing b =
case b of
SFalse -> pure 5
STrue -> pure "lol"
```
where `SBool` and friends are the traditional singletonised `Bool`

I originally tried to do
```haskell
type API = "other-stuff" :> forall b. Capture "bool" (SBool b) :> Get '[PlainText] (IntOrString b)
```
but `ghc` yelled at me for impredicative types. Maybe this could be worked around by making a `newtype` and instantiating some `servant` type families (?), but I'm not sure where I should be looking, and even if it works, it seems like I would need different newtypes for every endpoint :/

Context:
I have two types of things, and their types contain different data, but all queries on them are very similar (I can easily express it with type/data families).

Contributor guide

Open the contributing guide

Research direction

No repository files or tests are named. Start by examining the Servant API and Server types involved in the shown singleton Bool and type-family definitions, then verify whether the dependent return type can be represented without impredicative types; done means establishing a supported approach or documenting why it is not feasible.

Written by the indexing model from the issue text.

Assessment

Tech stack
haskell
Domain
api, backend-api-design
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.