manual_experimental.rst 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073
  1. =========================
  2. Nim Experimental Features
  3. =========================
  4. :Authors: Andreas Rumpf
  5. :Version: |nimversion|
  6. .. default-role:: code
  7. .. include:: rstcommon.rst
  8. .. contents::
  9. About this document
  10. ===================
  11. This document describes features of Nim that are to be considered experimental.
  12. Some of these are not covered by the `.experimental` pragma or
  13. `--experimental`:option: switch because they are already behind a special syntax and
  14. one may want to use Nim libraries using these features without using them
  15. oneself.
  16. **Note**: Unless otherwise indicated, these features are not to be removed,
  17. but refined and overhauled.
  18. Package level objects
  19. =====================
  20. Every Nim module resides in a (nimble) package. An object type can be attached
  21. to the package it resides in. If that is done, the type can be referenced from
  22. other modules as an `incomplete`:idx: object type. This feature allows to
  23. break up recursive type dependencies across module boundaries. Incomplete
  24. object types are always passed `byref` and can only be used in pointer like
  25. contexts (`var/ref/ptr IncompleteObject`) in general since the compiler does
  26. not yet know the size of the object. To complete an incomplete object
  27. the `package` pragma has to be used. `package` implies `byref`.
  28. As long as a type `T` is incomplete, neither `sizeof(T)` nor runtime
  29. type information for `T` is available.
  30. Example:
  31. .. code-block:: nim
  32. # module A (in an arbitrary package)
  33. type
  34. Pack.SomeObject = object ## declare as incomplete object of package 'Pack'
  35. Triple = object
  36. a, b, c: ref SomeObject ## pointers to incomplete objects are allowed
  37. ## Incomplete objects can be used as parameters:
  38. proc myproc(x: SomeObject) = discard
  39. .. code-block:: nim
  40. # module B (in package "Pack")
  41. type
  42. SomeObject* {.package.} = object ## Use 'package' to complete the object
  43. s, t: string
  44. x, y: int
  45. Void type
  46. =========
  47. The `void` type denotes the absence of any type. Parameters of
  48. type `void` are treated as non-existent, `void` as a return type means that
  49. the procedure does not return a value:
  50. .. code-block:: nim
  51. proc nothing(x, y: void): void =
  52. echo "ha"
  53. nothing() # writes "ha" to stdout
  54. The `void` type is particularly useful for generic code:
  55. .. code-block:: nim
  56. proc callProc[T](p: proc (x: T), x: T) =
  57. when T is void:
  58. p()
  59. else:
  60. p(x)
  61. proc intProc(x: int) = discard
  62. proc emptyProc() = discard
  63. callProc[int](intProc, 12)
  64. callProc[void](emptyProc)
  65. However, a `void` type cannot be inferred in generic code:
  66. .. code-block:: nim
  67. callProc(emptyProc)
  68. # Error: type mismatch: got (proc ())
  69. # but expected one of:
  70. # callProc(p: proc (T), x: T)
  71. The `void` type is only valid for parameters and return types; other symbols
  72. cannot have the type `void`.
  73. Automatic dereferencing
  74. =======================
  75. Automatic dereferencing is performed for the first argument of a routine call.
  76. This feature has to be enabled via `{.experimental: "implicitDeref".}`:
  77. .. code-block:: nim
  78. {.experimental: "implicitDeref".}
  79. proc depth(x: NodeObj): int = ...
  80. var
  81. n: Node
  82. new(n)
  83. echo n.depth
  84. # no need to write n[].depth either
  85. Code reordering
  86. ===============
  87. The code reordering feature can implicitly rearrange procedure, template, and
  88. macro definitions along with variable declarations and initializations at the top
  89. level scope so that, to a large extent, a programmer should not have to worry
  90. about ordering definitions correctly or be forced to use forward declarations to
  91. preface definitions inside a module.
  92. ..
  93. NOTE: The following was documentation for the code reordering precursor,
  94. which was {.noForward.}.
  95. In this mode, procedure definitions may appear out of order and the compiler
  96. will postpone their semantic analysis and compilation until it actually needs
  97. to generate code using the definitions. In this regard, this mode is similar
  98. to the modus operandi of dynamic scripting languages, where the function
  99. calls are not resolved until the code is executed. Here is the detailed
  100. algorithm taken by the compiler:
  101. 1. When a callable symbol is first encountered, the compiler will only note
  102. the symbol callable name and it will add it to the appropriate overload set
  103. in the current scope. At this step, it won't try to resolve any of the type
  104. expressions used in the signature of the symbol (so they can refer to other
  105. not yet defined symbols).
  106. 2. When a top level call is encountered (usually at the very end of the
  107. module), the compiler will try to determine the actual types of all of the
  108. symbols in the matching overload set. This is a potentially recursive process
  109. as the signatures of the symbols may include other call expressions, whose
  110. types will be resolved at this point too.
  111. 3. Finally, after the best overload is picked, the compiler will start
  112. compiling the body of the respective symbol. This in turn will lead the
  113. compiler to discover more call expressions that need to be resolved and steps
  114. 2 and 3 will be repeated as necessary.
  115. Please note that if a callable symbol is never used in this scenario, its
  116. body will never be compiled. This is the default behavior leading to best
  117. compilation times, but if exhaustive compilation of all definitions is
  118. required, using `nim check` provides this option as well.
  119. Example:
  120. .. code-block:: nim
  121. {.experimental: "codeReordering".}
  122. proc foo(x: int) =
  123. bar(x)
  124. proc bar(x: int) =
  125. echo(x)
  126. foo(10)
  127. Variables can also be reordered as well. Variables that are *initialized* (i.e.
  128. variables that have their declaration and assignment combined in a single
  129. statement) can have their entire initialization statement reordered. Be wary of
  130. what code is executed at the top level:
  131. .. code-block:: nim
  132. {.experimental: "codeReordering".}
  133. proc a() =
  134. echo(foo)
  135. var foo = 5
  136. a() # outputs: "5"
  137. ..
  138. TODO: Let's table this for now. This is an *experimental feature* and so the
  139. specific manner in which `declared` operates with it can be decided in
  140. eventuality, because right now it works a bit weirdly.
  141. The values of expressions involving `declared` are decided *before* the
  142. code reordering process, and not after. As an example, the output of this
  143. code is the same as it would be with code reordering disabled.
  144. .. code-block:: nim
  145. {.experimental: "codeReordering".}
  146. proc x() =
  147. echo(declared(foo))
  148. var foo = 4
  149. x() # "false"
  150. It is important to note that reordering *only* works for symbols at top level
  151. scope. Therefore, the following will *fail to compile:*
  152. .. code-block:: nim
  153. {.experimental: "codeReordering".}
  154. proc a() =
  155. b()
  156. proc b() =
  157. echo("Hello!")
  158. a()
  159. Named argument overloading
  160. ==========================
  161. Routines with the same type signature can be called differently if a parameter
  162. has different names. This does not need an `experimental` switch, but is an
  163. unstable feature.
  164. .. code-block:: Nim
  165. proc foo(x: int) =
  166. echo "Using x: ", x
  167. proc foo(y: int) =
  168. echo "Using y: ", y
  169. foo(x = 2)
  170. # Using x: 2
  171. foo(y = 2)
  172. # Using y: 2
  173. Do notation
  174. ===========
  175. As a special more convenient notation, proc expressions involved in procedure
  176. calls can use the `do` keyword:
  177. .. code-block:: nim
  178. sort(cities) do (x,y: string) -> int:
  179. cmp(x.len, y.len)
  180. # Less parenthesis using the method plus command syntax:
  181. cities = cities.map do (x:string) -> string:
  182. "City of " & x
  183. # In macros, the do notation is often used for quasi-quoting
  184. macroResults.add quote do:
  185. if not `ex`:
  186. echo `info`, ": Check failed: ", `expString`
  187. `do` is written after the parentheses enclosing the regular proc params.
  188. The proc expression represented by the do block is appended to them.
  189. In calls using the command syntax, the do block will bind to the immediately
  190. preceding expression, transforming it in a call.
  191. `do` with parentheses is an anonymous `proc`; however a `do` without
  192. parentheses is just a block of code. The `do` notation can be used to
  193. pass multiple blocks to a macro:
  194. .. code-block:: nim
  195. macro performWithUndo(task, undo: untyped) = ...
  196. performWithUndo do:
  197. # multiple-line block of code
  198. # to perform the task
  199. do:
  200. # code to undo it
  201. Special Operators
  202. =================
  203. dot operators
  204. -------------
  205. **Note**: Dot operators are still experimental and so need to be enabled
  206. via `{.experimental: "dotOperators".}`.
  207. Nim offers a special family of dot operators that can be used to
  208. intercept and rewrite proc call and field access attempts, referring
  209. to previously undeclared symbol names. They can be used to provide a
  210. fluent interface to objects lying outside the static confines of the
  211. type system such as values from dynamic scripting languages
  212. or dynamic file formats such as JSON or XML.
  213. When Nim encounters an expression that cannot be resolved by the
  214. standard overload resolution rules, the current scope will be searched
  215. for a dot operator that can be matched against a re-written form of
  216. the expression, where the unknown field or proc name is passed to
  217. an `untyped` parameter:
  218. .. code-block:: nim
  219. a.b # becomes `.`(a, b)
  220. a.b(c, d) # becomes `.`(a, b, c, d)
  221. The matched dot operators can be symbols of any callable kind (procs,
  222. templates and macros), depending on the desired effect:
  223. .. code-block:: nim
  224. template `.`(js: PJsonNode, field: untyped): JSON = js[astToStr(field)]
  225. var js = parseJson("{ x: 1, y: 2}")
  226. echo js.x # outputs 1
  227. echo js.y # outputs 2
  228. The following dot operators are available:
  229. operator `.`
  230. ------------
  231. This operator will be matched against both field accesses and method calls.
  232. operator `.()`
  233. ---------------
  234. This operator will be matched exclusively against method calls. It has higher
  235. precedence than the `.` operator and this allows one to handle expressions like
  236. `x.y` and `x.y()` differently if one is interfacing with a scripting language
  237. for example.
  238. operator `.=`
  239. -------------
  240. This operator will be matched against assignments to missing fields.
  241. .. code-block:: nim
  242. a.b = c # becomes `.=`(a, b, c)
  243. Call operator
  244. -------------
  245. The call operator, `()`, matches all kinds of unresolved calls and takes
  246. precedence over dot operators, however it does not match missing overloads
  247. for existing routines. The experimental `callOperator` switch must be enabled
  248. to use this operator.
  249. .. code-block:: nim
  250. {.experimental: "callOperator".}
  251. template `()`(a: int, b: float): untyped = $(a, b)
  252. block:
  253. let a = 1.0
  254. let b = 2
  255. doAssert b(a) == `()`(b, a)
  256. doAssert a.b == `()`(b, a)
  257. block:
  258. let a = 1.0
  259. proc b(): int = 2
  260. doAssert not compiles(b(a))
  261. doAssert not compiles(a.b) # `()` not called
  262. block:
  263. let a = 1.0
  264. proc b(x: float): int = int(x + 1)
  265. let c = 3.0
  266. doAssert not compiles(a.b(c)) # gives a type mismatch error same as b(a, c)
  267. doAssert (a.b)(c) == `()`(a.b, c)
  268. Not nil annotation
  269. ==================
  270. **Note:** This is an experimental feature. It can be enabled with
  271. `{.experimental: "notnil".}`.
  272. All types for which `nil` is a valid value can be annotated with the
  273. `not nil` annotation to exclude `nil` as a valid value:
  274. .. code-block:: nim
  275. {.experimental: "notnil".}
  276. type
  277. PObject = ref TObj not nil
  278. TProc = (proc (x, y: int)) not nil
  279. proc p(x: PObject) =
  280. echo "not nil"
  281. # compiler catches this:
  282. p(nil)
  283. # and also this:
  284. var x: PObject
  285. p(x)
  286. The compiler ensures that every code path initializes variables which contain
  287. non-nilable pointers. The details of this analysis are still to be specified
  288. here.
  289. .. include:: manual_experimental_strictnotnil.rst
  290. Concepts
  291. ========
  292. Concepts, also known as "user-defined type classes", are used to specify an
  293. arbitrary set of requirements that the matched type must satisfy.
  294. Concepts are written in the following form:
  295. .. code-block:: nim
  296. type
  297. Comparable = concept x, y
  298. (x < y) is bool
  299. Stack[T] = concept s, var v
  300. s.pop() is T
  301. v.push(T)
  302. s.len is Ordinal
  303. for value in s:
  304. value is T
  305. The concept is a match if:
  306. a) all of the expressions within the body can be compiled for the tested type
  307. b) all statically evaluable boolean expressions in the body must be true
  308. The identifiers following the `concept` keyword represent instances of the
  309. currently matched type. You can apply any of the standard type modifiers such
  310. as `var`, `ref`, `ptr` and `static` to denote a more specific type of
  311. instance. You can also apply the `type` modifier to create a named instance of
  312. the type itself:
  313. .. code-block:: nim
  314. type
  315. MyConcept = concept x, var v, ref r, ptr p, static s, type T
  316. ...
  317. Within the concept body, types can appear in positions where ordinary values
  318. and parameters are expected. This provides a more convenient way to check for
  319. the presence of callable symbols with specific signatures:
  320. .. code-block:: nim
  321. type
  322. OutputStream = concept var s
  323. s.write(string)
  324. In order to check for symbols accepting `type` params, you must prefix
  325. the type with the explicit `type` modifier. The named instance of the
  326. type, following the `concept` keyword is also considered to have the
  327. explicit modifier and will be matched only as a type.
  328. .. code-block:: nim
  329. type
  330. # Let's imagine a user-defined casting framework with operators
  331. # such as `val.to(string)` and `val.to(JSonValue)`. We can test
  332. # for these with the following concept:
  333. MyCastables = concept x
  334. x.to(type string)
  335. x.to(type JSonValue)
  336. # Let's define a couple of concepts, known from Algebra:
  337. AdditiveMonoid* = concept x, y, type T
  338. x + y is T
  339. T.zero is T # require a proc such as `int.zero` or 'Position.zero'
  340. AdditiveGroup* = concept x, y, type T
  341. x is AdditiveMonoid
  342. -x is T
  343. x - y is T
  344. Please note that the `is` operator allows one to easily verify the precise
  345. type signatures of the required operations, but since type inference and
  346. default parameters are still applied in the concept body, it's also possible
  347. to describe usage protocols that do not reveal implementation details.
  348. Much like generics, concepts are instantiated exactly once for each tested type
  349. and any static code included within the body is executed only once.
  350. Concept diagnostics
  351. -------------------
  352. By default, the compiler will report the matching errors in concepts only when
  353. no other overload can be selected and a normal compilation error is produced.
  354. When you need to understand why the compiler is not matching a particular
  355. concept and, as a result, a wrong overload is selected, you can apply the
  356. `explain` pragma to either the concept body or a particular call-site.
  357. .. code-block:: nim
  358. type
  359. MyConcept {.explain.} = concept ...
  360. overloadedProc(x, y, z) {.explain.}
  361. This will provide Hints in the compiler output either every time the concept is
  362. not matched or only on the particular call-site.
  363. Generic concepts and type binding rules
  364. ---------------------------------------
  365. The concept types can be parametric just like the regular generic types:
  366. .. code-block:: nim
  367. ### matrixalgo.nim
  368. import std/typetraits
  369. type
  370. AnyMatrix*[R, C: static int; T] = concept m, var mvar, type M
  371. M.ValueType is T
  372. M.Rows == R
  373. M.Cols == C
  374. m[int, int] is T
  375. mvar[int, int] = T
  376. type TransposedType = stripGenericParams(M)[C, R, T]
  377. AnySquareMatrix*[N: static int, T] = AnyMatrix[N, N, T]
  378. AnyTransform3D* = AnyMatrix[4, 4, float]
  379. proc transposed*(m: AnyMatrix): m.TransposedType =
  380. for r in 0 ..< m.R:
  381. for c in 0 ..< m.C:
  382. result[r, c] = m[c, r]
  383. proc determinant*(m: AnySquareMatrix): int =
  384. ...
  385. proc setPerspectiveProjection*(m: AnyTransform3D) =
  386. ...
  387. --------------
  388. ### matrix.nim
  389. type
  390. Matrix*[M, N: static int; T] = object
  391. data: array[M*N, T]
  392. proc `[]`*(M: Matrix; m, n: int): M.T =
  393. M.data[m * M.N + n]
  394. proc `[]=`*(M: var Matrix; m, n: int; v: M.T) =
  395. M.data[m * M.N + n] = v
  396. # Adapt the Matrix type to the concept's requirements
  397. template Rows*(M: typedesc[Matrix]): int = M.M
  398. template Cols*(M: typedesc[Matrix]): int = M.N
  399. template ValueType*(M: typedesc[Matrix]): typedesc = M.T
  400. -------------
  401. ### usage.nim
  402. import matrix, matrixalgo
  403. var
  404. m: Matrix[3, 3, int]
  405. projectionMatrix: Matrix[4, 4, float]
  406. echo m.transposed.determinant
  407. setPerspectiveProjection projectionMatrix
  408. When the concept type is matched against a concrete type, the unbound type
  409. parameters are inferred from the body of the concept in a way that closely
  410. resembles the way generic parameters of callable symbols are inferred on
  411. call sites.
  412. Unbound types can appear both as params to calls such as `s.push(T)` and
  413. on the right-hand side of the `is` operator in cases such as `x.pop is T`
  414. and `x.data is seq[T]`.
  415. Unbound static params will be inferred from expressions involving the `==`
  416. operator and also when types dependent on them are being matched:
  417. .. code-block:: nim
  418. type
  419. MatrixReducer[M, N: static int; T] = concept x
  420. x.reduce(SquareMatrix[N, T]) is array[M, int]
  421. The Nim compiler includes a simple linear equation solver, allowing it to
  422. infer static params in some situations where integer arithmetic is involved.
  423. Just like in regular type classes, Nim discriminates between `bind once`
  424. and `bind many` types when matching the concept. You can add the `distinct`
  425. modifier to any of the otherwise inferable types to get a type that will be
  426. matched without permanently inferring it. This may be useful when you need
  427. to match several procs accepting the same wide class of types:
  428. .. code-block:: nim
  429. type
  430. Enumerable[T] = concept e
  431. for v in e:
  432. v is T
  433. type
  434. MyConcept = concept o
  435. # this could be inferred to a type such as Enumerable[int]
  436. o.foo is distinct Enumerable
  437. # this could be inferred to a different type such as Enumerable[float]
  438. o.bar is distinct Enumerable
  439. # it's also possible to give an alias name to a `bind many` type class
  440. type Enum = distinct Enumerable
  441. o.baz is Enum
  442. On the other hand, using `bind once` types allows you to test for equivalent
  443. types used in multiple signatures, without actually requiring any concrete
  444. types, thus allowing you to encode implementation-defined types:
  445. .. code-block:: nim
  446. type
  447. MyConcept = concept x
  448. type T1 = auto
  449. x.foo(T1)
  450. x.bar(T1) # both procs must accept the same type
  451. type T2 = seq[SomeNumber]
  452. x.alpha(T2)
  453. x.omega(T2) # both procs must accept the same type
  454. # and it must be a numeric sequence
  455. As seen in the previous examples, you can refer to generic concepts such as
  456. `Enumerable[T]` just by their short name. Much like the regular generic types,
  457. the concept will be automatically instantiated with the bind once auto type
  458. in the place of each missing generic param.
  459. Please note that generic concepts such as `Enumerable[T]` can be matched
  460. against concrete types such as `string`. Nim doesn't require the concept
  461. type to have the same number of parameters as the type being matched.
  462. If you wish to express a requirement towards the generic parameters of
  463. the matched type, you can use a type mapping operator such as `genericHead`
  464. or `stripGenericParams` within the body of the concept to obtain the
  465. uninstantiated version of the type, which you can then try to instantiate
  466. in any required way. For example, here is how one might define the classic
  467. `Functor` concept from Haskell and then demonstrate that Nim's `Option[T]`
  468. type is an instance of it:
  469. .. code-block:: nim
  470. :test: "nim c $1"
  471. import std/[sugar, typetraits]
  472. type
  473. Functor[A] = concept f
  474. type MatchedGenericType = genericHead(typeof(f))
  475. # `f` will be a value of a type such as `Option[T]`
  476. # `MatchedGenericType` will become the `Option` type
  477. f.val is A
  478. # The Functor should provide a way to obtain
  479. # a value stored inside it
  480. type T = auto
  481. map(f, A -> T) is MatchedGenericType[T]
  482. # And it should provide a way to map one instance of
  483. # the Functor to a instance of a different type, given
  484. # a suitable `map` operation for the enclosed values
  485. import std/options
  486. echo Option[int] is Functor # prints true
  487. Concept derived values
  488. ----------------------
  489. All top level constants or types appearing within the concept body are
  490. accessible through the dot operator in procs where the concept was successfully
  491. matched to a concrete type:
  492. .. code-block:: nim
  493. type
  494. DateTime = concept t1, t2, type T
  495. const Min = T.MinDate
  496. T.Now is T
  497. t1 < t2 is bool
  498. type TimeSpan = typeof(t1 - t2)
  499. TimeSpan * int is TimeSpan
  500. TimeSpan + TimeSpan is TimeSpan
  501. t1 + TimeSpan is T
  502. proc eventsJitter(events: Enumerable[DateTime]): float =
  503. var
  504. # this variable will have the inferred TimeSpan type for
  505. # the concrete Date-like value the proc was called with:
  506. averageInterval: DateTime.TimeSpan
  507. deviation: float
  508. ...
  509. Concept refinement
  510. ------------------
  511. When the matched type within a concept is directly tested against a different
  512. concept, we say that the outer concept is a refinement of the inner concept and
  513. thus it is more-specific. When both concepts are matched in a call during
  514. overload resolution, Nim will assign a higher precedence to the most specific
  515. one. As an alternative way of defining concept refinements, you can use the
  516. object inheritance syntax involving the `of` keyword:
  517. .. code-block:: nim
  518. type
  519. Graph = concept g, type G of EquallyComparable, Copyable
  520. type
  521. VertexType = G.VertexType
  522. EdgeType = G.EdgeType
  523. VertexType is Copyable
  524. EdgeType is Copyable
  525. var
  526. v: VertexType
  527. e: EdgeType
  528. IncidendeGraph = concept of Graph
  529. # symbols such as variables and types from the refined
  530. # concept are automatically in scope:
  531. g.source(e) is VertexType
  532. g.target(e) is VertexType
  533. g.outgoingEdges(v) is Enumerable[EdgeType]
  534. BidirectionalGraph = concept g, type G
  535. # The following will also turn the concept into a refinement when it
  536. # comes to overload resolution, but it doesn't provide the convenient
  537. # symbol inheritance
  538. g is IncidendeGraph
  539. g.incomingEdges(G.VertexType) is Enumerable[G.EdgeType]
  540. proc f(g: IncidendeGraph)
  541. proc f(g: BidirectionalGraph) # this one will be preferred if we pass a type
  542. # matching the BidirectionalGraph concept
  543. ..
  544. Converter type classes
  545. ----------------------
  546. Concepts can also be used to convert a whole range of types to a single type or
  547. a small set of simpler types. This is achieved with a `return` statement within
  548. the concept body:
  549. .. code-block:: nim
  550. type
  551. Stringable = concept x
  552. $x is string
  553. return $x
  554. StringRefValue[CharType] = object
  555. base: ptr CharType
  556. len: int
  557. StringRef = concept x
  558. # the following would be an overloaded proc for cstring, string, seq and
  559. # other user-defined types, returning either a StringRefValue[char] or
  560. # StringRefValue[wchar]
  561. return makeStringRefValue(x)
  562. # the varargs param will here be converted to an array of StringRefValues
  563. # the proc will have only two instantiations for the two character types
  564. proc log(format: static string, varargs[StringRef])
  565. # this proc will allow char and wchar values to be mixed in
  566. # the same call at the cost of additional instantiations
  567. # the varargs param will be converted to a tuple
  568. proc log(format: static string, varargs[distinct StringRef])
  569. ..
  570. VTable types
  571. ------------
  572. Concepts allow Nim to define a great number of algorithms, using only
  573. static polymorphism and without erasing any type information or sacrificing
  574. any execution speed. But when polymorphic collections of objects are required,
  575. the user must use one of the provided type erasure techniques - either common
  576. base types or VTable types.
  577. VTable types are represented as "fat pointers" storing a reference to an
  578. object together with a reference to a table of procs implementing a set of
  579. required operations (the so called vtable).
  580. In contrast to other programming languages, the vtable in Nim is stored
  581. externally to the object, allowing you to create multiple different vtable
  582. views for the same object. Thus, the polymorphism in Nim is unbounded -
  583. any type can implement an unlimited number of protocols or interfaces not
  584. originally envisioned by the type's author.
  585. Any concept type can be turned into a VTable type by using the `vtref`
  586. or the `vtptr` compiler magics. Under the hood, these magics generate
  587. a converter type class, which converts the regular instances of the matching
  588. types to the corresponding VTable type.
  589. .. code-block:: nim
  590. type
  591. IntEnumerable = vtref Enumerable[int]
  592. MyObject = object
  593. enumerables: seq[IntEnumerable]
  594. streams: seq[OutputStream.vtref]
  595. proc addEnumerable(o: var MyObject, e: IntEnumerable) =
  596. o.enumerables.add e
  597. proc addStream(o: var MyObject, e: OutputStream.vtref) =
  598. o.streams.add e
  599. The procs that will be included in the vtable are derived from the concept
  600. body and include all proc calls for which all param types were specified as
  601. concrete types. All such calls should include exactly one param of the type
  602. matched against the concept (not necessarily in the first position), which
  603. will be considered the value bound to the vtable.
  604. Overloads will be created for all captured procs, accepting the vtable type
  605. in the position of the captured underlying object.
  606. Under these rules, it's possible to obtain a vtable type for a concept with
  607. unbound type parameters or one instantiated with metatypes (type classes),
  608. but it will include a smaller number of captured procs. A completely empty
  609. vtable will be reported as an error.
  610. The `vtref` magic produces types which can be bound to `ref` types and
  611. the `vtptr` magic produced types bound to `ptr` types.
  612. Type bound operations
  613. =====================
  614. There are 4 operations that are bound to a type:
  615. 1. Assignment
  616. 2. Moves
  617. 3. Destruction
  618. 4. Deep copying for communication between threads
  619. These operations can be *overridden* instead of *overloaded*. This means the
  620. implementation is automatically lifted to structured types. For instance if type
  621. `T` has an overridden assignment operator `=` this operator is also used
  622. for assignments of the type `seq[T]`. Since these operations are bound to a
  623. type they have to be bound to a nominal type for reasons of simplicity of
  624. implementation: This means an overridden `deepCopy` for `ref T` is really
  625. bound to `T` and not to `ref T`. This also means that one cannot override
  626. `deepCopy` for both `ptr T` and `ref T` at the same time; instead a
  627. helper distinct or object type has to be used for one pointer type.
  628. Assignments, moves and destruction are specified in
  629. the `destructors <destructors.html>`_ document.
  630. deepCopy
  631. --------
  632. `=deepCopy` is a builtin that is invoked whenever data is passed to
  633. a `spawn`'ed proc to ensure memory safety. The programmer can override its
  634. behaviour for a specific `ref` or `ptr` type `T`. (Later versions of the
  635. language may weaken this restriction.)
  636. The signature has to be:
  637. .. code-block:: nim
  638. proc `=deepCopy`(x: T): T
  639. This mechanism will be used by most data structures that support shared memory
  640. like channels to implement thread safe automatic memory management.
  641. The builtin `deepCopy` can even clone closures and their environments. See
  642. the documentation of `spawn <#parallel-amp-spawn-spawn-statement>`_ for details.
  643. Case statement macros
  644. =====================
  645. Macros named `case` can rewrite `case` statements for certain types in order to
  646. implement `pattern matching`:idx:. The following example implements a
  647. simplistic form of pattern matching for tuples, leveraging the existing
  648. equality operator for tuples (as provided in `system.==`):
  649. .. code-block:: nim
  650. :test: "nim c $1"
  651. {.experimental: "caseStmtMacros".}
  652. import std/macros
  653. macro `case`(n: tuple): untyped =
  654. result = newTree(nnkIfStmt)
  655. let selector = n[0]
  656. for i in 1 ..< n.len:
  657. let it = n[i]
  658. case it.kind
  659. of nnkElse, nnkElifBranch, nnkElifExpr, nnkElseExpr:
  660. result.add it
  661. of nnkOfBranch:
  662. for j in 0..it.len-2:
  663. let cond = newCall("==", selector, it[j])
  664. result.add newTree(nnkElifBranch, cond, it[^1])
  665. else:
  666. error "custom 'case' for tuple cannot handle this node", it
  667. case ("foo", 78)
  668. of ("foo", 78): echo "yes"
  669. of ("bar", 88): echo "no"
  670. else: discard
  671. Currently case statement macros must be enabled explicitly
  672. via `{.experimental: "caseStmtMacros".}`.
  673. `case` macros are subject to overload resolution. The type of the
  674. `case` statement's selector expression is matched against the type
  675. of the first argument of the `case` macro. Then the complete `case`
  676. statement is passed in place of the argument and the macro is evaluated.
  677. In other words, the macro needs to transform the full `case` statement
  678. but only the statement's selector expression is used to determine which
  679. macro to call.
  680. Term rewriting macros
  681. =====================
  682. Term rewriting macros are macros or templates that have not only
  683. a *name* but also a *pattern* that is searched for after the semantic checking
  684. phase of the compiler: This means they provide an easy way to enhance the
  685. compilation pipeline with user defined optimizations:
  686. .. code-block:: nim
  687. template optMul{`*`(a, 2)}(a: int): int = a+a
  688. let x = 3
  689. echo x * 2
  690. The compiler now rewrites `x * 2` as `x + x`. The code inside the
  691. curlies is the pattern to match against. The operators `*`, `**`,
  692. `|`, `~` have a special meaning in patterns if they are written in infix
  693. notation, so to match verbatim against `*` the ordinary function call syntax
  694. needs to be used.
  695. Term rewriting macro are applied recursively, up to a limit. This means that
  696. if the result of a term rewriting macro is eligible for another rewriting,
  697. the compiler will try to perform it, and so on, until no more optimizations
  698. are applicable. To avoid putting the compiler into an infinite loop, there is
  699. a hard limit on how many times a single term rewriting macro can be applied.
  700. Once this limit has been passed, the term rewriting macro will be ignored.
  701. Unfortunately optimizations are hard to get right and even the tiny example
  702. is **wrong**:
  703. .. code-block:: nim
  704. template optMul{`*`(a, 2)}(a: int): int = a+a
  705. proc f(): int =
  706. echo "side effect!"
  707. result = 55
  708. echo f() * 2
  709. We cannot duplicate 'a' if it denotes an expression that has a side effect!
  710. Fortunately Nim supports side effect analysis:
  711. .. code-block:: nim
  712. template optMul{`*`(a, 2)}(a: int{noSideEffect}): int = a+a
  713. proc f(): int =
  714. echo "side effect!"
  715. result = 55
  716. echo f() * 2 # not optimized ;-)
  717. You can make one overload matching with a constraint and one without, and the
  718. one with a constraint will have precedence, and so you can handle both cases
  719. differently.
  720. So what about `2 * a`? We should tell the compiler `*` is commutative. We
  721. cannot really do that however as the following code only swaps arguments
  722. blindly:
  723. .. code-block:: nim
  724. template mulIsCommutative{`*`(a, b)}(a, b: int): int = b*a
  725. What optimizers really need to do is a *canonicalization*:
  726. .. code-block:: nim
  727. template canonMul{`*`(a, b)}(a: int{lit}, b: int): int = b*a
  728. The `int{lit}` parameter pattern matches against an expression of
  729. type `int`, but only if it's a literal.
  730. Parameter constraints
  731. ---------------------
  732. The `parameter constraint`:idx: expression can use the operators `|` (or),
  733. `&` (and) and `~` (not) and the following predicates:
  734. =================== =====================================================
  735. Predicate Meaning
  736. =================== =====================================================
  737. `atom` The matching node has no children.
  738. `lit` The matching node is a literal like `"abc"`, `12`.
  739. `sym` The matching node must be a symbol (a bound
  740. identifier).
  741. `ident` The matching node must be an identifier (an unbound
  742. identifier).
  743. `call` The matching AST must be a call/apply expression.
  744. `lvalue` The matching AST must be an lvalue.
  745. `sideeffect` The matching AST must have a side effect.
  746. `nosideeffect` The matching AST must have no side effect.
  747. `param` A symbol which is a parameter.
  748. `genericparam` A symbol which is a generic parameter.
  749. `module` A symbol which is a module.
  750. `type` A symbol which is a type.
  751. `var` A symbol which is a variable.
  752. `let` A symbol which is a `let` variable.
  753. `const` A symbol which is a constant.
  754. `result` The special `result` variable.
  755. `proc` A symbol which is a proc.
  756. `method` A symbol which is a method.
  757. `iterator` A symbol which is an iterator.
  758. `converter` A symbol which is a converter.
  759. `macro` A symbol which is a macro.
  760. `template` A symbol which is a template.
  761. `field` A symbol which is a field in a tuple or an object.
  762. `enumfield` A symbol which is a field in an enumeration.
  763. `forvar` A for loop variable.
  764. `label` A label (used in `block` statements).
  765. `nk*` The matching AST must have the specified kind.
  766. (Example: `nkIfStmt` denotes an `if` statement.)
  767. `alias` States that the marked parameter needs to alias
  768. with *some* other parameter.
  769. `noalias` States that *every* other parameter must not alias
  770. with the marked parameter.
  771. =================== =====================================================
  772. Predicates that share their name with a keyword have to be escaped with
  773. backticks.
  774. The `alias` and `noalias` predicates refer not only to the matching AST,
  775. but also to every other bound parameter; syntactically they need to occur after
  776. the ordinary AST predicates:
  777. .. code-block:: nim
  778. template ex{a = b + c}(a: int{noalias}, b, c: int) =
  779. # this transformation is only valid if 'b' and 'c' do not alias 'a':
  780. a = b
  781. inc a, c
  782. Another example:
  783. .. code-block:: nim
  784. proc somefunc(s: string) = assert s == "variable"
  785. proc somefunc(s: string{nkStrLit}) = assert s == "literal"
  786. proc somefunc(s: string{nkRStrLit}) = assert s == r"raw"
  787. proc somefunc(s: string{nkTripleStrLit}) = assert s == """triple"""
  788. proc somefunc(s: static[string]) = assert s == "constant"
  789. # Use parameter constraints to provide overloads based on both the input parameter type and form.
  790. var variable = "variable"
  791. somefunc(variable)
  792. const constant = "constant"
  793. somefunc(constant)
  794. somefunc("literal")
  795. somefunc(r"raw")
  796. somefunc("""triple""")
  797. Pattern operators
  798. -----------------
  799. The operators `*`, `**`, `|`, `~` have a special meaning in patterns
  800. if they are written in infix notation.
  801. The `|` operator
  802. ~~~~~~~~~~~~~~~~~~
  803. The `|` operator if used as infix operator creates an ordered choice:
  804. .. code-block:: nim
  805. template t{0|1}(): untyped = 3
  806. let a = 1
  807. # outputs 3:
  808. echo a
  809. The matching is performed after the compiler performed some optimizations like
  810. constant folding, so the following does not work:
  811. .. code-block:: nim
  812. template t{0|1}(): untyped = 3
  813. # outputs 1:
  814. echo 1
  815. The reason is that the compiler already transformed the 1 into "1" for
  816. the `echo` statement. However, a term rewriting macro should not change the
  817. semantics anyway. In fact they can be deactivated with the `--patterns:off`:option:
  818. command line option or temporarily with the `patterns` pragma.
  819. The `{}` operator
  820. ~~~~~~~~~~~~~~~~~~~
  821. A pattern expression can be bound to a pattern parameter via the `expr{param}`
  822. notation:
  823. .. code-block:: nim
  824. template t{(0|1|2){x}}(x: untyped): untyped = x+1
  825. let a = 1
  826. # outputs 2:
  827. echo a
  828. The `~` operator
  829. ~~~~~~~~~~~~~~~~~~
  830. The `~` operator is the **not** operator in patterns:
  831. .. code-block:: nim
  832. template t{x = (~x){y} and (~x){z}}(x, y, z: bool) =
  833. x = y
  834. if x: x = z
  835. var
  836. a = false
  837. b = true
  838. c = false
  839. a = b and c
  840. echo a
  841. The `*` operator
  842. ~~~~~~~~~~~~~~~~~~
  843. The `*` operator can *flatten* a nested binary expression like `a & b & c`
  844. to `&(a, b, c)`:
  845. .. code-block:: nim
  846. var
  847. calls = 0
  848. proc `&&`(s: varargs[string]): string =
  849. result = s[0]
  850. for i in 1..len(s)-1: result.add s[i]
  851. inc calls
  852. template optConc{ `&&` * a }(a: string): untyped = &&a
  853. let space = " "
  854. echo "my" && (space & "awe" && "some " ) && "concat"
  855. # check that it's been optimized properly:
  856. doAssert calls == 1
  857. The second operator of `*` must be a parameter; it is used to gather all the
  858. arguments. The expression `"my" && (space & "awe" && "some " ) && "concat"`
  859. is passed to `optConc` in `a` as a special list (of kind `nkArgList`)
  860. which is flattened into a call expression; thus the invocation of `optConc`
  861. produces:
  862. .. code-block:: nim
  863. `&&`("my", space & "awe", "some ", "concat")
  864. The `**` operator
  865. ~~~~~~~~~~~~~~~~~~~
  866. The `**` is much like the `*` operator, except that it gathers not only
  867. all the arguments, but also the matched operators in reverse polish notation:
  868. .. code-block:: nim
  869. import std/macros
  870. type
  871. Matrix = object
  872. dummy: int
  873. proc `*`(a, b: Matrix): Matrix = discard
  874. proc `+`(a, b: Matrix): Matrix = discard
  875. proc `-`(a, b: Matrix): Matrix = discard
  876. proc `$`(a: Matrix): string = result = $a.dummy
  877. proc mat21(): Matrix =
  878. result.dummy = 21
  879. macro optM{ (`+`|`-`|`*`) ** a }(a: Matrix): untyped =
  880. echo treeRepr(a)
  881. result = newCall(bindSym"mat21")
  882. var x, y, z: Matrix
  883. echo x + y * z - x
  884. This passes the expression `x + y * z - x` to the `optM` macro as
  885. an `nnkArgList` node containing::
  886. Arglist
  887. Sym "x"
  888. Sym "y"
  889. Sym "z"
  890. Sym "*"
  891. Sym "+"
  892. Sym "x"
  893. Sym "-"
  894. (Which is the reverse polish notation of `x + y * z - x`.)
  895. Parameters
  896. ----------
  897. Parameters in a pattern are type checked in the matching process. If a
  898. parameter is of the type `varargs` it is treated specially and it can match
  899. 0 or more arguments in the AST to be matched against:
  900. .. code-block:: nim
  901. template optWrite{
  902. write(f, x)
  903. ((write|writeLine){w})(f, y)
  904. }(x, y: varargs[untyped], f: File, w: untyped) =
  905. w(f, x, y)
  906. Example: Partial evaluation
  907. ---------------------------
  908. The following example shows how some simple partial evaluation can be
  909. implemented with term rewriting:
  910. .. code-block:: nim
  911. proc p(x, y: int; cond: bool): int =
  912. result = if cond: x + y else: x - y
  913. template optP1{p(x, y, true)}(x, y: untyped): untyped = x + y
  914. template optP2{p(x, y, false)}(x, y: untyped): untyped = x - y
  915. Example: Hoisting
  916. -----------------
  917. The following example shows how some form of hoisting can be implemented:
  918. .. code-block:: nim
  919. import std/pegs
  920. template optPeg{peg(pattern)}(pattern: string{lit}): Peg =
  921. var gl {.global, gensym.} = peg(pattern)
  922. gl
  923. for i in 0 .. 3:
  924. echo match("(a b c)", peg"'(' @ ')'")
  925. echo match("W_HI_Le", peg"\y 'while'")
  926. The `optPeg` template optimizes the case of a peg constructor with a string
  927. literal, so that the pattern will only be parsed once at program startup and
  928. stored in a global `gl` which is then re-used. This optimization is called
  929. hoisting because it is comparable to classical loop hoisting.
  930. AST based overloading
  931. =====================
  932. Parameter constraints can also be used for ordinary routine parameters; these
  933. constraints affect ordinary overloading resolution then:
  934. .. code-block:: nim
  935. proc optLit(a: string{lit|`const`}) =
  936. echo "string literal"
  937. proc optLit(a: string) =
  938. echo "no string literal"
  939. const
  940. constant = "abc"
  941. var
  942. variable = "xyz"
  943. optLit("literal")
  944. optLit(constant)
  945. optLit(variable)
  946. However, the constraints `alias` and `noalias` are not available in
  947. ordinary routines.
  948. Parallel & Spawn
  949. ================
  950. Nim has two flavors of parallelism:
  951. 1) `Structured`:idx: parallelism via the `parallel` statement.
  952. 2) `Unstructured`:idx: parallelism via the standalone `spawn` statement.
  953. Nim has a builtin thread pool that can be used for CPU intensive tasks. For
  954. IO intensive tasks the `async` and `await` features should be
  955. used instead. Both parallel and spawn need the `threadpool <threadpool.html>`_
  956. module to work.
  957. Somewhat confusingly, `spawn` is also used in the `parallel` statement
  958. with slightly different semantics. `spawn` always takes a call expression of
  959. the form `f(a, ...)`. Let `T` be `f`'s return type. If `T` is `void`
  960. then `spawn`'s return type is also `void` otherwise it is `FlowVar[T]`.
  961. Within a `parallel` section sometimes the `FlowVar[T]` is eliminated
  962. to `T`. This happens when `T` does not contain any GC'ed memory.
  963. The compiler can ensure the location in `location = spawn f(...)` is not
  964. read prematurely within a `parallel` section and so there is no need for
  965. the overhead of an indirection via `FlowVar[T]` to ensure correctness.
  966. **Note**: Currently exceptions are not propagated between `spawn`'ed tasks!
  967. Spawn statement
  968. ---------------
  969. `spawn`:idx: can be used to pass a task to the thread pool:
  970. .. code-block:: nim
  971. import std/threadpool
  972. proc processLine(line: string) =
  973. discard "do some heavy lifting here"
  974. for x in lines("myinput.txt"):
  975. spawn processLine(x)
  976. sync()
  977. For reasons of type safety and implementation simplicity the expression
  978. that `spawn` takes is restricted:
  979. * It must be a call expression `f(a, ...)`.
  980. * `f` must be `gcsafe`.
  981. * `f` must not have the calling convention `closure`.
  982. * `f`'s parameters may not be of type `var`.
  983. This means one has to use raw `ptr`'s for data passing reminding the
  984. programmer to be careful.
  985. * `ref` parameters are deeply copied which is a subtle semantic change and
  986. can cause performance problems but ensures memory safety. This deep copy
  987. is performed via `system.deepCopy` and so can be overridden.
  988. * For *safe* data exchange between `f` and the caller a global `TChannel`
  989. needs to be used. However, since spawn can return a result, often no further
  990. communication is required.
  991. `spawn` executes the passed expression on the thread pool and returns
  992. a `data flow variable`:idx: `FlowVar[T]` that can be read from. The reading
  993. with the `^` operator is **blocking**. However, one can use `blockUntilAny` to
  994. wait on multiple flow variables at the same time:
  995. .. code-block:: nim
  996. import std/threadpool, ...
  997. # wait until 2 out of 3 servers received the update:
  998. proc main =
  999. var responses = newSeq[FlowVarBase](3)
  1000. for i in 0..2:
  1001. responses[i] = spawn tellServer(Update, "key", "value")
  1002. var index = blockUntilAny(responses)
  1003. assert index >= 0
  1004. responses.del(index)
  1005. discard blockUntilAny(responses)
  1006. Data flow variables ensure that no data races
  1007. are possible. Due to technical limitations not every type `T` is possible in
  1008. a data flow variable: `T` has to be of the type `ref`, `string`, `seq`
  1009. or of a type that doesn't contain a type that is garbage collected. This
  1010. restriction is not hard to work-around in practice.
  1011. Parallel statement
  1012. ------------------
  1013. Example:
  1014. .. code-block:: nim
  1015. :test: "nim c --threads:on $1"
  1016. # Compute PI in an inefficient way
  1017. import std/[strutils, math, threadpool]
  1018. {.experimental: "parallel".}
  1019. proc term(k: float): float = 4 * math.pow(-1, k) / (2*k + 1)
  1020. proc pi(n: int): float =
  1021. var ch = newSeq[float](n+1)
  1022. parallel:
  1023. for k in 0..ch.high:
  1024. ch[k] = spawn term(float(k))
  1025. for k in 0..ch.high:
  1026. result += ch[k]
  1027. echo formatFloat(pi(5000))
  1028. The parallel statement is the preferred mechanism to introduce parallelism in a
  1029. Nim program. A subset of the Nim language is valid within a `parallel`
  1030. section. This subset is checked during semantic analysis to be free of data
  1031. races. A sophisticated `disjoint checker`:idx: ensures that no data races are
  1032. possible even though shared memory is extensively supported!
  1033. The subset is in fact the full language with the following
  1034. restrictions / changes:
  1035. * `spawn` within a `parallel` section has special semantics.
  1036. * Every location of the form `a[i]` and `a[i..j]` and `dest` where
  1037. `dest` is part of the pattern `dest = spawn f(...)` has to be
  1038. provably disjoint. This is called the *disjoint check*.
  1039. * Every other complex location `loc` that is used in a spawned
  1040. proc (`spawn f(loc)`) has to be immutable for the duration of
  1041. the `parallel` section. This is called the *immutability check*. Currently
  1042. it is not specified what exactly "complex location" means. We need to make
  1043. this an optimization!
  1044. * Every array access has to be provably within bounds. This is called
  1045. the *bounds check*.
  1046. * Slices are optimized so that no copy is performed. This optimization is not
  1047. yet performed for ordinary slices outside of a `parallel` section.
  1048. Guards and locks
  1049. ================
  1050. Apart from `spawn` and `parallel` Nim also provides all the common low level
  1051. concurrency mechanisms like locks, atomic intrinsics or condition variables.
  1052. Nim significantly improves on the safety of these features via additional
  1053. pragmas:
  1054. 1) A `guard`:idx: annotation is introduced to prevent data races.
  1055. 2) Every access of a guarded memory location needs to happen in an
  1056. appropriate `locks`:idx: statement.
  1057. 3) Locks and routines can be annotated with `lock levels`:idx: to allow
  1058. potential deadlocks to be detected during semantic analysis.
  1059. Guards and the locks section
  1060. ----------------------------
  1061. Protecting global variables
  1062. ~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1063. Object fields and global variables can be annotated via a `guard` pragma:
  1064. .. code-block:: nim
  1065. var glock: TLock
  1066. var gdata {.guard: glock.}: int
  1067. The compiler then ensures that every access of `gdata` is within a `locks`
  1068. section:
  1069. .. code-block:: nim
  1070. proc invalid =
  1071. # invalid: unguarded access:
  1072. echo gdata
  1073. proc valid =
  1074. # valid access:
  1075. {.locks: [glock].}:
  1076. echo gdata
  1077. Top level accesses to `gdata` are always allowed so that it can be initialized
  1078. conveniently. It is *assumed* (but not enforced) that every top level statement
  1079. is executed before any concurrent action happens.
  1080. The `locks` section deliberately looks ugly because it has no runtime
  1081. semantics and should not be used directly! It should only be used in templates
  1082. that also implement some form of locking at runtime:
  1083. .. code-block:: nim
  1084. template lock(a: TLock; body: untyped) =
  1085. pthread_mutex_lock(a)
  1086. {.locks: [a].}:
  1087. try:
  1088. body
  1089. finally:
  1090. pthread_mutex_unlock(a)
  1091. The guard does not need to be of any particular type. It is flexible enough to
  1092. model low level lockfree mechanisms:
  1093. .. code-block:: nim
  1094. var dummyLock {.compileTime.}: int
  1095. var atomicCounter {.guard: dummyLock.}: int
  1096. template atomicRead(x): untyped =
  1097. {.locks: [dummyLock].}:
  1098. memoryReadBarrier()
  1099. x
  1100. echo atomicRead(atomicCounter)
  1101. The `locks` pragma takes a list of lock expressions `locks: [a, b, ...]`
  1102. in order to support *multi lock* statements. Why these are essential is
  1103. explained in the `lock levels <#guards-and-locks-lock-levels>`_ section.
  1104. Protecting general locations
  1105. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1106. The `guard` annotation can also be used to protect fields within an object.
  1107. The guard then needs to be another field within the same object or a
  1108. global variable.
  1109. Since objects can reside on the heap or on the stack this greatly enhances the
  1110. expressivity of the language:
  1111. .. code-block:: nim
  1112. type
  1113. ProtectedCounter = object
  1114. v {.guard: L.}: int
  1115. L: TLock
  1116. proc incCounters(counters: var openArray[ProtectedCounter]) =
  1117. for i in 0..counters.high:
  1118. lock counters[i].L:
  1119. inc counters[i].v
  1120. The access to field `x.v` is allowed since its guard `x.L` is active.
  1121. After template expansion, this amounts to:
  1122. .. code-block:: nim
  1123. proc incCounters(counters: var openArray[ProtectedCounter]) =
  1124. for i in 0..counters.high:
  1125. pthread_mutex_lock(counters[i].L)
  1126. {.locks: [counters[i].L].}:
  1127. try:
  1128. inc counters[i].v
  1129. finally:
  1130. pthread_mutex_unlock(counters[i].L)
  1131. There is an analysis that checks that `counters[i].L` is the lock that
  1132. corresponds to the protected location `counters[i].v`. This analysis is called
  1133. `path analysis`:idx: because it deals with paths to locations
  1134. like `obj.field[i].fieldB[j]`.
  1135. The path analysis is **currently unsound**, but that doesn't make it useless.
  1136. Two paths are considered equivalent if they are syntactically the same.
  1137. This means the following compiles (for now) even though it really should not:
  1138. .. code-block:: nim
  1139. {.locks: [a[i].L].}:
  1140. inc i
  1141. access a[i].v
  1142. Lock levels
  1143. -----------
  1144. Lock levels are used to enforce a global locking order in order to detect
  1145. potential deadlocks during semantic analysis. A lock level is an constant
  1146. integer in the range 0..1_000. Lock level 0 means that no lock is acquired at
  1147. all.
  1148. If a section of code holds a lock of level `M` than it can also acquire any
  1149. lock of level `N < M`. Another lock of level `M` cannot be acquired. Locks
  1150. of the same level can only be acquired *at the same time* within a
  1151. single `locks` section:
  1152. .. code-block:: nim
  1153. var a, b: TLock[2]
  1154. var x: TLock[1]
  1155. # invalid locking order: TLock[1] cannot be acquired before TLock[2]:
  1156. {.locks: [x].}:
  1157. {.locks: [a].}:
  1158. ...
  1159. # valid locking order: TLock[2] acquired before TLock[1]:
  1160. {.locks: [a].}:
  1161. {.locks: [x].}:
  1162. ...
  1163. # invalid locking order: TLock[2] acquired before TLock[2]:
  1164. {.locks: [a].}:
  1165. {.locks: [b].}:
  1166. ...
  1167. # valid locking order, locks of the same level acquired at the same time:
  1168. {.locks: [a, b].}:
  1169. ...
  1170. Here is how a typical multilock statement can be implemented in Nim. Note how
  1171. the runtime check is required to ensure a global ordering for two locks `a`
  1172. and `b` of the same lock level:
  1173. .. code-block:: nim
  1174. template multilock(a, b: ptr TLock; body: untyped) =
  1175. if cast[ByteAddress](a) < cast[ByteAddress](b):
  1176. pthread_mutex_lock(a)
  1177. pthread_mutex_lock(b)
  1178. else:
  1179. pthread_mutex_lock(b)
  1180. pthread_mutex_lock(a)
  1181. {.locks: [a, b].}:
  1182. try:
  1183. body
  1184. finally:
  1185. pthread_mutex_unlock(a)
  1186. pthread_mutex_unlock(b)
  1187. Whole routines can also be annotated with a `locks` pragma that takes a lock
  1188. level. This then means that the routine may acquire locks of up to this level.
  1189. This is essential so that procs can be called within a `locks` section:
  1190. .. code-block:: nim
  1191. proc p() {.locks: 3.} = discard
  1192. var a: TLock[4]
  1193. {.locks: [a].}:
  1194. # p's locklevel (3) is strictly less than a's (4) so the call is allowed:
  1195. p()
  1196. As usual `locks` is an inferred effect and there is a subtype
  1197. relation: `proc () {.locks: N.}` is a subtype of `proc () {.locks: M.}`
  1198. iff (M <= N).
  1199. The `locks` pragma can also take the special value `"unknown"`. This
  1200. is useful in the context of dynamic method dispatching. In the following
  1201. example, the compiler can infer a lock level of 0 for the `base` case.
  1202. However, one of the overloaded methods calls a procvar which is
  1203. potentially locking. Thus, the lock level of calling `g.testMethod`
  1204. cannot be inferred statically, leading to compiler warnings. By using
  1205. `{.locks: "unknown".}`, the base method can be marked explicitly as
  1206. having unknown lock level as well:
  1207. .. code-block:: nim
  1208. type SomeBase* = ref object of RootObj
  1209. type SomeDerived* = ref object of SomeBase
  1210. memberProc*: proc ()
  1211. method testMethod(g: SomeBase) {.base, locks: "unknown".} = discard
  1212. method testMethod(g: SomeDerived) =
  1213. if g.memberProc != nil:
  1214. g.memberProc()
  1215. noRewrite pragma
  1216. ----------------
  1217. Term rewriting macros and templates are currently greedy and
  1218. they will rewrite as long as there is a match.
  1219. There was no way to ensure some rewrite happens only once,
  1220. e.g. when rewriting term to same term plus extra content.
  1221. `noRewrite` pragma can actually prevent further rewriting on marked code,
  1222. e.g. with given example `echo("ab")` will be rewritten just once:
  1223. .. code-block:: nim
  1224. template pwnEcho{echo(x)}(x: untyped) =
  1225. {.noRewrite.}: echo("pwned!")
  1226. echo "ab"
  1227. `noRewrite` pragma can be useful to control term-rewriting macros recursion.
  1228. Aliasing restrictions in parameter passing
  1229. ==========================================
  1230. **Note**: The aliasing restrictions are currently not enforced by the
  1231. implementation and need to be fleshed out further.
  1232. "Aliasing" here means that the underlying storage locations overlap in memory
  1233. at runtime. An "output parameter" is a parameter of type `var T`,
  1234. an input parameter is any parameter that is not of type `var`.
  1235. 1. Two output parameters should never be aliased.
  1236. 2. An input and an output parameter should not be aliased.
  1237. 3. An output parameter should never be aliased with a global or thread local
  1238. variable referenced by the called proc.
  1239. 4. An input parameter should not be aliased with a global or thread local
  1240. variable updated by the called proc.
  1241. One problem with rules 3 and 4 is that they affect specific global or thread
  1242. local variables, but Nim's effect tracking only tracks "uses no global variable"
  1243. via `.noSideEffect`. The rules 3 and 4 can also be approximated by a different rule:
  1244. 5. A global or thread local variable (or a location derived from such a location)
  1245. can only passed to a parameter of a `.noSideEffect` proc.
  1246. Noalias annotation
  1247. ==================
  1248. Since version 1.4 of the Nim compiler, there is a `.noalias` annotation for variables
  1249. and parameters. It is mapped directly to C/C++'s `restrict`:c: keyword and means that
  1250. the underlying pointer is pointing to a unique location in memory, no other aliases to
  1251. this location exist. It is *unchecked* that this alias restriction is followed, if the
  1252. restriction is violated, the backend optimizer is free to miscompile the code.
  1253. This is an **unsafe** language feature.
  1254. Ideally in later versions of the language, the restriction will be enforced at
  1255. compile time. (Which is also why the name `noalias` was choosen instead of a more
  1256. verbose name like `unsafeAssumeNoAlias`.)
  1257. Strict funcs
  1258. ============
  1259. Since version 1.4 a stricter definition of "side effect" is available. In addition
  1260. to the existing rule that a side effect is calling a function with side effects
  1261. the following rule is also enforced:
  1262. Any mutation to an object does count as a side effect if that object is reachable
  1263. via a parameter that is not declared as a `var` parameter.
  1264. For example:
  1265. .. code-block:: nim
  1266. {.experimental: "strictFuncs".}
  1267. type
  1268. Node = ref object
  1269. le, ri: Node
  1270. data: string
  1271. func len(n: Node): int =
  1272. # valid: len does not have side effects
  1273. var it = n
  1274. while it != nil:
  1275. inc result
  1276. it = it.ri
  1277. func mut(n: Node) =
  1278. let m = n # is the statement that connected the mutation to the parameter
  1279. m.data = "yeah" # the mutation is here
  1280. # Error: 'mut' can have side effects
  1281. # an object reachable from 'n' is potentially mutated
  1282. The algorithm behind this analysis is described in
  1283. the `view types section <#view-types-algorithm>`_.
  1284. View types
  1285. ==========
  1286. **Note**: `--experimental:views`:option: is more effective
  1287. with `--experimental:strictFuncs`:option:.
  1288. A view type is a type that is or contains one of the following types:
  1289. - `lent T` (view into `T`)
  1290. - `openArray[T]` (pair of (pointer to array of `T`, size))
  1291. For example:
  1292. .. code-block:: nim
  1293. type
  1294. View1 = openArray[byte]
  1295. View2 = lent string
  1296. View3 = Table[openArray[char], int]
  1297. Exceptions to this rule are types constructed via `ptr` or `proc`.
  1298. For example, the following types are **not** view types:
  1299. .. code-block:: nim
  1300. type
  1301. NotView1 = proc (x: openArray[int])
  1302. NotView2 = ptr openArray[char]
  1303. NotView3 = ptr array[4, lent int]
  1304. The mutability aspect of a view type is not part of the type but part
  1305. of the locations it's derived from. More on this later.
  1306. A *view* is a symbol (a let, var, const, etc.) that has a view type.
  1307. Since version 1.4 Nim allows view types to be used as local variables.
  1308. This feature needs to be enabled via `{.experimental: "views".}`.
  1309. A local variable of a view type *borrows* from the locations and
  1310. it is statically enforced that the view does not outlive the location
  1311. it was borrowed from.
  1312. For example:
  1313. .. code-block:: nim
  1314. {.experimental: "views".}
  1315. proc take(a: openArray[int]) =
  1316. echo a.len
  1317. proc main(s: seq[int]) =
  1318. var x: openArray[int] = s # 'x' is a view into 's'
  1319. # it is checked that 'x' does not outlive 's' and
  1320. # that 's' is not mutated.
  1321. for i in 0 .. high(x):
  1322. echo x[i]
  1323. take(x)
  1324. take(x.toOpenArray(0, 1)) # slicing remains possible
  1325. let y = x # create a view from a view
  1326. take y
  1327. # it is checked that 'y' does not outlive 'x' and
  1328. # that 'x' is not mutated as long as 'y' lives.
  1329. main(@[11, 22, 33])
  1330. A local variable of a view type can borrow from a location
  1331. derived from a parameter, another local variable, a global `const` or `let`
  1332. symbol or a thread-local `var` or `let`.
  1333. Let `p` the proc that is analysed for the correctness of the borrow operation.
  1334. Let `source` be one of:
  1335. - A formal parameter of `p`. Note that this does not cover parameters of
  1336. inner procs.
  1337. - The `result` symbol of `p`.
  1338. - A local `var` or `let` or `const` of `p`. Note that this does
  1339. not cover locals of inner procs.
  1340. - A thread-local `var` or `let`.
  1341. - A global `let` or `const`.
  1342. - A constant array/seq/object/tuple constructor.
  1343. Path expressions
  1344. ----------------
  1345. A location derived from `source` is then defined as a path expression that
  1346. has `source` as the owner. A path expression `e` is defined recursively:
  1347. - `source` itself is a path expression.
  1348. - Container access like `e[i]` is a path expression.
  1349. - Tuple access `e[0]` is a path expression.
  1350. - Object field access `e.field` is a path expression.
  1351. - `system.toOpenArray(e, ...)` is a path expression.
  1352. - Pointer dereference `e[]` is a path expression.
  1353. - An address `addr e`, `unsafeAddr e` is a path expression.
  1354. - A type conversion `T(e)` is a path expression.
  1355. - A cast expression `cast[T](e)` is a path expression.
  1356. - `f(e, ...)` is a path expression if `f`'s return type is a view type.
  1357. Because the view can only have been borrowed from `e`, we then know
  1358. that owner of `f(e, ...)` is `e`.
  1359. If a view type is used as a return type, the location must borrow from a location
  1360. that is derived from the first parameter that is passed to the proc.
  1361. See https://nim-lang.org/docs/manual.html#procedures-var-return-type for
  1362. details about how this is done for `var T`.
  1363. A mutable view can borrow from a mutable location, an immutable view can borrow
  1364. from both a mutable or an immutable location.
  1365. If a view borrows from a mutable location, the view can be used to update the
  1366. location. Otherwise it cannot be used for mutations.
  1367. The *duration* of a borrow is the span of commands beginning from the assignment
  1368. to the view and ending with the last usage of the view.
  1369. For the duration of the borrow operation, no mutations to the borrowed locations
  1370. may be performed except via the view that borrowed from the
  1371. location. The borrowed location is said to be *sealed* during the borrow.
  1372. .. code-block:: nim
  1373. {.experimental: "views".}
  1374. type
  1375. Obj = object
  1376. field: string
  1377. proc dangerous(s: var seq[Obj]) =
  1378. let v: lent Obj = s[0] # seal 's'
  1379. s.setLen 0 # prevented at compile-time because 's' is sealed.
  1380. echo v.field
  1381. The scope of the view does not matter:
  1382. .. code-block:: nim
  1383. proc valid(s: var seq[Obj]) =
  1384. let v: lent Obj = s[0] # begin of borrow
  1385. echo v.field # end of borrow
  1386. s.setLen 0 # valid because 'v' isn't used afterwards
  1387. The analysis requires as much precision about mutations as is reasonably obtainable,
  1388. so it is more effective with the experimental `strict funcs <#strict-funcs>`_
  1389. feature. In other words `--experimental:views`:option: works better
  1390. with `--experimental:strictFuncs`:option:.
  1391. The analysis is currently control flow insensitive:
  1392. .. code-block:: nim
  1393. proc invalid(s: var seq[Obj]) =
  1394. let v: lent Obj = s[0]
  1395. if false:
  1396. s.setLen 0
  1397. echo v.field
  1398. In this example, the compiler assumes that `s.setLen 0` invalidates the
  1399. borrow operation of `v` even though a human being can easily see that it
  1400. will never do that at runtime.
  1401. Start of a borrow
  1402. -----------------
  1403. A borrow starts with one of the following:
  1404. - The assignment of a non-view-type to a view-type.
  1405. - The assignment of a location that is derived from a local parameter
  1406. to a view-type.
  1407. If a non l-value is borrowed from, as in:
  1408. .. code-block:: nim
  1409. func f(): Obj
  1410. let v: lent Obj = f()
  1411. Then it is turned into a borrow from an l-value:
  1412. .. code-block:: nim
  1413. func f(): Obj
  1414. let tmp = f()
  1415. let v: lent Obj = tmp
  1416. Such a borrow is only supported for local variables of type `lent T`. This
  1417. limitation might be removed in later versions of the language.
  1418. End of a borrow
  1419. ---------------
  1420. A borrow operation ends with the last usage of the view variable.
  1421. Reborrows
  1422. ---------
  1423. A view `v` can borrow from multiple different locations. However, the borrow
  1424. is always the full span of `v`'s lifetime and every location that is borrowed
  1425. from is sealed during `v`'s lifetime.
  1426. Algorithm
  1427. ---------
  1428. The following section is an outline of the algorithm that the current implementation
  1429. uses. The algorithm performs two traversals over the AST of the procedure or global
  1430. section of code that uses a view variable. No fixpoint iterations are performed, the
  1431. complexity of the analysis is O(N) where N is the number of nodes of the AST.
  1432. The first pass over the AST computes the lifetime of each local variable based on
  1433. a notion of an "abstract time", in the implementation it's a simple integer that is
  1434. incremented for every visited node.
  1435. In the second pass information about the underlying object "graphs" is computed.
  1436. Let `v` be a parameter or a local variable. Let `G(v)` be the graph
  1437. that `v` belongs to. A graph is defined by the set of variables that belong
  1438. to the graph. Initially for all `v`: `G(v) = {v}`. Every variable can only
  1439. be part of a single graph.
  1440. Assignments like `a = b` "connect" two variables, both variables end up in the
  1441. same graph `{a, b} = G(a) = G(b)`. Unfortunately, the pattern to look for is
  1442. much more complex than that and can involve multiple assignment targets
  1443. and sources::
  1444. f(x, y) = g(a, b)
  1445. connects `x` and `y` to `a` and `b`: `G(x) = G(y) = G(a) = G(b) = {x, y, a, b}`.
  1446. A type based alias analysis rules out some of these combinations, for example
  1447. a `string` value cannot possibly be connected to a `seq[int]`.
  1448. A pattern like `v[] = value` or `v.field = value` marks `G(v)` as mutated.
  1449. After the second pass a set of disjoint graphs was computed.
  1450. For strict functions it is then enforced that there is no graph that is both mutated
  1451. and has an element that is an immutable parameter (that is a parameter that is not
  1452. of type `var T`).
  1453. For borrow checking a different set of checks is performed. Let `v` be the view
  1454. and `b` the location that is borrowed from.
  1455. - The lifetime of `v` must not exceed `b`'s lifetime. Note: The lifetime of
  1456. a parameter is the complete proc body.
  1457. - If `v` is used for a mutation, `b` must be a mutable location too.
  1458. - During `v`'s lifetime, `G(b)` can only be modified by `v` (and only if
  1459. `v` is a mutable view).
  1460. - If `v` is `result` then `b` has to be a location derived from the first
  1461. formal parameter or from a constant location.
  1462. - A view cannot be used for a read or a write access before it was assigned to.