Skip to content

Instantly share code, notes, and snippets.

@dibrinsofor
Created April 24, 2024 05:10
Show Gist options
  • Save dibrinsofor/6d3c16c7eb718ebbf66f49ddc1cd2b93 to your computer and use it in GitHub Desktop.
Save dibrinsofor/6d3c16c7eb718ebbf66f49ddc1cd2b93 to your computer and use it in GitHub Desktop.
b2t2 table encoding
(* table *)
type 'a header = (string * 'a) list
type 'a header = string list
class ['a] table (header : 'b header) = object
val mutable header : 'b header = header
val mutable columns : 'a list = []
method columns : 'a list = columns
(* add rows, can't enforce the header *)
method add_rows (rows : 'a list) : 'a list =
columns <- rows @ columns;
columns
end;;
(* empty table - weak type *)
let empty_table = new table [];;
(* example encoding - students *)
class student (n : string) (a : int) (c: string) =
object
val mutable name : string option = Some n
val mutable age : int option = Some a
val mutable favourite_colour : string option = Some c
method name : string option = name
method set_name : string option -> unit = fun new_name -> name <- new_name
method age : int option = age
method set_age : int option -> unit = fun new_age -> age <- new_age
method favourite_colour : string option = favourite_colour
method set_favourite_colour : string option -> unit = fun new_colour -> favourite_colour <- new_colour
end;;
let bob = new student "Bob" 12 "Blue";;
let alice = new student "Alice" 17 "Green";;
let eve = new student "Eve" 13 "Red";;
let students = new table ["name"; "age"; "favourite_colour"];;
students#add_rows [bob; alice; eve];;
@dibrinsofor
Copy link
Author

table encoding in ocaml that meets the b2t2 specification but is too restrictive to type well-meaning programs.

example students table from b2t2 specification

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment