Coder Social home page Coder Social logo

class.el's Introduction

class.el --- an OOP System in Emacs Lisp

class.el is an OOP system in Emacs Lisp. Its syntax comes from Python.

A Simple Example

Define a class My-Class:

     (class My-Class ()

       (defun init (self arg)
         "This method initialize the instance"
         (@ self 'arg arg))

       (defun set-arg (self new-arg)
         "Set arg"
         (@ self 'arg new-arg))

       (defun print-arg (self)
         "Print out the member `arg'"
         (message (@ self 'arg))))

My-Class is now defined as a function, which can be called to create and initialize a new instance:

    (setq obj (My-Class "hello"))
    (@ obj 'arg)
    => "hello"

obj is now an instance of My-Class. Call the method set-arg to change its initial value of arg:

    (@ obj 'set-arg (concat (@ obj 'arg) " world"))

Call the method print-arg to print out arg:

    (@ obj 'print-arg)
    => "hello world"

Class Definition Syntax

    (class NAME BASES &optional DOC &rest SLOTS)

See more information about the definition of class, please C-h f class.

Inheritance

Define class A that inherits from class B:

    (class A (B)
      ...)

This library supports multi inheritance as well. Class A with multiple bases B, C and D looks like this:

    (class A (B C D)
      ...)

Note: The method resolution order is depth-first. For example:

    (class O ()
      (defun foo (self) "from class O"))
    (class B (O))
    (class C (O)
      (defun foo (self) "from class C"))
    (class D (O)
      (defun init (self)))
    (class A (B C D))

    (setq a (A))
    (@ a 'foo)
    => "from class O"

Since the search order of method foo is B, O, C, D, the foo called above is from class O.

Custom classes are supposed to inherit from the built-in class Object, which provides some useful methods such as get-member, default init, etc.

Access to Members

Like other OOP languages, class members can be private, protected or public. To specify the access level for class members, you can use the following form:

(MODIFIER
  SLOTS...)

where MODIFIER can be private, protected or public (default).

Static and Class Methods

Static methods do not receive an implicit first argument, while class methods receives the class as its first argument. They are defined inside the form starting with modifier staticmethod and classmethod respectively, for example:

    (class Class (Object)
      ...

      (staticmethod
        (defun foo ()
          (print "greet from static method")))

       (private
         (setq var 10))
       
       (classmethod
         (defun bar (cls)
           (1+ (@ cls 'var))))

      ...)

      (@ Class 'foo)
      => "greet from static method"
      
      (@ Class 'bar)
      => 11

class.el's People

Contributors

ptpt avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.