Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

Interface macro for nim

License

NotificationsYou must be signed in to change notification settings

planetis-m/protocoled

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

About

This nimble package contains theprotocol macro for easily implementinginterfacesin Nim.

Theprotocol macro

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)

Known quirks

  • You need to separate theself parameter from the rest with a semicolon;.
  • The export marker* is using infix notation like so:impl *Student.
  • In the constructorproc, implicit return of the last expression is not supported.

License

This library is distributed under the MIT license. For more information seecopying.txt.

About

Interface macro for nim

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages


[8]ページ先頭

©2009-2025 Movatter.jp