Nim: There's no `$` for openArray

Created on 1 Jun 2018  路  12Comments  路  Source: nim-lang/Nim

import strformat
proc foo(x: openArray[int]) =
  echo fmt"{x}"

Compiling that gives this error:

lib/pure/strformat.nim(268, 13) Error: type mismatch: got <openarray[int]>
but expected one of:
proc `$`(x: string): string
proc `$`(s: WideCString): string
proc `$`[T: tuple |
    object](x: T): string
proc `$`(x: uint64): string
proc `$`(x: int64): string
proc `$`[T, IDX](x: array[IDX, T]): string
proc `$`[Enum: enum](x: Enum): string
proc `$`(w: WideCString; estimate: int; replacement: int = 0x0000FFFD): string
proc `$`[T](x: set[T]): string
proc `$`[T](x: seq[T]): string
proc `$`(x: int): string
proc `$`(x: cstring): string
proc `$`(x: bool): string
proc `$`(x: float): string
proc `$`(x: char): string
Stdlib

All 12 comments

that's because $ is not defined for openarray

Ah, was just going to comment on that.. here's an even smaller example:

proc testOpenArray(x: openArray[int]) =
    echo x 

I am aware that repr(x) can be used instead. But I eventually would like to use fmt for open arrays too.

From my patched superpupercool system.nim :)

proc `$`*[T](x: openarray[T]): string =
  ## generic ``$`` operator for openarrays that is lifted from the components
  ## of `x`. Example:
  ##
  ## .. code-block:: nim
  ##   $(@[23, 45].toOpenArray(0, 1)) == "[23, 45]"
  collectionToString(x, "[", ", ", "]")

@data-man what do you have in your patched superpupercool system.nim and why you don't make PRs? 馃拑

@data-man Of course I tried it and didn't work.. your superpupercool system.nim has collectionToString defined too :P

@kaushalmodi no, collectionToString is a proc defined in system.nim, but it's not exported.

@Yardanico

Because PRs for system.nim often isn't accepted.

what do you have in your patched superpupercool system.nim

Many Things. :-D

@data-man well, you should at least try to make a PR, so there will be a discussion and people will decide if it's good to put it in system.nim

_Should_ try?
No, I'll spend my time better.

Well, it means that you don't want to contribute to Nim (with system.nim additions) :P

@data-man I didn't mean that you should just commit, I've meant that you should make PRs, but ok :D

@Yardanico

I hope that Araq wouldn't object to this improvement. ;)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

yglukhov picture yglukhov  路  46Comments

mratsim picture mratsim  路  38Comments

Araq picture Araq  路  74Comments

timotheecour picture timotheecour  路  33Comments

ghost picture ghost  路  27Comments