Code Monkey home page Code Monkey logo

shene's Introduction

shene

Zero-cost interface for Nim.

Installation

nimble install shene

Ideas

Decouple impl and data

Impl represents Impl Class and data stands for Data Class. Impl Class supplies all interfaces which should be satisfied. Data Class supplies all attributes that can be extended by users. They both support inheritance.

type
  Must*[U: object; T: object | ref object] = object 
    impl*: U
    data*: T

Impl Class

We define function pointers just like before, but now the first parameter of function declarations should be generics type. We also should only define function pointers without any other attributes.

type
  Animal*[T] = object of RootObj
    sleepImpl: proc (a: T) {.nimcall, gcsafe.}
    barkImpl: proc (a: var T, b: int, c: int): string {.nimcall, gcsafe.}
    danceImpl: proc (a: T, b: string): string {.nimcall, gcsafe.}

Data Class

Data Class contains all user-defined attributes. You can add data attributes by means of inheritance. But be careful, now we need Must[Animal[Cat], Cat] type as the type of our object which is passed to People object. shene supplies must(Animal, Dog) which is a helper templates to simplify type declaration. It also overloads dot operator and makes assignment easier

# If class is ref object, user must `new Must.data` or `init Must`.
type
  Dog = object
    id: string
    did: int
    name: string


proc bark(d: var Dog, b: int, c: int): string =
  doAssert d.name == "OK"
  d.did = 777
  doAssert d.did == 777
  d.id = "First"
  doAssert d.id == "First"

# must(Impl class, Data class)
proc newDog(): must(Animal, Dog) =
  result.name = "OK"
  result.did = 12
  result.barkImpl = bark

Oriented-User Class

Regarding People class, we need additional generics type. must(Animal, T) is the helper templates for Must[Animal[T], T].

type
  People*[T] = object
    pet: must(Animal, T)

Usage

Its usage is very simple. We only need to add additional generics type. There is little difference compared to before.

var d = newDog()
var p1 = People[Dog](pet: d)
discard p1.pet.call(barkImpl, 13, 14)

shene's People

Contributors

ringabout avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  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.