GithubHelp home page GithubHelp logo

philippmdoerner / constructor Goto Github PK

View Code? Open in Web Editor NEW

This project forked from beef331/constructor

0.0 1.0 0.0 122 KB

Nim macros to aid in object construction including event programming, and constructors.

License: MIT License

Nim 100.00%

constructor's Introduction

Constructor

A collection of useful macros, mostly related to the construction of objects.

Installation

nimble install constructor

Constructor

constructor works similarly to construct but does it with your own procedures so you can match your preferred method. You can pass other parameters to the object constructor by having a variable named the same in the main scope of the procedure. Aside from that it's practically like writting your own init procedure.

import constructor/constructor
type
  User = object
    name: string
    age: int
    lastOnline: float

proc initUser*(name: string, age: int): User {.constr.} # Can use like a forward declare.

proc init(T: typedesc[User], name: string, age: int): User {.constr.} =
  result.lastOnline = 30f


assert initUser("hello", 10) == User(name: "hello", lastOnline: 0f, age: 10)
assert User.init("hello", 30) == User(name: "hello", lastOnline: 30f, age: 30)

Defaults

You can use the defaults and implDefaults macros, which allow you to easily generate a constructor with default values. If your type is a value-allocated type the constructor is init<YOUR TYPE>() (e.g. initThingy()). If your type is a heap-allocated object the constructor is new<YOUR TYPE>() (e.g. newThingy()).

import constructor/defaults
type Thingy{.defaults.} = object
  a: float = 10 # Can do it this way
  b = "Hmm" # Can also do it this way
  c = 10
implDefaults(Thingy) # Required to embed the procedure
assert initThingy() == Thingy(a: 10, b: "Hmm", c: 10)

Modifying implDefaults

You can pass implDefaults a list of DefaultFlag flags to modify its behaviour. Currently implemented flags are:

  • defExported - Adds a '*' to the proc so that it is exported (e.g. generates newThingy\*() instead of newThingy())
  • defTypeConstr - Changes the constructor signature for heap-allocated from newThingy()/initThingy() to new(_: typedesc[Thingy])/init(_: typedesc[Thingy])
import constructor/defaults
type Thingy{.defaults.} = ref object of RootObj
  a: float = 10 # Can do it this way
  b = "Hmm" # Can also do it this way
  c = 10

implDefaults(Thingy, {DefaultFlag.defExported, DefaultFlag.defTypeConstr})
assert new(Thingy) == Thingy(a: 10, b: "Hmm", c: 10)

type OtherThingy{.defaults.} = object
  d = "lula"

implDefaults(OtherThingy, {DefaultFlag.defExported, DefaultFlag.defTypeConstr})
assert init(OtherThingy) == OtherThingy(d: "lula")

constructor's People

Contributors

beef331 avatar knorrfg avatar philippmdoerner avatar shadowninja55 avatar

Watchers

 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.