say-my-name: Require explicit type application for some type variables.

[ bsd3, library, types ] [ Propose Tags ] [ Report a vulnerability ]

Require explicit type application for some type variables.


[Skip to Readme]

Downloads

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees

Candidates

  • No Candidates
Versions [RSS] 0.1.0.0
Dependencies base (>=4.7 && <5) [details]
License BSD-3-Clause
Copyright 2020 Matt Noonan
Author Matt Noonan
Maintainer matt.noonan@gmail.com
Category Types
Home page https://github.com/matt-noonan/say-my-name#readme
Source repo head: git clone https://github.com/githubuser/say-my-name
Uploaded by mnoonan at 2020-11-18T17:15:48Z
Distributions NixOS:0.1.0.0
Downloads 256 total (9 in the last 30 days)
Rating (no votes yet) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs available [build log]
Last success reported on 2020-11-18 [all 1 reports]

Readme for say-my-name-0.1.0.0

[back to package description]

say-my-name

This is a small, slightly cursed package that lets you require explicit type applications for certain type variables. Blame @taylorfausak for asking if it could be done, but not if it should be done!

To force explicit type annotations on a type variable a, add a new type variable a_ and a constraint MustName a "a" a_:

-- NOTE: requires AllowAmbiguousTypes to define!
sayMyNameId :: forall a_ a. (MustName a "a" a_) => a -> a
sayMyNameId = id

This constraint means "the user must supply a value for a via an explicit type annotation that specifies a_". The string "a" is used to make nice error messages:

λ> sayMyNameId "test"
sayMyNameId "test"

<interactive>:57:1: error:
    • The type parameter `a` must be supplied by an explicit type application, even if it could be inferred.
    • When checking the inferred type
        it :: forall t.
              (Break (TypeError ...) t, Data.String.IsString (SayMyName_ t)) =>
              SayMyName_ t
λ> sayMyNameId "test" :: String
sayMyNameId "test" :: String

<interactive>:58:1: error:
    • The type parameter `a` must be supplied by an explicit type application, even if it could be inferred.
    • In the expression: sayMyNameId "test" :: String
      In an equation for ‘it’: it = sayMyNameId "test" :: String
λ> sayMyNameId ("test" :: String)
sayMyNameId ("test" :: String)

<interactive>:59:1: error:
    • The type parameter `a` must be supplied by an explicit type application, even if it could be inferred.
    • In the expression: sayMyNameId ("test" :: String)
      In an equation for ‘it’: it = sayMyNameId ("test" :: String)
λ> sayMyNameId @String "test"
sayMyNameId @String "test"
"test"

You can also augment the error message with a usage example:

-- NOTE: requires AllowAmbiguousTypes to define!
sayMyNameId :: forall a_ a. (MustNameEx a "a" a_ "sayMyNameId @String \"hello, world!\"") => a -> a
sayMyNameId = id
λ> sayMyNameId "test"

<interactive>:36:1: error:
    • The type parameter `a` must be supplied by an explicit type application, even if it could be inferred. For example: sayMyNameId @String "hello, world!"
    • In the expression: sayMyNameId "test"
      In an equation for ‘it’: it = sayMyNameId "test"