Skip to content

write requires conversion to string #13182

Closed
@ghost

Description

write requires conversion to string

hmm, I think this is a bug

Example

type
  Kind = enum INT, STR, ELT
  Tree = ref object
    case k: Kind
    of INT: i: int
    of STR: s: string
    of ELT:
      e: string
      a: seq[(string, string)]
      b: seq[Tree]

proc tree(x: int): Tree = Tree(k: INT, i: x)
proc tree(x: string): Tree = Tree(k: STR, s: x)

proc write(f: File, x: Tree) =
  case x.k
  of INT: f.write x.i
  of STR: f.write x.s
  of ELT:
    f.write '('
    f.write x.e
    f.write ' '
    for (k, v) in x.a:
      f.write k
      f.write '='
      f.write v
    f.write ' '
    for i in x.b:
      f.write i
    f.write ')'

stdout.write tree 42

Current Output

rec.nim(29, 15) Error: type mismatch: got <Tree>
but expected one of:
proc `$`(x: bool): string
proc `$`(x: int64): string
proc `$`(w: WideCString; estimate: int; replacement: int = 0x0000FFFD): string
proc `$`(x: char): string
proc `$`(x: float): string
proc `$`(x: cstring): string
proc `$`[T](x: openArray[T]): string
proc `$`[T, IDX](x: array[IDX, T]): string
proc `$`(s: WideCString): string
proc `$`(x: string): string
proc `$`[T: tuple |
    object](x: T): string
proc `$`[Enum: enum](x: Enum): string
proc `$`(x: uint64): string
proc `$`(t: typedesc): string
proc `$`[T](x: set[T]): string
proc `$`[T](x: seq[T]): string
proc `$`(x: int): string
proc `$`[T, U](x: HSlice[T, U]): string

Expected Output

42

Possible Solution

s/write/mywrite fixes the example

but i would expect that my write method would be used

Additional Information

$ nim -v
Nim Compiler Version 0.20.2 [Linux: amd64]
Compiled at 2019-07-17
Copyright (c) 2006-2019 by Andreas Rumpf

active boot switches: -d:release -d:nativeStackTrace

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions