Cheddar Documentation
  • Introduction
  • Syntax
  • Literals
    • Comment
    • String
    • Number
    • Array
    • Boolean
  • Mathematics
    • Addition
    • Subtraction
    • Multiplication
    • Division
    • Exponentiation
    • Remainder
    • Negation
    • Sign
    • Root
    • Bitwise AND
    • Bitwise OR
    • Bitwise XOR
    • Bitwise NOT
    • Bitwise Left Shift
    • Bitwise Right Shift
  • Variables
  • Functions
    • Defining
      • Lambda
      • Functionized Operators
      • Functionized Properties
    • Operations
      • Functional Bonding
      • Functional Composition
  • Default Operators
    • What Is
    • Instance-of
    • Actually Is
  • Control Flow
    • Conditional
    • Loops
      • For Loops
      • While Loops
  • Standard Library
    • String
      • Bytes
      • Count
      • Length
      • Match
      • Slice
      • Tail
      • Chars
      • Head
      • Lines
      • Ord
      • Split
      • Test
      • Chunk
      • Index
      • Lower
      • Reverse
      • Substitute
      • Upper
  • Developing
    • Structure
    • Primitive Objects
      • Scope
      • Class
      • Variable
      • Namespace
    • Getting Started
    • API
      • Primitives
        • string
        • number
        • array
        • bool
        • func
        • nil
Powered by GitBook
On this page

Was this helpful?

  1. Developing
  2. Primitive Objects

Class

PreviousScopeNextVariable

Last updated 4 years ago

Was this helpful?

You may know what a class is, a "blueprint" for which an object is to be constructed from, but internally they are very simple (the source code contains detailed information of their implementation, may provide additional details). A class has 3 main parts:

         Class
           |
  +--------+--------+
  |        |        |
scope    init()  instance

A class is a scope. An instance of a class is really just a scope. This may sound confusing but, think about what a scope is, it is a hashmap of variables which can inherit and provides interfacing for accessing it's variables. That sounds just like what a class is.

So what else? A class has an initializer, this is the main part seperating a class from a scope. Here are the steps taken during a class initalization:

  1. Create an empty scope (this is called the "instance")

  2. Copy the class's scope to the instance (and the inheriting class's if applicable).

  3. Run the initializer (init) and pass the instance as "self".

  4. Return the instance.

As you can see, the returned instance, is just a regular scope, but pre-populated. When executed, methods are passed the scope as a whole. Here's an example:

class MyClass() {
  whatIsSelf => self
}

var A = MyClass{};
A.whatIsSelf();   // < Instance of "MyClass" >

as you can see, whatIsSelf is passed its scope (A) by the evaluator, but the scope is not directly attached to whatIsSelf.

the class implementation