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

2.2 Data Kinds: the code on page 29 will not work as is #30

Open
Ptival opened this issue Jun 11, 2020 · 1 comment
Open

2.2 Data Kinds: the code on page 29 will not work as is #30

Ptival opened this issue Jun 11, 2020 · 1 comment

Comments

@Ptival
Copy link

Ptival commented Jun 11, 2020

Page 29 defines a data type to be used as a data kind:

data UserType
  = User
  | Admin

Then suggests to refine some User data type:

data User = User { ... }

Unfortunately, the two cannot coexist as is, since they use the same data constructor name User. Seasoned Haskellers will not be too bothered by this, but someone a little new can be surprised by the duplicate identifier error.

(Mediocre) suggestions for fixes:

  • renaming User into something else in UserType... nothing great comes to mind, the somewhat verbose NormalUser? :\
  • explicitly mentioning that UserType should live in its own module and used qualified as UserType.User?

Not sure, it's a very minor issue! :-)

@zoranbosnjak
Copy link

Regarding the same code snippet... could you please give an example of how to use doSensitiveThings in connection with the userAdminToken. It is not clear to me how can you possibly turn Maybe (Proxy 'Admin) into a type error?

This is obviously wrong example...

{-# LANGUAGE DataKinds #-}
import Data.Proxy

data UserType = NonAdmin | Admin

data User = User { userAdminToken :: Maybe (Proxy 'Admin) }

doSensitiveThings :: Proxy 'Admin -> IO ()
doSensitiveThings _proxy = print "This is admin." -- ??

main = do
    -- Is there any other way to create users?
    let adminUser = User (Just Proxy) -- ??
        nonAdminUser = User Nothing  -- ??

    -- How to use 'doSensitiveThings' properly ??
    doSensitiveThings undefined

There should probably be data User t = ... instead of data User = .... But I am obviously missing something important about Proxy. I would appreciate a working example.

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