mirror of
https://github.com/nim-lang/Nim.git
synced 2026-01-02 11:12:37 +00:00
@@ -77,7 +77,7 @@ proc checkTypes(c: PPatternContext, p: PSym, n: PNode): bool =
|
||||
if isNil(n.typ):
|
||||
result = p.typ.kind in {tyVoid, tyStmt}
|
||||
else:
|
||||
result = sigmatch.argtypeMatches(c.c, p.typ, n.typ)
|
||||
result = sigmatch.argtypeMatches(c.c, p.typ, n.typ, fromHlo = true)
|
||||
|
||||
proc isPatternParam(c: PPatternContext, p: PNode): bool {.inline.} =
|
||||
result = p.kind == nkSym and p.sym.kind == skParam and p.sym.owner == c.owner
|
||||
|
||||
@@ -2366,14 +2366,18 @@ proc matches*(c: PContext, n, nOrig: PNode, m: var TCandidate) =
|
||||
for t in m.inferredTypes:
|
||||
if t.sonsLen > 1: t.sons.setLen 1
|
||||
|
||||
proc argtypeMatches*(c: PContext, f, a: PType): bool =
|
||||
proc argtypeMatches*(c: PContext, f, a: PType, fromHlo = false): bool =
|
||||
var m: TCandidate
|
||||
initCandidate(c, m, f)
|
||||
let res = paramTypesMatch(m, f, a, c.graph.emptyNode, nil)
|
||||
#instantiateGenericConverters(c, res, m)
|
||||
# XXX this is used by patterns.nim too; I think it's better to not
|
||||
# instantiate generic converters for that
|
||||
result = res != nil
|
||||
if not fromHlo:
|
||||
res != nil
|
||||
else:
|
||||
# pattern templates do not allow for conversions except from int literal
|
||||
res != nil and m.convMatches == 0 and m.intConvMatches in [0, 256]
|
||||
|
||||
proc instTypeBoundOp*(c: PContext; dc: PSym; t: PType; info: TLineInfo;
|
||||
op: TTypeAttachedOp; col: int): PSym {.procvar.} =
|
||||
|
||||
27
tests/template/tpattern_with_converter.nim
Normal file
27
tests/template/tpattern_with_converter.nim
Normal file
@@ -0,0 +1,27 @@
|
||||
discard """
|
||||
output: 10.0
|
||||
"""
|
||||
|
||||
type
|
||||
MyFloat = object
|
||||
val: float
|
||||
|
||||
converter to_myfloat*(x: float): MyFloat {.inline.} =
|
||||
MyFloat(val: x)
|
||||
|
||||
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
|
||||
|
||||
echo floatDouble(5)
|
||||
Reference in New Issue
Block a user