123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252 |
- discard """
- output: '''
- i2416
- 33
- foo55
- foo8.0
- fooaha
- bar7
- immediate
- 10
- 4true
- 132
- '''
- """
- import macros
- import i2416
- i2416()
- import mcan_access_hidden_field
- var myfoo = createFoo(33, 44)
- echo myfoo.geta
- import mgensym_generic_cross_module
- foo(55)
- foo 8.0
- foo "aha"
- bar 7
- block generic_templates:
- type
- SomeObj = object of RootObj
- Foo[T, U] = object
- x: T
- y: U
- template someTemplate[T](): tuple[id: int32, obj: T] =
- var result: tuple[id: int32, obj: T] = (0'i32, T())
- result
- let ret = someTemplate[SomeObj]()
- # https://github.com/nim-lang/Nim/issues/7829
- proc inner[T](): int =
- discard
- template outer[A](): untyped =
- inner[A]()
- template outer[B](x: int): untyped =
- inner[B]()
- var i1 = outer[int]()
- var i2 = outer[int](i1)
- # https://github.com/nim-lang/Nim/issues/7883
- template t1[T: int|int64](s: string): T =
- var t: T
- t
- template t1[T: int|int64](x: int, s: string): T =
- var t: T
- t
- var i3: int = t1[int]("xx")
- block tgetast_typeliar:
- proc error(s: string) = quit s
- macro assertOrReturn(condition: bool; message: string): typed =
- var line = condition.lineInfo()
- result = quote do:
- block:
- if not likely(`condition`):
- error("Assertion failed: " & $(`message`) & "\n" & `line`)
- return
- macro assertOrReturn(condition: bool): typed =
- var message = condition.toStrLit()
- result = getAst assertOrReturn(condition, message)
- proc point(size: int16): tuple[x, y: int16] =
- # returns random point in square area with given `size`
- assertOrReturn size > 0
- type
- MyFloat = object
- val: float
- converter to_myfloat(x: float): MyFloat {.inline.} =
- MyFloat(val: x)
- block pattern_with_converter:
- proc `+`(x1, x2: MyFloat): MyFloat =
- MyFloat(val: x1.val + x2.val)
- proc `*`(x1, x2: MyFloat): MyFloat =
- MyFloat(val: x1.val * x2.val)
- template optMul{`*`(a, 2.0)}(a: MyFloat): MyFloat =
- a + a
- func floatMyFloat(x: MyFloat): MyFloat =
- result = x * 2.0
- func floatDouble(x: float): float =
- result = x * 2.0
- doAssert floatDouble(5) == 10.0
- block prefer_immediate:
- # Test that immediate templates are preferred over non-immediate templates
- template foo(a, b: untyped) = echo "foo expr"
- template foo(a, b: int) = echo "foo int"
- template foo(a, b: float) = echo "foo float"
- template foo(a, b: string) = echo "foo string"
- template foo(a, b: untyped) {.immediate.} = echo "immediate"
- template foo(a, b: bool) = echo "foo bool"
- template foo(a, b: char) = echo "foo char"
- foo(undeclaredIdentifier, undeclaredIdentifier2)
- block procparshadow:
- template something(name: untyped) =
- proc name(x: int) =
- var x = x # this one should not be rejected by the compiler (#5225)
- echo x
- something(what)
- what(10)
- # bug #4750
- type
- O = object
- i: int
- OP = ptr O
- template alf(p: pointer): untyped =
- cast[OP](p)
- proc t1(al: pointer) =
- var o = alf(al)
- proc t2(alf: pointer) =
- var x = alf
- var o = alf(x)
- block symchoicefield:
- type Foo = object
- len: int
- var f = Foo(len: 40)
- template getLen(f: Foo): int = f.len
- doAssert f.getLen == 40
- # This fails, because `len` gets the nkOpenSymChoice
- # treatment inside the template early pass and then
- # it can't be recognized as a field anymore
- import os, times
- include "sunset.nimf"
- block ttempl:
- const
- tabs = [["home", "index"],
- ["news", "news"],
- ["documentation", "documentation"],
- ["download", "download"],
- ["FAQ", "question"],
- ["links", "links"]]
- var i = 0
- for item in items(tabs):
- var content = $i
- var file: File
- if open(file, changeFileExt(item[1], "html"), fmWrite):
- write(file, sunsetTemplate(current=item[1], ticker="", content=content,
- tabs=tabs))
- close(file)
- else:
- write(stdout, "cannot open file for writing")
- inc(i)
- block ttempl4:
- template `:=`(name, val: untyped): typed =
- var name = val
- ha := 1 * 4
- hu := "ta-da" == "ta-da"
- echo ha, hu
- import mtempl5
- block ttempl5:
- echo templ()
- #bug #892
- proc parse_to_close(value: string, index: int, open='(', close=')'): int =
- discard
- # Call parse_to_close
- template get_next_ident: typed =
- discard "{something}".parse_to_close(0, open = '{', close = '}')
- get_next_ident()
- #identifier expected, but found '(open|open|open)'
- #bug #880 (also example in the manual!)
- template typedef(name: untyped, typ: typedesc) =
- type
- `T name` {.inject.} = typ
- `P name` {.inject.} = ref `T name`
- typedef(myint, int)
- var x: PMyInt
- block templreturntype:
- template `=~` (a: int, b: int): bool = false
- var foo = 2 =~ 3
|