mirror of
https://github.com/nim-lang/Nim.git
synced 2026-02-15 07:43:26 +00:00
merged branch overloading-for-macros
This commit is contained in:
@@ -2,12 +2,12 @@ discard """
|
||||
file: "tambsys.nim"
|
||||
output: ""
|
||||
"""
|
||||
# Test ambiguous symbols
|
||||
|
||||
import mambsys1, mambsys2
|
||||
|
||||
var
|
||||
v: mambsys1.TExport
|
||||
mambsys2.foo(3) #OUT
|
||||
# Test ambiguous symbols
|
||||
|
||||
import mambsys1, mambsys2
|
||||
|
||||
var
|
||||
v: mambsys1.TExport
|
||||
mambsys2.foo(3) #OUT
|
||||
|
||||
|
||||
|
||||
@@ -21,7 +21,7 @@ template Comparable(typ: typeDesc): stmt =
|
||||
proc `<=` * (x, y: typ): bool {.borrow.}
|
||||
proc `==` * (x, y: typ): bool {.borrow.}
|
||||
|
||||
template DefineCurrency(typ, base: expr): stmt =
|
||||
template DefineCurrency(typ, base: expr): stmt {.immediate.} =
|
||||
type
|
||||
typ* = distinct base
|
||||
Additive(typ)
|
||||
|
||||
@@ -6,7 +6,7 @@ discard """
|
||||
|
||||
var testNumber = 0
|
||||
|
||||
template test(opr, a, b, c: expr): stmt =
|
||||
template test(opr, a, b, c: expr): stmt {.immediate.} =
|
||||
# test the expression at compile and runtime
|
||||
block:
|
||||
const constExpr = opr(a, b)
|
||||
@@ -43,5 +43,3 @@ test(`shl`, 0xffffffff'i32, 0x4'i32, 0xfffffff0'i32)
|
||||
|
||||
Echo("Success") #OUT Success
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
discard """
|
||||
file: "toverl2.nim"
|
||||
output: "true012"
|
||||
output: "true012innertrue"
|
||||
"""
|
||||
# Test new overloading resolution rules
|
||||
|
||||
@@ -14,14 +14,20 @@ iterator toverl2(x: int): int =
|
||||
while res < x:
|
||||
yield res
|
||||
inc(res)
|
||||
|
||||
|
||||
var
|
||||
pp: proc (x: bool): string = toverl2
|
||||
|
||||
stdout.write(pp(true))
|
||||
|
||||
for x in toverl2(3):
|
||||
stdout.write(toverl2(x))
|
||||
|
||||
block:
|
||||
proc toverl2(x: int): string = return "inner"
|
||||
stdout.write(toverl2(5))
|
||||
stdout.write(true)
|
||||
|
||||
stdout.write("\n")
|
||||
#OUT true012
|
||||
|
||||
|
||||
#OUT true012innertrue
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@ discard """
|
||||
output: "23456"
|
||||
"""
|
||||
|
||||
template toSeq*(iter: expr): expr =
|
||||
template toSeq*(iter: expr): expr {.immediate.} =
|
||||
var result: seq[type(iter)] = @[]
|
||||
for x in iter: add(result, x)
|
||||
result
|
||||
|
||||
@@ -1,2 +1,2 @@
|
||||
import uclosures
|
||||
import uclosures, utemplates
|
||||
|
||||
|
||||
32
tests/run/utemplates.nim
Normal file
32
tests/run/utemplates.nim
Normal file
@@ -0,0 +1,32 @@
|
||||
import unittest
|
||||
|
||||
template t(a: int): expr = "int"
|
||||
template t(a: string): expr = "string"
|
||||
|
||||
test "templates can be overloaded":
|
||||
check t(10) == "int"
|
||||
check t("test") == "string"
|
||||
|
||||
test "previous definitions can be further overloaded or hidden in local scopes":
|
||||
template t(a: bool): expr = "bool"
|
||||
|
||||
check t(true) == "bool"
|
||||
check t(10) == "int"
|
||||
|
||||
template t(a: int): expr = "inner int"
|
||||
check t(10) == "inner int"
|
||||
check t("test") == "string"
|
||||
|
||||
test "templates can be redefined multiple times":
|
||||
template customAssert(cond: bool, msg: string): stmt =
|
||||
if not cond: fail(msg)
|
||||
|
||||
template assertion_failed(body: stmt) =
|
||||
template fail(msg: string): stmt = body
|
||||
|
||||
assertion_failed: check msg == "first fail path"
|
||||
customAssert false, "first fail path"
|
||||
|
||||
assertion_failed: check msg == "second fail path"
|
||||
customAssert false, "second fail path"
|
||||
|
||||
Reference in New Issue
Block a user