summary refs log blame commit diff stats
path: root/tests/generics/t6137.nim
blob: 991f39dd157f11ad83a04b2e64dfa51356fe4acf (plain) (tree)
1
2
3
           
                                                                                               
          














                                                
                                           








                                                                                     
discard """
  errormsg: "\'vectFunc\' doesn't have a concrete type, due to unspecified generic parameters."
  line: 28
"""

type
  # simple vector of declared fixed length
  vector[N : static[int]] = array[0..N-1, float]

proc `*`[T](x: float, a: vector[T]): vector[T] =
  # multiplication by scalar
  for ii in 0..high(a):
    result[ii] = a[ii]*x

let
  # define a vector of length 3
  x: vector[3] = [1.0, 3.0, 5.0]

proc vectFunc[T](x: vector[T]): vector[T] =
  # Define a vector function
  result = 2.0*x

proc passVectFunction[T](g: proc(x: vector[T]): vector[T], x: vector[T]): vector[T] =
  # pass a vector function as input in another procedure
  result = g(x)

let
  xNew = passVectFunction(vectFunc,x)
class="o">*(data: string): PLLStream = new(result) result.s = data result.kind = llsString proc llStreamOpen*(f: File): PLLStream = new(result) result.f = f result.kind = llsFile proc llStreamOpen*(filename: string, mode: FileMode): PLLStream = new(result) result.kind = llsFile if not open(result.f, filename, mode): result = nil proc llStreamOpen*(): PLLStream = new(result) result.kind = llsNone proc llStreamOpenStdIn*(): PLLStream = new(result) result.kind = llsStdIn result.s = "" result.lineOffset = -1 proc llStreamClose*(s: PLLStream) = case s.kind of llsNone, llsString, llsStdIn: discard of llsFile: close(s.f) when not declared(readLineFromStdin): # fallback implementation: proc readLineFromStdin(prompt: string, line: var string): bool = stdout.write(prompt) result = readLine(stdin, line) if not result: stdout.write("\n") quit(0) proc endsWith*(x: string, s: set[char]): bool = var i = x.len-1 while i >= 0 and x[i] == ' ': dec(i) if i >= 0 and x[i] in s: result = true const LineContinuationOprs = {'+', '-', '*', '/', '\\', '<', '>', '!', '?', '^', '|', '%', '&', '$', '@', '~', ','} AdditionalLineContinuationOprs = {'#', ':', '='} proc endsWithOpr*(x: string): bool = # also used by the standard template filter: result = x.endsWith(LineContinuationOprs) proc continueLine(line: string, inTripleString: bool): bool {.inline.} = result = inTripleString or line[0] == ' ' or line.endsWith(LineContinuationOprs+AdditionalLineContinuationOprs) proc countTriples(s: string): int = var i = 0 while i < s.len: if s[i] == '"' and s[i+1] == '"' and s[i+2] == '"': inc result inc i, 2 inc i proc llReadFromStdin(s: PLLStream, buf: pointer, bufLen: int): int = s.s = "" s.rd = 0 var line = newStringOfCap(120) var triples = 0 while readLineFromStdin(if s.s.len == 0: ">>> " else: "... ", line): add(s.s, line) add(s.s, "\n") inc triples, countTriples(line) if not continueLine(line, (triples and 1) == 1): break inc(s.lineOffset) result = min(bufLen, len(s.s) - s.rd) if result > 0: copyMem(buf, addr(s.s[s.rd]), result) inc(s.rd, result) proc llStreamRead*(s: PLLStream, buf: pointer, bufLen: int): int = case s.kind of llsNone: result = 0 of llsString: result = min(bufLen, len(s.s) - s.rd) if result > 0: copyMem(buf, addr(s.s[0 + s.rd]), result) inc(s.rd, result) of llsFile: result = readBuffer(s.f, buf, bufLen) of llsStdIn: result = llReadFromStdin(s, buf, bufLen) proc llStreamReadLine*(s: PLLStream, line: var string): bool = setLen(line, 0) case s.kind of llsNone: result = true of llsString: while s.rd < len(s.s): case s.s[s.rd] of '\x0D': inc(s.rd) if s.s[s.rd] == '\x0A': inc(s.rd) break of '\x0A': inc(s.rd) break else: add(line, s.s[s.rd]) inc(s.rd) result = line.len > 0 or s.rd < len(s.s) of llsFile: result = readLine(s.f, line) of llsStdIn: result = readLine(stdin, line) proc llStreamWrite*(s: PLLStream, data: string) = case s.kind of llsNone, llsStdIn: discard of llsString: add(s.s, data) inc(s.wr, len(data)) of llsFile: write(s.f, data) proc llStreamWriteln*(s: PLLStream, data: string) = llStreamWrite(s, data) llStreamWrite(s, "\n") proc llStreamWrite*(s: PLLStream, data: char) = var c: char case s.kind of llsNone, llsStdIn: discard of llsString: add(s.s, data) inc(s.wr) of llsFile: c = data discard writeBuffer(s.f, addr(c), sizeof(c)) proc llStreamWrite*(s: PLLStream, buf: pointer, buflen: int) = case s.kind of llsNone, llsStdIn: discard of llsString: if buflen > 0: setLen(s.s, len(s.s) + buflen) copyMem(addr(s.s[0 + s.wr]), buf, buflen) inc(s.wr, buflen) of llsFile: discard writeBuffer(s.f, buf, buflen) proc llStreamReadAll*(s: PLLStream): string = const bufSize = 2048 case s.kind of llsNone, llsStdIn: result = "" of llsString: if s.rd == 0: result = s.s else: result = substr(s.s, s.rd) s.rd = len(s.s) of llsFile: result = newString(bufSize) var bytes = readBuffer(s.f, addr(result[0]), bufSize) var i = bytes while bytes == bufSize: setLen(result, i + bufSize) bytes = readBuffer(s.f, addr(result[i + 0]), bufSize) inc(i, bytes) setLen(result, i)