Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7168743
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T14:50:30+00:00 2026-05-28T14:50:30+00:00

Imagine there is a newtype declaration: newtype T = T Int This declaration is

  • 0

Imagine there is a newtype declaration:

newtype T = T Int

This declaration is in a module but not exported. I would like to pass a value of type T to a function. Now, I can declare my own version of T using the exact same definition. The compiler will of course complain if I pass (my.T 0) to a function expecting (hidden.T 0). I will use unsafeCoerce to coerce the former to the latter. It’s mentioned here that usage is safe “between a newtype and the type that it wraps.” I would like to just check if it’s also safe in the case I described.

I know this is frowned upon and against all principles of good software practices, type theory, functional programming philosophy, ghc policies, common sense,..etc. Yet, I want to know if this will “normally” work.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T14:50:31+00:00Added an answer on May 28, 2026 at 2:50 pm

    This might be safe with the current GHC implementation, but this is not the recommended way to solve your particular problem.

    The pattern that is usually used instead is to have an internal module like this:

    module Foo.Types (T(..)) where
    
    newtype T = T Int
    

    This module is declared as un-exported in your Cabal file. Then, in the module where you want to use the type in, you import the Types module, and use the constructor directly:

    module Foo.Bla where
    
    import Foo.Types (T(..))
    
    f :: T -> Bla
    f (T int) = makeBla int
    

    Finally, you can export the opaque type however you want. For example:

    module Foo (T) where
    
    import Foo.Types (T(..))
    
    makeT :: Int -> T
    makeT = T
    

    It is possible to use coercion instead, but it would be a bad idea to rely on it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given this is a basic question, I imagine there might be duplicates, but I
I'm sure there's an easy answer to this, but I'm not really sure what
If there were such a thing I would imagine the syntax to be something
There is one thing that I do not understand... Imagine you have a text
In the Eclipse IDE (and many others I would imagine) there is a simple
I guess my description is not clear enough. So I'll try again: Imagine there
I always use dfs -get or dfs -cat, but I imagine there might be
Imagine there is a table: declare @tab table (id int, val int) insert into
I imagine there is a simple solution, but it eludes me. If you look
I just stumbled onto this SO question and was wondering if there would be

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.