// a demo on OOP staload "ATS/reference.sats" // class ob = { super: // no super classes method MSGcopy : obj (myclass) method MSGprint : unit } val supOb : sup (ob) = super { super: // none method MSGcopy = myself } class eq = { super: // no super classes method MSGeq (obj (myclass)): Bool // binary method MSGneq (obj (myclass)): Bool // binary } val supEq: sup (eq) = super { super: // no super classes method MSGeq (other) = not (myself # (MSGneq other)) method MSGneq (other) = not (myself # (MSGeq other)) } class ip = { // integer pair super: ob, eq method MSGgetfst : Int method MSGsetfst (Int) : unit method MSGgetsnd : Int method MSGsetsnd (Int) : unit } datatype color = R | B class cip = { // colored integer pair super: ip method MSGgetcolor : color method MSGsetcolor (color) : unit } // obj : cls -> type fun newIP (x: Int, y: Int): obj (ip) = let val xref: ref Int = ref x and yref: ref Int = ref y in object { super: supOb, supEq method MSGgetfst = !xref method MSGsetfst (x') = xref := x' method MSGgetsnd = !yref method MSGsetsnd (y') = yref := y' method* MSGcopy = newIP (!xref, !yref) // support no inheritance method MSGprint = begin print_string "("; print_int !xref; print_string ", "; print_int !yref; print_string ")" end } end fun printIP (O: obj (ip)): unit = begin print_string "("; print_int (O # MSGgetfst); print_string ", "; print_int (O # MSGgetsnd); print_string ")" end fun swapIP (O: obj (ip)): unit = let val x = O # MSGgetfst val y = O # MSGgetsnd in O # (MSGsetfst y); O # (MSGsetsnd x) end // my_ip_1 and my_ip_2 have difference states val my_ip_1 = newIP (6, 9) val my_ip_2 = my_ip_1 # MSGcopy val '() = begin my_ip_1 # MSGprint; print_newline (); my_ip_2 # MSGprint; print_newline (); swapIP my_ip_1; my_ip_1 # MSGprint; print_newline (); my_ip_2 # MSGprint; print_newline (); end