Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/jjv360/nim-classes
Adds class support to Nim.
https://github.com/jjv360/nim-classes
Last synced: 2 months ago
JSON representation
Adds class support to Nim.
- Host: GitHub
- URL: https://github.com/jjv360/nim-classes
- Owner: jjv360
- License: mit
- Created: 2020-10-24T00:53:08.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2023-03-13T14:44:05.000Z (almost 2 years ago)
- Last Synced: 2024-08-04T03:05:00.730Z (6 months ago)
- Language: Nim
- Homepage:
- Size: 56.6 KB
- Stars: 89
- Watchers: 7
- Forks: 3
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-nim - classes - Python-style class system for Nim. (Language Features / Object-Oriented Programming)
README
# Classes
![](https://img.shields.io/badge/status-beta-orange)
A collection of macros which add class support to Nim. To install, run `nimble install classes`. Features include:
- Simple class syntax
- Class variables with default values
- Inheritance and `super.calls()`
- Default constructors
- Static and abstract methods
- Methods and variables defined in any order
- Mixins## Examples
```nim
import classes# A base class
class Shape:## The position of the shape
var x = 0
var y = 0## Optional constructor
method init() =
echo "Creating a Shape"## Optional destructor (not supported in JS)
method deinit() =
echo "Destroying a Shape"## Abstract draw function
method draw()## Calculate size
method calculateSize(): float = return 0## Static method
method defaultSize(): float {.static.} = 5# A subclass
class Square of Shape:# Radius
var radius = 5## Override constructor
method init() =
super.init()
echo "Creating a Square"## Draw it
method draw() =
echo "Drawing a square of size " & $this.calculateSize()## Calculate size
method calculateSize(): float = super.calculateSize() + this.radius# Many ways of creating class instances:
let obj = Square.init()
let obj = Square().init()
let obj = newSquare()# Call static methods
# NOTE: Static methods don't always work if the name of your nim file is the exact same as the class name.
# You may get a `type mismatch: got <>` error in that case.
Shape.defaultSize()# Data only classes
class DataOnly:
var v0 = 7
var v1: int
var v2: string# Constructing it this way allows you to pass in values for the variables that don't have values set
let obj = DataOnly(v1: 10, v2: "20").init()# Using the mixin keyword will copy the variables and methods from one class to another
class CustomNote:
var note = ""class Circle of Shape:
mixin CustomNotelet circle = Circle.init()
circle.note = "My custom note"# Singleton classes act the same way as standard classes, but use a .shared() accessor instead of constructors.
# The default constructor is called the first time the singleton class is accessed.
singleton MySingletonClass:
var v1 = 7
method init() = echo "Singleton accessed for the first time!"# Access the singleton, this also triggers the init() the first time
echo MySingletonClass.shared.v1
```## Issues
- **JS backend:** As of Nim 1.4.8 (when I last tested), the internal [dom](https://nim-lang.org/docs/dom.html#class%2CNode) library is exposing a `class()` function, and this function always takes precedent over this library's `class` macro. I don't know how to fix this. In the mean time, you can replace the word `class` with `classes.class`