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

Idea: Coercible trait #1896

Closed
DemiMarie opened this Issue Feb 11, 2017 · 2 comments

Comments

Projects
None yet
3 participants
@DemiMarie
Copy link

DemiMarie commented Feb 11, 2017

GHC implements a two-paramenter type class called Coercible and a corresponding safe function coerce, with the following types:

-- Instances of this class exist only if a and b
-- have the same runtime representations AND
-- all constructors in A and B are in scope
class Coercible a b

-- | compiler built-in – not really implemented this way
coerce :: Coercible a b => a -> b
coerce = unsafeCoerce

This allows the following:

import Data.Coerce

newtype X a = X a

changeList :: [X a] -> [a]
changeList = coerce

Without coerce, this would require unsafeCoerce (GHC's version of mem::transmute) or an O(n) mapping over the list.

In Rust, the same problem can arise. It would be nice to support something like this.

@glaebhoerl

This comment has been minimized.

Copy link
Contributor

glaebhoerl commented Feb 11, 2017

It has been proposed multiple times (originally by me): #270

@Centril

This comment has been minimized.

Copy link
Contributor

Centril commented Feb 23, 2018

Closing as duplicate.

@Centril Centril closed this Feb 23, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.