Why is this code an 'unused variable self'

Consider the following code:

class dom_listener_list =
  object (self)
    val mutable the_list : Dom_html.event_listener_id list = []

    method add_event_listener (typ : 'a Dom_html.Event.typ) (handler : Dom_html.pointerEvent Js.t -> bool Js.t) : unit =
      let listener = Dom_html.addEventListener Dom_html.window typ (Dom.handler handler) Js._true in
      the_list <- listener :: the_list

    method remove_all_listeners =
      List.iter ~f:Dom_html.removeEventListener the_list;
      the_list <- []
  end

The compiler complains:

15 |   object (self)
              ^^^^^^
Error (warning 27 [unused-var-strict]): unused variable self.

Now, this confuses me: isn’t the use of the_list an implicit use of self ? Without using self, how we are accessing the_list ?

Implicit use of the self object doesn’t count as an use of the explicit name for self, which is needed for methods (and not for instance variables):

let o = object(a_random_name_for_self)
  method one = []
  method succ x = [x]
  method two = a_random_name_for_self#succ a_random_name_for_self#one
end
1 Like