beef331 / website

Code for the official Nim programming language website
https://nim-lang.org
19 stars 1 forks source link

OOlib: upgrade #85

Closed glassesneo closed 2 months ago

glassesneo commented 10 months ago

Name: OOilb

Author: Neo

Posting:

OOlib version 0.7.0

New super cool features here!

Multiple constructor

class Gun:
  var
    offence: int
    capacity = 6
    price: int

  proc `new`(offence: int) =
    self.offence = offence
    self.capacity = 8
    self.price = 300

  proc `new`(capacity: int) =
    self.offence = 14
    self.capacity = capacity
    self.price = 200

# This `new()` is made from the type definition
let _ = Gun.new(offence = 5, price = 6)

# 2nd one
let _ = Gun.new(offence = 12)

# 3rd one
let _ = Gun.new(capacity = 10)

class Sword:
  var
    offence: int
    price {.initial.} = 100

# made from the type definition
let _ = Sword.new(8)

{.construct.}

a type definition with {.construct.} are made into a class. Now that we can define a class without using class!

type Shield {.construct.} = ref object
  defence*: int
  price {.initial.}: int = 100

# This new() was made by `{.construct.}` pragma
let _ = Shield.new(4)

Multiple implementation

Now we can use multiple implementation!

protocol Readable:
  var text: string

protocol Writable:
  var text: string
  proc `text=`(value: string)

protocol Product:
  var price: int

type Writer* {.protocoled.} = tuple
  write: proc(text: string)

class Book impl (Readable, Product):
  var
    text: string = ""
    price: int

class Diary impl (Readable, Writable, Product):
  var text {.initial.}: string = ""
  var price: int
  proc `text=`(value: string) =
    self.text = value

class HTMLWriter impl Writer:
  var writable: Writable
  proc write(text: string) =
    self.writable.text = text

let book = Book.new(price = 500)

let _ = book.toProtocol()

let diary = Diary.new(price = 300)

let _ = diary.toProtocol()

Also, the features below were removed: