![Page 2: Writing a native Mac GUI application in HaskellWriting a native Mac GUI application in Haskell Nikolas Mayr BOBKonf2017 1 Agenda • JSON Class Generator](https://reader036.vdocuments.net/reader036/viewer/2022062414/5ebf4c9614621f356e1b4c45/html5/thumbnails/2.jpg)
Agenda• JSON Class Generator
• Joining the worlds
• Architecture
• In practice
• Retrospective
2
![Page 3: Writing a native Mac GUI application in HaskellWriting a native Mac GUI application in Haskell Nikolas Mayr BOBKonf2017 1 Agenda • JSON Class Generator](https://reader036.vdocuments.net/reader036/viewer/2022062414/5ebf4c9614621f356e1b4c45/html5/thumbnails/3.jpg)
JSON Class Generator
• Mac Application
• ~15.000 lines of Haskell
• ~ 3.000* lines of Objective-C
• Mac App Store
• Web ⬇
• Sandboxed
• lifted.software
3
![Page 4: Writing a native Mac GUI application in HaskellWriting a native Mac GUI application in Haskell Nikolas Mayr BOBKonf2017 1 Agenda • JSON Class Generator](https://reader036.vdocuments.net/reader036/viewer/2022062414/5ebf4c9614621f356e1b4c45/html5/thumbnails/4.jpg)
Demo
4
![Page 5: Writing a native Mac GUI application in HaskellWriting a native Mac GUI application in Haskell Nikolas Mayr BOBKonf2017 1 Agenda • JSON Class Generator](https://reader036.vdocuments.net/reader036/viewer/2022062414/5ebf4c9614621f356e1b4c45/html5/thumbnails/5.jpg)
5
![Page 6: Writing a native Mac GUI application in HaskellWriting a native Mac GUI application in Haskell Nikolas Mayr BOBKonf2017 1 Agenda • JSON Class Generator](https://reader036.vdocuments.net/reader036/viewer/2022062414/5ebf4c9614621f356e1b4c45/html5/thumbnails/6.jpg)
Recap
• Define Classes/Enums/Methods
• Export Objective-C code
• Use code to serialize/deserialize JSON
• Drop in (HTTP-)Client to connect to Service
6
![Page 7: Writing a native Mac GUI application in HaskellWriting a native Mac GUI application in Haskell Nikolas Mayr BOBKonf2017 1 Agenda • JSON Class Generator](https://reader036.vdocuments.net/reader036/viewer/2022062414/5ebf4c9614621f356e1b4c45/html5/thumbnails/7.jpg)
How Did I Write It?
• Hardcoded types in Haskell source (2014)
• Command Line Tool (2015)
• Mac/Cocoa NSDocument based GUI App (2016)
7
![Page 8: Writing a native Mac GUI application in HaskellWriting a native Mac GUI application in Haskell Nikolas Mayr BOBKonf2017 1 Agenda • JSON Class Generator](https://reader036.vdocuments.net/reader036/viewer/2022062414/5ebf4c9614621f356e1b4c45/html5/thumbnails/8.jpg)
Evolution
• Hardcoded types in HS source (2014)
• Command Line Tool (2015)
• Mac/Cocoa NSDocument based GUI App (2016)
8
> api --lang objc --def api.json --out ./src/api
![Page 9: Writing a native Mac GUI application in HaskellWriting a native Mac GUI application in Haskell Nikolas Mayr BOBKonf2017 1 Agenda • JSON Class Generator](https://reader036.vdocuments.net/reader036/viewer/2022062414/5ebf4c9614621f356e1b4c45/html5/thumbnails/9.jpg)
Cutting Edge
• Hardcoded types in HS source (2014)
• Command Line Tool (2015)
• Mac/Cocoa NSDocument based GUI App (2016)
9
![Page 10: Writing a native Mac GUI application in HaskellWriting a native Mac GUI application in Haskell Nikolas Mayr BOBKonf2017 1 Agenda • JSON Class Generator](https://reader036.vdocuments.net/reader036/viewer/2022062414/5ebf4c9614621f356e1b4c45/html5/thumbnails/10.jpg)
Joining the Worlds
10
![Page 11: Writing a native Mac GUI application in HaskellWriting a native Mac GUI application in Haskell Nikolas Mayr BOBKonf2017 1 Agenda • JSON Class Generator](https://reader036.vdocuments.net/reader036/viewer/2022062414/5ebf4c9614621f356e1b4c45/html5/thumbnails/11.jpg)
How to Build the UI• Non-native UI
• Web
• Cross platform UI Libraries (GTK, QT)
• Native UI
• Cocoa/ObjC UI + ObjC model -> call CLT
• Cocoa/ObjC UI + HS model (together in one app)
11
![Page 12: Writing a native Mac GUI application in HaskellWriting a native Mac GUI application in Haskell Nikolas Mayr BOBKonf2017 1 Agenda • JSON Class Generator](https://reader036.vdocuments.net/reader036/viewer/2022062414/5ebf4c9614621f356e1b4c45/html5/thumbnails/12.jpg)
Adding Some Glue• Communicate to HS via FFI
• c2hs
• Bindings (HoC, ObjectiveHaskell)
• language-c-inline
• Others (Apache Thrift)
• Client-Server (no network)12
![Page 13: Writing a native Mac GUI application in HaskellWriting a native Mac GUI application in Haskell Nikolas Mayr BOBKonf2017 1 Agenda • JSON Class Generator](https://reader036.vdocuments.net/reader036/viewer/2022062414/5ebf4c9614621f356e1b4c45/html5/thumbnails/13.jpg)
How Do We Connect ObjC to a Server that
Speaks JSON?
13
![Page 14: Writing a native Mac GUI application in HaskellWriting a native Mac GUI application in Haskell Nikolas Mayr BOBKonf2017 1 Agenda • JSON Class Generator](https://reader036.vdocuments.net/reader036/viewer/2022062414/5ebf4c9614621f356e1b4c45/html5/thumbnails/14.jpg)
JSON Code Generator!
14
![Page 15: Writing a native Mac GUI application in HaskellWriting a native Mac GUI application in Haskell Nikolas Mayr BOBKonf2017 1 Agenda • JSON Class Generator](https://reader036.vdocuments.net/reader036/viewer/2022062414/5ebf4c9614621f356e1b4c45/html5/thumbnails/15.jpg)
~3000 LoC + ~13000 LoC
~16000 LoC
15
ObjC Generated
⌨ ⚙
![Page 16: Writing a native Mac GUI application in HaskellWriting a native Mac GUI application in Haskell Nikolas Mayr BOBKonf2017 1 Agenda • JSON Class Generator](https://reader036.vdocuments.net/reader036/viewer/2022062414/5ebf4c9614621f356e1b4c45/html5/thumbnails/16.jpg)
How to Compile• Compiled languages
• Xcode, not Terminal
• Build phases
• HS to static library "-staticlib" => liba.a (ext. build tool)
• Generate API from specification file
• Compile ObjC (+Storyboards), copy asses/frameworks
• Link with liba.a libiconv
16
![Page 17: Writing a native Mac GUI application in HaskellWriting a native Mac GUI application in Haskell Nikolas Mayr BOBKonf2017 1 Agenda • JSON Class Generator](https://reader036.vdocuments.net/reader036/viewer/2022062414/5ebf4c9614621f356e1b4c45/html5/thumbnails/17.jpg)
Compile Attempts
17
![Page 18: Writing a native Mac GUI application in HaskellWriting a native Mac GUI application in Haskell Nikolas Mayr BOBKonf2017 1 Agenda • JSON Class Generator](https://reader036.vdocuments.net/reader036/viewer/2022062414/5ebf4c9614621f356e1b4c45/html5/thumbnails/18.jpg)
Application Launch
• Two runtimes to start
• Bound threads
• FFI
• -threaded
18
![Page 19: Writing a native Mac GUI application in HaskellWriting a native Mac GUI application in Haskell Nikolas Mayr BOBKonf2017 1 Agenda • JSON Class Generator](https://reader036.vdocuments.net/reader036/viewer/2022062414/5ebf4c9614621f356e1b4c45/html5/thumbnails/19.jpg)
Architecture
19
![Page 20: Writing a native Mac GUI application in HaskellWriting a native Mac GUI application in Haskell Nikolas Mayr BOBKonf2017 1 Agenda • JSON Class Generator](https://reader036.vdocuments.net/reader036/viewer/2022062414/5ebf4c9614621f356e1b4c45/html5/thumbnails/20.jpg)
Server (Haskell)
Client
Service
ApiDocument : NSDocument
WindowController
Document
RAC
20
![Page 21: Writing a native Mac GUI application in HaskellWriting a native Mac GUI application in Haskell Nikolas Mayr BOBKonf2017 1 Agenda • JSON Class Generator](https://reader036.vdocuments.net/reader036/viewer/2022062414/5ebf4c9614621f356e1b4c45/html5/thumbnails/21.jpg)
Server (Haskell)
Client
Service
ApiDocument : NSDocument
WindowController
Document
RAC
21
![Page 22: Writing a native Mac GUI application in HaskellWriting a native Mac GUI application in Haskell Nikolas Mayr BOBKonf2017 1 Agenda • JSON Class Generator](https://reader036.vdocuments.net/reader036/viewer/2022062414/5ebf4c9614621f356e1b4c45/html5/thumbnails/22.jpg)
Server (Haskell)
Client
Service
ApiDocument : NSDocument
WindowController
Document
RAC
22
![Page 23: Writing a native Mac GUI application in HaskellWriting a native Mac GUI application in Haskell Nikolas Mayr BOBKonf2017 1 Agenda • JSON Class Generator](https://reader036.vdocuments.net/reader036/viewer/2022062414/5ebf4c9614621f356e1b4c45/html5/thumbnails/23.jpg)
Client-Server (HS)• Stores model (synchronized access)
• Serves JSON API
• No HTTP needed (server hardwired)
• Blocking calls
• No latency
• One instance per Document23
![Page 24: Writing a native Mac GUI application in HaskellWriting a native Mac GUI application in Haskell Nikolas Mayr BOBKonf2017 1 Agenda • JSON Class Generator](https://reader036.vdocuments.net/reader036/viewer/2022062414/5ebf4c9614621f356e1b4c45/html5/thumbnails/24.jpg)
Haskell Server
24
![Page 25: Writing a native Mac GUI application in HaskellWriting a native Mac GUI application in Haskell Nikolas Mayr BOBKonf2017 1 Agenda • JSON Class Generator](https://reader036.vdocuments.net/reader036/viewer/2022062414/5ebf4c9614621f356e1b4c45/html5/thumbnails/25.jpg)
Model• Undo Manager
• Outline View
• Encoding Callbacks
• Continuation Passing Style
• Performance (outline view, json serialization)
25
![Page 26: Writing a native Mac GUI application in HaskellWriting a native Mac GUI application in Haskell Nikolas Mayr BOBKonf2017 1 Agenda • JSON Class Generator](https://reader036.vdocuments.net/reader036/viewer/2022062414/5ebf4c9614621f356e1b4c45/html5/thumbnails/26.jpg)
Server (Haskell)
Client
Service
ApiDocument : NSDocument
WindowController
Document
RAC
26
![Page 27: Writing a native Mac GUI application in HaskellWriting a native Mac GUI application in Haskell Nikolas Mayr BOBKonf2017 1 Agenda • JSON Class Generator](https://reader036.vdocuments.net/reader036/viewer/2022062414/5ebf4c9614621f356e1b4c45/html5/thumbnails/27.jpg)
(ObjC) Client-Server• Powers service
• Serializes requests (Object -> JSONValue -> char *)
• Memory management
• Deserializes responses (NSData * -> JSONValue -> Object)
• Logs errors and exceptions
• Updates ModelRevisionCounter
27
![Page 28: Writing a native Mac GUI application in HaskellWriting a native Mac GUI application in Haskell Nikolas Mayr BOBKonf2017 1 Agenda • JSON Class Generator](https://reader036.vdocuments.net/reader036/viewer/2022062414/5ebf4c9614621f356e1b4c45/html5/thumbnails/28.jpg)
(ObjC) Client-Server• Powers service
• Serializes requests (Object -> JSONValue -> char *)
• Memory management
• Deserializes responses (NSData * -> JSONValue -> Object)
• Logs errors and exceptions
• Updates ModelRevisionCounter
28
![Page 29: Writing a native Mac GUI application in HaskellWriting a native Mac GUI application in Haskell Nikolas Mayr BOBKonf2017 1 Agenda • JSON Class Generator](https://reader036.vdocuments.net/reader036/viewer/2022062414/5ebf4c9614621f356e1b4c45/html5/thumbnails/29.jpg)
Memory Management• ObjC ARC
• HS GC
• Avoidance
• Request: FFI bracket
• Response: NSData allocation, ARC
• Problem: wasting memory (make model strict)29
![Page 30: Writing a native Mac GUI application in HaskellWriting a native Mac GUI application in Haskell Nikolas Mayr BOBKonf2017 1 Agenda • JSON Class Generator](https://reader036.vdocuments.net/reader036/viewer/2022062414/5ebf4c9614621f356e1b4c45/html5/thumbnails/30.jpg)
Exception Handling
• Error reporting
• What happens if ObjC crashes?
• What happens if HS crashes?
• Problem: Lost exceptions
30
![Page 31: Writing a native Mac GUI application in HaskellWriting a native Mac GUI application in Haskell Nikolas Mayr BOBKonf2017 1 Agenda • JSON Class Generator](https://reader036.vdocuments.net/reader036/viewer/2022062414/5ebf4c9614621f356e1b4c45/html5/thumbnails/31.jpg)
Server (Haskell)
Client
Service
ApiDocument : NSDocument
WindowController
Document
RAC
31
![Page 32: Writing a native Mac GUI application in HaskellWriting a native Mac GUI application in Haskell Nikolas Mayr BOBKonf2017 1 Agenda • JSON Class Generator](https://reader036.vdocuments.net/reader036/viewer/2022062414/5ebf4c9614621f356e1b4c45/html5/thumbnails/32.jpg)
Service
32
![Page 33: Writing a native Mac GUI application in HaskellWriting a native Mac GUI application in Haskell Nikolas Mayr BOBKonf2017 1 Agenda • JSON Class Generator](https://reader036.vdocuments.net/reader036/viewer/2022062414/5ebf4c9614621f356e1b4c45/html5/thumbnails/33.jpg)
Server (Haskell)
Client
Service
ApiDocument : NSDocument
WindowController
Document
RAC
33
![Page 34: Writing a native Mac GUI application in HaskellWriting a native Mac GUI application in Haskell Nikolas Mayr BOBKonf2017 1 Agenda • JSON Class Generator](https://reader036.vdocuments.net/reader036/viewer/2022062414/5ebf4c9614621f356e1b4c45/html5/thumbnails/34.jpg)
NSDocument RAC Write
34
![Page 35: Writing a native Mac GUI application in HaskellWriting a native Mac GUI application in Haskell Nikolas Mayr BOBKonf2017 1 Agenda • JSON Class Generator](https://reader036.vdocuments.net/reader036/viewer/2022062414/5ebf4c9614621f356e1b4c45/html5/thumbnails/35.jpg)
NSDocument RAC Read
35
![Page 36: Writing a native Mac GUI application in HaskellWriting a native Mac GUI application in Haskell Nikolas Mayr BOBKonf2017 1 Agenda • JSON Class Generator](https://reader036.vdocuments.net/reader036/viewer/2022062414/5ebf4c9614621f356e1b4c45/html5/thumbnails/36.jpg)
Server (Haskell)
Client
Service
ApiDocument : NSDocument
WindowController
Document
RAC
36
![Page 37: Writing a native Mac GUI application in HaskellWriting a native Mac GUI application in Haskell Nikolas Mayr BOBKonf2017 1 Agenda • JSON Class Generator](https://reader036.vdocuments.net/reader036/viewer/2022062414/5ebf4c9614621f356e1b4c45/html5/thumbnails/37.jpg)
User Interface
37
![Page 38: Writing a native Mac GUI application in HaskellWriting a native Mac GUI application in Haskell Nikolas Mayr BOBKonf2017 1 Agenda • JSON Class Generator](https://reader036.vdocuments.net/reader036/viewer/2022062414/5ebf4c9614621f356e1b4c45/html5/thumbnails/38.jpg)
Server (Haskell)
Client
Service
ApiDocument : NSDocument
WindowController
Document
RAC
38
Unidirectional Data Flow
![Page 39: Writing a native Mac GUI application in HaskellWriting a native Mac GUI application in Haskell Nikolas Mayr BOBKonf2017 1 Agenda • JSON Class Generator](https://reader036.vdocuments.net/reader036/viewer/2022062414/5ebf4c9614621f356e1b4c45/html5/thumbnails/39.jpg)
In Practice
39
![Page 40: Writing a native Mac GUI application in HaskellWriting a native Mac GUI application in Haskell Nikolas Mayr BOBKonf2017 1 Agenda • JSON Class Generator](https://reader036.vdocuments.net/reader036/viewer/2022062414/5ebf4c9614621f356e1b4c45/html5/thumbnails/40.jpg)
Development Tools• Xcode, -debugger, for ObjC
• Haskell for Mac (Haskell.app IDE) for HS
• Sublime: for shell scrips (Haskell Shelly)
• Instruments
• Threadscope (performance), RTS opts MEM
40
![Page 41: Writing a native Mac GUI application in HaskellWriting a native Mac GUI application in Haskell Nikolas Mayr BOBKonf2017 1 Agenda • JSON Class Generator](https://reader036.vdocuments.net/reader036/viewer/2022062414/5ebf4c9614621f356e1b4c45/html5/thumbnails/41.jpg)
Debugging
41ObjC HS
![Page 42: Writing a native Mac GUI application in HaskellWriting a native Mac GUI application in Haskell Nikolas Mayr BOBKonf2017 1 Agenda • JSON Class Generator](https://reader036.vdocuments.net/reader036/viewer/2022062414/5ebf4c9614621f356e1b4c45/html5/thumbnails/42.jpg)
Testing
• Unit tests for ObjC and HS
• UI tests
• Server interface tests
42
![Page 43: Writing a native Mac GUI application in HaskellWriting a native Mac GUI application in Haskell Nikolas Mayr BOBKonf2017 1 Agenda • JSON Class Generator](https://reader036.vdocuments.net/reader036/viewer/2022062414/5ebf4c9614621f356e1b4c45/html5/thumbnails/43.jpg)
Retrospective
43
![Page 44: Writing a native Mac GUI application in HaskellWriting a native Mac GUI application in Haskell Nikolas Mayr BOBKonf2017 1 Agenda • JSON Class Generator](https://reader036.vdocuments.net/reader036/viewer/2022062414/5ebf4c9614621f356e1b4c45/html5/thumbnails/44.jpg)
Challenges
• Getting started
• Building, Setup, Architecture
• Debugging
• Persistence. All problems solvable.
44
![Page 45: Writing a native Mac GUI application in HaskellWriting a native Mac GUI application in Haskell Nikolas Mayr BOBKonf2017 1 Agenda • JSON Class Generator](https://reader036.vdocuments.net/reader036/viewer/2022062414/5ebf4c9614621f356e1b4c45/html5/thumbnails/45.jpg)
Reflecting• Haskell Language
• Two Languages in one app
• Domain specific
• Cross platform
• Swift
• Cross platform UI Libraries45
![Page 46: Writing a native Mac GUI application in HaskellWriting a native Mac GUI application in Haskell Nikolas Mayr BOBKonf2017 1 Agenda • JSON Class Generator](https://reader036.vdocuments.net/reader036/viewer/2022062414/5ebf4c9614621f356e1b4c45/html5/thumbnails/46.jpg)
Where to get started• Tim Scheffler's blog (1) (2) (3)
• FFI section of HS2010 report
• Dropbox CppCon2014 Cross Platform (1) (2)
• Libs: language-c-inline (talk) (github) (hackage)
• Haskell for Mac IDE
46
![Page 47: Writing a native Mac GUI application in HaskellWriting a native Mac GUI application in Haskell Nikolas Mayr BOBKonf2017 1 Agenda • JSON Class Generator](https://reader036.vdocuments.net/reader036/viewer/2022062414/5ebf4c9614621f356e1b4c45/html5/thumbnails/47.jpg)
Questions?
47