Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

There's no $ for openArray #7940

Closed
kaushalmodi opened this issue Jun 1, 2018 · 12 comments
Closed

There's no $ for openArray #7940

kaushalmodi opened this issue Jun 1, 2018 · 12 comments
Labels

Comments

@kaushalmodi
Copy link
Contributor

@kaushalmodi kaushalmodi commented Jun 1, 2018

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
@Yardanico
Copy link
Collaborator

@Yardanico Yardanico commented Jun 1, 2018

that's because $ is not defined for openarray

@Yardanico Yardanico changed the title [strformat] fmt does not work on openarrays There's no $ for openArray Jun 1, 2018
@kaushalmodi
Copy link
Contributor Author

@kaushalmodi kaushalmodi commented Jun 1, 2018

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.

@data-man
Copy link
Contributor

@data-man data-man commented Jun 1, 2018

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, "[", ", ", "]")
@Yardanico
Copy link
Collaborator

@Yardanico Yardanico commented Jun 1, 2018

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

@kaushalmodi
Copy link
Contributor Author

@kaushalmodi kaushalmodi commented Jun 1, 2018

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

@Yardanico
Copy link
Collaborator

@Yardanico Yardanico commented Jun 1, 2018

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

@data-man
Copy link
Contributor

@data-man data-man commented Jun 1, 2018

@Yardanico

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

what do you have in your patched superpupercool system.nim

Many Things. :-D

@Yardanico
Copy link
Collaborator

@Yardanico Yardanico commented Jun 1, 2018

@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

@data-man
Copy link
Contributor

@data-man data-man commented Jun 1, 2018

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

@Yardanico
Copy link
Collaborator

@Yardanico Yardanico commented Jun 1, 2018

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

@data-man data-man closed this in b4626a2 Jun 1, 2018
@Yardanico
Copy link
Collaborator

@Yardanico Yardanico commented Jun 1, 2018

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

@data-man
Copy link
Contributor

@data-man data-man commented Jun 1, 2018

@Yardanico

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants