
Often, we need to create a newtype that is equivalent to a given type for safety reasons. Using type synonym is useless from type safety perspective. With newtype, we have to add a "deriving" clause to it for deriving the required instances, to make it practically useful. Does it make sense, and is it possible to have something like a "clonetype" that creates a new type and derives all the instances of the parent type as well? It will be quite helpful in creating equivalent newtype synonyms quickly. Almost always, I do not use a newtype where I should just because of the inconvenience of deriving the instances. Ideally, we should just be able to say something like: clonetype MyString = String and we are good to go. What is the shortest possible way to achieve this with currently available mechanisms, if any? -harendra

Hi, On 24/09/18 12:42, Harendra Kumar wrote:
and we are good to go. What is the shortest possible way to achieve this with currently available mechanisms, if any?
What about GeneralizedNewtypeDeriving[1]? [1] https://downloads.haskell.org/~ghc/latest/docs/html/users_guide/glasgow_exts... Cheers, -- -alex https://unendli.ch/

Does it make sense, and is it possible to have something like a "clonetype" that creates a new type and derives all the instances of the parent type as well?
Presumably, the reason why you create a newtype is because you want the type system to distinguish the two types. Having automatically access to all the base type's classes may sometimes be what you want, but in other cases it hides too much of the distinction between the two types. Stefan
participants (3)
-
Alex Silva
-
Harendra Kumar
-
Stefan Monnier