Skip to content
This repository has been archived by the owner on Sep 11, 2022. It is now read-only.

chessai/shwifty

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

84 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Shwifty

Generate Swift types from Haskell types

Examples:

A simple sum type

data SumType = Sum1 | Sum2 | Sum3
getShwifty ''SumType
enum SumType {
    case sum1
    case sum2
    case sum3
}

A simple product type

data ProductType = ProductType { x :: Int, y :: Int }
getShwifty ''ProductType
struct ProductType {
    let x: Int
    let y: Int
}

A sum type with type variables

data SumType a b = SumL a | SumR b
getShwifty ''SumType
enum SumType<A, B> {
    case sumL(A)
    case sumR(B)
}

A product type with type variables

data ProductType a b = ProductType 
  { aField :: a
  , bField :: b 
  }
getShwifty ''ProductType
struct ProductType<A, B> {
    let aField: A
    let bField: B
}

A newtype

newtype Newtype a = Newtype { getNewtype :: a }
getShwifty ''Newtype
struct Newtype<A> {
    let getNewtype: A
}

A type with a function field

newtype Endo a = Endo { appEndo :: a -> a }
getShwifty ''Endo
struct Endo<A> {
    let appEndo: ((A) -> A)
}

A weird type with nested fields. Also note the Result's types being flipped from that of the Either.

data YouveGotProblems a b = YouveGotProblems 
  { field1 :: Maybe (Maybe (Maybe a))
  , field2 :: Either (Maybe a) (Maybe b) 
  }
getShwifty ''YouveGotProblems
struct YouveGotProblems<A, B> {
    let field1: A???
    let field2: Result<B?, A?>
}

A type with polykinded type variables

data PolyKinded (a :: k) = PolyKinded
getShwifty ''PolyKinded
struct PolyKinded<A> { }

A sum type where constructors might be records

data SumType a b (c :: k) 
  = Sum1 Int a (Maybe b) 
  | Sum2 b 
  | Sum3 { x :: Int, y :: Int }
getShwifty ''SumType
enum SumType<A, B, C> {
  case field1(Int, A, B?)
  case field2(B)
  case field3(_ x: Int, _ y: Int)
}

A type containing another type with instance generated by 'getShwifty'

newtype MyFirstType a = MyFirstType { getMyFirstType :: a }
getShwifty ''MyFirstType

data Contains a = Contains 
  { x :: MyFirstType Int
  , y :: MyFirstType a 
  }
getShwifty ''Contains
struct MyFirstType<A> {
  let getMyFirstType: A
}

struct Contains<A> {
  let x: MyFirstType<Int>
  let y: MyFirstType<A>
}