def initialize(type_id, type_string, box) @type_id = type_id if ts = @@interned_type_strings[type_string]? @type_string = ts else @@interned_type_strings[type_string] = type_string @type_string = type_string end @box = box end end
class SafeBoxer(T) def self.box(object : T) : SafeBox SafeBox.new(T.crystal_type_id, T.to_s, Box(T).box(object)) end
def self.unbox(safe_box : SafeBox) if safe_box.type_id != T.crystal_type_id raise "Tried to unbox a SafeBox of #{safe_box.type_string} as a #{T}" else Box(T).unbox(safe_box.box) end end end