- Notifications
You must be signed in to change notification settings - Fork1
planetis-m/protocoled
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
This nimble package contains theprotocol
macro for easily implementinginterfacesin Nim.
Example:
import protocoledprotocol PExpr:proc eval(e):int impl PLiteral:var x:intproc eval(e):int= e.xproc newLit(x:int): PLiteral=result= PLiteral(x: x) impl PPlusExpr:var a, b: PExprproc eval(e):int= eval(e.a)+ eval(e.b)proc newPlus(a, b: PExpr): PPlusExpr=result= PPlusExpr(a: a, b: b)
Notice the typeless parametere
, the macro takes care of assigning it theproper type. Then it is translated roughly into this code:
type PExpr=refobjectof RootObj## abstract base class for an expression evalImpl:proc(e: PExpr):int {.nimcall.} PLiteral=refobjectof PExpr x:int PPlusExpr=refobjectof PExpr a, b: PExprproc eval(e: PExpr):int= assert e.evalImpl!=nil e.evalImpl(e)proc evalLit(e: PExpr):int= PLiteral(e).xproc evalPlus(e: PExpr):int= eval(PPlusExpr(e).a)+ eval(PPlusExpr(e).b)proc newLit(x:int): PLiteral= PLiteral(evalImpl: evalLit, x: x)proc newPlus(a, b: PExpr): PPlusExpr= PPlusExpr(evalImpl: evalPlus, a: a, b: b)
- You need to separate the
self
parameter from the rest with a semicolon;
. - The export marker
*
is using infix notation like so:impl *Student
. - In the constructor
proc
, implicit return of the last expression is not supported.
This library is distributed under the MIT license. For more information seecopying.txt
.
About
Interface macro for nim
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Releases
No releases published
Packages0
No packages published
Uh oh!
There was an error while loading.Please reload this page.