timotheecour / Nim

Nim is a compiled, garbage-collected systems programming language with a design that focuses on efficiency, expressiveness, and elegance (in that order of priority).
http://nim-lang.org/
Other
2 stars 0 forks source link

add `fmtFloat` to stdlib #766

Open timotheecour opened 3 years ago

timotheecour commented 3 years ago

links

juancarlospaco commented 3 years ago

That was my idea on the original PR too, but got chopped off as usual. 🤷

timotheecour commented 3 years ago

the fact that there was so many review comments and commits in the PR (because of failing edge cases) played a role IMO in the closing of that PR.

But regardless, the feature is useful and (as you discovered yourself) is tricky to implement correctly, which makes it a great candidate for stdlib; we should address the concerns raised in the closing comments regarding API though.

juancarlospaco commented 3 years ago

My first commit was fast and easy to understand, ...then people requested to support whatever crossed their mind.

The problem is that adds more and more defensive programming, maybe there needs to be 2 modes, via another proc or overload or a static argument on 1 proc, a "strict" one thats efficient and tiny, and another that parses/formats any random potato into a float.

timotheecour commented 3 years ago

I'm not saying it's an easy problem; the API needs to satisfy both:

decisions to make

API

it should be additive (analog to addFloat), which is the most composable/efficient, can be used in conjunction with sugar.dup

type OptFloatFmt = object
  sep: char ...
type OptFloatParse = object
  sep: char ...
proc addFloatCustom*(result: var string, a: SomeFloat, opt = initOptFloatFmt())
proc parseCustom*(ret: var SomeFloat, s: openArray[char], opt = initOptFloatParse()): int
  # returns nb of chars parsed in `s`

note

https://github.com/timotheecour/Nim/issues/741 could be used to define packed object (more efficient + syntax sugar) instead of OptFloatFmt, OptFloatParse

juancarlospaco commented 3 years ago
  • which module? strutils already has formatEng, so it could make sense there

strmisc says This module contains various string utility routines that are uncommonly used in comparison to the ones in strutils.

I should live there IMHO.

timotheecour commented 3 years ago

possibly