Skip to content

Idea: Coercible trait #1896

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

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

Idea: Coercible trait #1896

DemiMarie opened this issue Feb 11, 2017 · 2 comments
Labels
T-lang Relevant to the language team, which will review and decide on the RFC. T-libs-api Relevant to the library API team, which will review and decide on the RFC.

Comments

@DemiMarie
Copy link

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
Copy link
Contributor

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

@Centril Centril added T-lang Relevant to the language team, which will review and decide on the RFC. T-libs-api Relevant to the library API team, which will review and decide on the RFC. labels Feb 23, 2018
@Centril
Copy link
Contributor

Centril commented Feb 23, 2018

Closing as duplicate.

@Centril Centril closed this as completed Feb 23, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T-lang Relevant to the language team, which will review and decide on the RFC. T-libs-api Relevant to the library API team, which will review and decide on the RFC.
Projects
None yet
Development

No branches or pull requests

3 participants