diff options
author | metagn <metagngn@gmail.com> | 2023-04-11 10:23:41 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-04-11 09:23:41 +0200 |
commit | 1bb117cd7a49954832d21e6a1502492770acb77b (patch) | |
tree | 299cbbc13c72d8f0e3844a1c9770874a25050a67 /doc | |
parent | 420b0c14eb0b82e05873191b277e889f95bc802b (diff) | |
download | Nim-1bb117cd7a49954832d21e6a1502492770acb77b.tar.gz |
`proc` typeclass accounts for `iterator`, call conventions + `nil` fix + document typeclass AST (#21629)
* test fix #16546 #16548 + another issue * please don't tell me other packages do this * fix CI + test typeclass callconv pragma * better logic in parser * docs and changelog
Diffstat (limited to 'doc')
-rw-r--r-- | doc/astspec.txt | 36 | ||||
-rw-r--r-- | doc/manual.md | 13 |
2 files changed, 46 insertions, 3 deletions
diff --git a/doc/astspec.txt b/doc/astspec.txt index bfaec7155..9929d8ccd 100644 --- a/doc/astspec.txt +++ b/doc/astspec.txt @@ -1348,7 +1348,7 @@ Generic parameters are treated in the type, not the ``proc`` itself. Concrete syntax: ```nim - type MyProc[T] = proc(x: T) + type MyProc[T] = proc(x: T) {.nimcall.} ``` AST: @@ -1363,7 +1363,8 @@ AST: nnkProcTy( # behaves like a procedure declaration from here on nnkFormalParams( # ... - ) + ), + nnkPragma(nnkIdent("nimcall")) ) ) ``` @@ -1371,6 +1372,37 @@ AST: The same syntax applies to ``iterator`` (with ``nnkIteratorTy``), but *does not* apply to ``converter`` or ``template``. +Type class versions of these nodes generally share the same node kind but +without any child nodes. The ``tuple`` type class is represented by +``nnkTupleClassTy``, while a ``proc`` or ``iterator`` type class with pragmas +has an ``nnkEmpty`` node in place of the ``nnkFormalParams`` node of a +concrete ``proc`` or ``iterator`` type node. + + ```nim + type TypeClass = proc {.nimcall.} | ref | tuple + ``` + +AST: + + ```nim + nnkTypeDef( + nnkIdent("TypeClass"), + nnkEmpty(), + nnkInfix( + nnkIdent("|"), + nnkProcTy( + nnkEmpty(), + nnkPragma(nnkIdent("nimcall")) + ), + nnkInfix( + nnkIdent("|"), + nnkRefTy(), + nnkTupleClassTy() + ) + ) + ) + ``` + Mixin statement --------------- diff --git a/doc/manual.md b/doc/manual.md index 042773bcb..f5ee10fda 100644 --- a/doc/manual.md +++ b/doc/manual.md @@ -5467,9 +5467,9 @@ type class matches ================== =================================================== `object` any object type `tuple` any tuple type - `enum` any enumeration `proc` any proc type +`iterator` any iterator type `ref` any `ref` type `ptr` any `ptr` type `var` any `var` type @@ -5529,6 +5529,17 @@ as `type constraints`:idx: of the generic type parameter: onlyIntOrString("xy", 50) # invalid as 'T' cannot be both at the same time ``` +`proc` and `iterator` type classes also accept a calling convention pragma +to restrict the calling convention of the matching `proc` or `iterator` type. + + ```nim + proc onlyClosure[T: proc {.closure.}](x: T) = discard + + onlyClosure(proc() = echo "hello") # valid + proc foo() {.nimcall.} = discard + onlyClosure(foo) # type mismatch + ``` + Implicit generics ----------------- |