Hello.
I am trying to express a concept for accepting 'any' regular expression Engine, providing that the user perform a small wrapping effort.
Here is the best I could achieve (MRE).
#A simple concept for a regexp engine, so that we can use ANY one.
type
ReEngine = concept r, var v, type R
loadEnginePattern(v, type string) is R
reEngineMatch(r, type string) is bool
proc buildAndMatch[T:ReEngine] (pattern: string, payload: string) : bool =
var r = T()
r = r.loadEnginePattern(pattern)
return r.reEngineMatch(payload)
import regex
type ReWrap = object
reg : Regex
proc loadEnginePattern(r: var ReWrap, p: string) : Rewrap =
return ReWrap(reg : re(p))
proc reEngineMatch(r : ReWrap, s: string) : bool =
return s.contains(r.reg)
echo buildAndMatch[ReWrap](pattern = "bc" , payload = "aaabccc")
echo buildAndMatch[ReWrap](pattern = "bc" , payload = "abaabaccc")
compile and run ok with Nim 2.2.8.
I've tried many various things, could not get a compiling version...
Any advice welcome ! Thanks.
Use new-styled concepts:
type
ReEngine = concept
proc loadEnginePattern(v: var Self; s: string): Self
proc reEngineMatch(r: Self; s: string): bool