- 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 protocoledprotocolPExpr:proceval(e):intimplPLiteral:var x:intproceval(e):int= e.xprocnewLit(x:int):PLiteral=result=PLiteral(x: x)implPPlusExpr:var a, b:PExprproceval(e):int=eval(e.a)+eval(e.b)procnewPlus(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:
typePExpr=refobjectofRootObj## abstract base class for an expression evalImpl:proc(e:PExpr):int {.nimcall.}PLiteral=refobjectofPExpr x:intPPlusExpr=refobjectofPExpr a, b:PExprproceval(e:PExpr):int=assert e.evalImpl!=nil e.evalImpl(e)procevalLit(e:PExpr):int=PLiteral(e).xprocevalPlus(e:PExpr):int=eval(PPlusExpr(e).a)+eval(PPlusExpr(e).b)procnewLit(x:int):PLiteral=PLiteral(evalImpl: evalLit, x: x)procnewPlus(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
.