Skip to content

GitLab

  • Projects
  • Groups
  • Snippets
  • Help
    • Loading...
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
  • Sign in / Register
GHC
GHC
  • Project overview
    • Project overview
    • Details
    • Activity
    • Releases
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
    • Locked Files
  • Issues 4,251
    • Issues 4,251
    • List
    • Boards
    • Labels
    • Service Desk
    • Milestones
    • Iterations
  • Merge Requests 396
    • Merge Requests 396
  • Requirements
    • Requirements
    • List
  • CI / CD
    • CI / CD
    • Pipelines
    • Jobs
    • Schedules
  • Security & Compliance
    • Security & Compliance
    • Dependency List
    • License Compliance
  • Operations
    • Operations
    • Incidents
    • Environments
  • Analytics
    • Analytics
    • CI / CD
    • Code Review
    • Insights
    • Issue
    • Repository
    • Value Stream
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Members
    • Members
  • Collapse sidebar
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
  • Glasgow Haskell Compiler
  • GHCGHC
  • Wiki
    • Objective c
  • type safe messaging

Last edited by pumpkin Sep 07, 2010
Page history New page

type safe messaging

Using generalized data families for selectors

A simple way of representing messages that classes respond to is using a GADT data family indexed (as a type function) by the class that responds to the messages and (as a GADT output index) by the function type equivalent to the selector (but we can omit the receiver):

{-# LANGUAGE TypeFamilies, GADTs #-}

data family Selector :: * -> * -> *

send :: Selector r f -> r -> f
send = undefined

newtype NSObject = NSObject (Ptr ())

data instance Selector NSObject a where
  Hash :: Selector NSObject (IO Word)
  Class :: Selector NSObject (IO Class)
  IsKindOfClass_ :: Selector NSObject (Class -> IO Bool)
  -- ...

In practice, the Selector family might be an associated data type of a typeclass of objects, with send as a method that dispatches those selectors down to the appropriate FFI calls.

The question here is what to do about selector overlaps across classes. If we can find a nice solution for subclassing we might not need to think about overlap between methods on superclasses. Maybe wrapping the whole thing in a class would allow us to deal with the overlap, and the methods would also allow us to use camel case on the method names. As all class objects themselves will probably just be newtypes around pointers, maybe GeneralizedNewtypeDeriving can help us avoid too much boilerplate too?

Clone repository

GHC Home
GHC User's Guide

Joining In

Newcomers info
Mailing Lists & IRC
The GHC Team

Documentation

GHC Status Info
Working conventions
Building Guide
Debugging
Commentary

Wiki

Title Index
Recent Changes