darkly icon indicating copy to clipboard operation
darkly copied to clipboard

Compare to mmun's spec

Open nrc opened this issue 7 years ago • 7 comments

https://gist.github.com/mmun/bffc62a5eb6ad648b7f7b2825364962c (cc @mmun)

  • [ ] - Add scan! for scan-not-a-whole-line case.
  • [ ] - Robust parsing: scan! should keep trying to match the input until it finds something that matches or we run out of bytes. This matches cin and scanf semantics.
  • [x] - Expression form of scan!(...) to avoid boilerplatey bindings in some cases, e.g.
// this snippet also uses default format string inference on the next line
for i in 0..scan!("{}") {
  for j in 0..scan!("{}") {
    // ...
  }
}
  • [ ] - Let scan!() infer a default format string:
    • let x: i32 = scan!(); // "{}"
    • let x: (i32,) = scan!(); // "{}"
    • let x: (i32, i32) = scan!(); // "{}{}"

nrc avatar May 06 '17 21:05 nrc

I just pushed a commit which adds the expression form. It always returns a tuple of results, which is not great for single values, but that should be fairly easy to add. E.g.,

    let x: i32 = scan!("hello {}"); // todo
    let x: (i32,) = scan!("hello {}"); // works
    let x: (i32, i32) = scan!("hello {}"); // works

nrc avatar May 22 '17 03:05 nrc

Awesome. I came to the same conclusion after several months of using scan!("hello {}").0 in my own library -- supporting singular values is just way more ergonomic!

mmun avatar May 22 '17 03:05 mmun

@nrc Should it be "hello {} {}" in your third line? Or am I misunderstanding.

mmun avatar May 22 '17 03:05 mmun

it should

nrc avatar May 22 '17 03:05 nrc

Another thing that comes up often is scanning individual characters. E.g. I'd expect that for the input string "123",

let (x: char, y: u8) = scan!("{}{}") // x == '1', y == 23

mmun avatar May 22 '17 04:05 mmun

We could allow an empty format string in the statement form too: scanln!(x: i32) is equivalent to scanln!("{}", x: i32)

nrc avatar Dec 26 '18 09:12 nrc

I came to the same conclusion after several months of using

Single values are not tuple-wrapped now

nrc avatar Dec 28 '18 07:12 nrc