Skip to content

Proposal: Compiler-Solved TypeName Constraint #3787

@eric-corumdigital

Description

@eric-corumdigital
class TypeName (a ∷ Type) (name ∷ Symbol) | a → name, name → a

For all types there is an instance of TypeName which bijectively maps types to their fully-qualified name.

A higher-order example is:

TypeName (Maybe Int) "(Data.Maybe.Maybe Prim.Int)"

Two reasons I know of to have this.

  1. Dynamic.
foreign import data Any ∷ Type

data Dynamic = Dynamic String Any

toDynamic ∷ ∀ a name. IsSymbol name ⇒ TypeName a name ⇒ a → Dynamic
toDynamic x = Dynamic (reflectSymbol (SProxy∷_ name)) (unsafeCoerce x)

fromDynamic ∷ ∀ a name. IsSymbol name ⇒ TypeName a name ⇒ Dynamic → Maybe a
fromDynamic (Dynamic name x)
  | name == reflectSymbol (SProxy∷_ name) = Just (unsafeCoerce x)
  | otherwise = Nothing
  1. Efficient type-level Type sets (because rows already have bespoke solvers). Every label is the type name of its type. For example:
( "(Data.Maybe.Maybe Prim.Int)" ∷ Maybe Int, "Prim.Int" ∷ Int )

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions