beef331 / constructor Goto Github PK
View Code? Open in Web Editor NEWNim macros to aid in object construction including event programming, and constructors.
License: MIT License
Nim macros to aid in object construction including event programming, and constructors.
License: MIT License
when trying the example code:
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)
with the flag --experimental:strictFuncs set, the compiler complains:
..../.nimble/pkgs/micros-0.1.5/micros/definitions/routines.nim(109, 49) template/generic instantiation of & from here
..../nim-1.6.6/lib/system.nim(1675, 8) Error: '&' can have side effects an object reachable from 'x' is potentially mutated
..../nim-1.6.6/lib/system.nim(1689, 23) the mutation is here
..../nim-1.6.6/lib/system.nim(1689, 23) is the statement that connected the mutation to the parameter
It would be nice to be able to use the lib independently of that flag.
Taken from a larger project, source is here: https://github.com/ajusa/dalo/blob/master/src/dalo.nim
Here's the code:
proc initField*(label = "", default = "", widget = defaultInput): Field
Field(label: label, widget: widget, default: default)
The constructor version of this causes a segfault, whereas having the proc above doesn't call a segfault. widget is of type
Widget* = proc(f: Field, value: string, error: string): VNode {.closure.}
Since it seems to segfault when I call render (which calls the Widget proc) I can only assume that something related to that is causing the segfault. Hopefully that's enough for you, let me know if you need more information!
type FileInfo* = object
path*: string
size*: BiggestInt
exists*: bool
type Image* = object
file*: FileInfo
dest*: string
date*: DateTime
Doing
proc initImage*(file: FileInfo, dest: string, lastModified: Time): Image =
Image.init(file, dest, lastModified.inZone(local()))
results in the error Error: unhandled exception: index out of bounds, the container is empty [IndexDefect]
I can only assume this has to do with the nested object, as your tests seem to pass.
Attempting to use defaults with generic objects results in compiler errors both with {}
and {defTypeConstr}
. We likely need to lift the generic parameters and add them to the procedures. Then use Inner[T]
everywhere instead of Inner
.
@Patitotective no clue if you have any interest in throwing darts and seeing what lands. Totally up to you.
Heyho, I am using constructor together with orm for convenience purposes and stumbled over this bug:
import norm/[model, pragmas]
import constructor/defaults
import std/times
type
Item* {.defaults, tableName: "atable".} = ref object of Model
name*: string = ""
campaign_id*: int64 = -2
update_datetime*: DateTime = now()
creation_datetime*: DateTime = now()
implDefaults(Item)
let i1 = newItem() # Works but should it?
#let i2 = initItem() # does not compile
According to what we discussed on discord, the object as a value-allocated type Item
should create a initItem()
in accordance with nim best practices.
Instead it gets a newItem()
proc like a heap allocated object.
This appears to be an issue due to inheritance in the type.
We had discussed this one on discord a couple days back. Having runable examples for the macros and doc comments could be useful.
I'll make a PR to add some that you can do with as you please, just wanted to contribute something mildly useful.
To do this we should replace the bool
exported with a bitset that allows features as we go, such as defTypeInit
.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.