std/options: $some(3) is now "some(3)", etc. (#17147)

* std/options: $some(3) is now "some(3)", not "Some(3)", `$none(int)` is now `"none(int)"` instead of `"None[int]"`

* fix tests

* disable optionsutils
This commit is contained in:
Timothee Cour 2021-02-24 14:17:16 -08:00 • committed by GitHub
commit 578d1ee751
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 29 additions and 20 deletions

View file

@ -178,6 +178,9 @@ provided by the operating system.
- Added to `wrapnils` an option-like API via `??.`, `isSome`, `get`. - Added to `wrapnils` an option-like API via `??.`, `isSome`, `get`.
- `std/options` changed `$some(3)` to `"some(3)"` instead of `"Some(3)"`
and `$none(int)` to `"none(int)"` instead of `"None[int]"`.
## Language changes ## Language changes
- `nimscript` now handles `except Exception as e`. - `nimscript` now handles `except Exception as e`.

View file

@ -306,8 +306,8 @@ proc flatMap*[T, R](self: Option[T],
proc filter*[T](self: Option[T], callback: proc (input: T): bool): Option[T] {.inline.} = proc filter*[T](self: Option[T], callback: proc (input: T): bool): Option[T] {.inline.} =
## Applies a `callback` to the value of the `Option`. ## Applies a `callback` to the value of the `Option`.
## ##
## If the `callback` returns `true`, the option is returned as `Some`. ## If the `callback` returns `true`, the option is returned as `some`.
## If it returns `false`, it is returned as `None`. ## If it returns `false`, it is returned as `none`.
## ##
## **See also:** ## **See also:**
## * `flatMap proc <#flatMap,Option[A],proc(A)>`_ ## * `flatMap proc <#flatMap,Option[A],proc(A)>`_
@ -325,8 +325,8 @@ proc filter*[T](self: Option[T], callback: proc (input: T): bool): Option[T] {.i
self self
proc `==`*[T](a, b: Option[T]): bool {.inline.} = proc `==`*[T](a, b: Option[T]): bool {.inline.} =
## Returns `true` if both `Option`s are `None`, ## Returns `true` if both `Option`s are `none`,
## or if they are both `Some` and have equal values. ## or if they are both `some` and have equal values.
runnableExamples: runnableExamples:
let let
a = some(42) a = some(42)
@ -346,18 +346,24 @@ proc `==`*[T](a, b: Option[T]): bool {.inline.} =
proc `$`*[T](self: Option[T]): string = proc `$`*[T](self: Option[T]): string =
## Get the string representation of the `Option`. ## Get the string representation of the `Option`.
runnableExamples: runnableExamples:
assert $some(42) == "Some(42)" assert $some(42) == "some(42)"
assert $none(int) == "None[int]" assert $none(int) == "none(int)"
if self.isSome: if self.isSome:
when defined(nimLagacyOptionsDollar):
result = "Some(" result = "Some("
else:
result = "some("
result.addQuoted self.val result.addQuoted self.val
result.add ")" result.add ")"
else: else:
when defined(nimLagacyOptionsDollar):
result = "None[" & name(T) & "]" result = "None[" & name(T) & "]"
else:
result = "none(" & name(T) & ")"
proc unsafeGet*[T](self: Option[T]): lent T {.inline.}= proc unsafeGet*[T](self: Option[T]): lent T {.inline.}=
## Returns the value of a `Some`. The behavior is undefined for `None`. ## Returns the value of a `some`. The behavior is undefined for `none`.
## ##
## **Note:** Use this only when you are **absolutely sure** the value is present ## **Note:** Use this only when you are **absolutely sure** the value is present
## (e.g. after checking with `isSome <#isSome,Option[T]>`_). ## (e.g. after checking with `isSome <#isSome,Option[T]>`_).

View file

@ -114,7 +114,7 @@ pkg2 "nimwc", "nim c nimwc.nim"
pkg2 "norm", "nim c -r tests/sqlite/trows.nim" pkg2 "norm", "nim c -r tests/sqlite/trows.nim"
pkg2 "npeg", "nimble testarc" pkg2 "npeg", "nimble testarc"
pkg2 "numericalnim", "nim c -r tests/test_integrate.nim" pkg2 "numericalnim", "nim c -r tests/test_integrate.nim"
pkg2 "optionsutils" # pkg2 "optionsutils" # pending changing test from `Some` to `some` (etc) in tests/test2.nim, refs #17147
pkg2 "ormin", "nim c -o:orminn ormin.nim" pkg2 "ormin", "nim c -o:orminn ormin.nim"
pkg2 "parsetoml" pkg2 "parsetoml"
pkg2 "patty" pkg2 "patty"

View file

@ -24,9 +24,9 @@ G:0,1:0.1
G:0,1:0.1 G:0,1:0.1
H:1:0.1 H:1:0.1
0 0
(foo: None[seq[Foo]], s: "") (foo: none(seq[Foo]), s: "")
(foo: Some(@[(a: "world", bar: None[Bar])]), s: "hello,") (foo: some(@[(a: "world", bar: none(Bar))]), s: "hello,")
@[(a: "hey", bar: None[Bar])] @[(a: "hey", bar: none(Bar))]
''' '''
joinable: false joinable: false
""" """

View file

@ -116,8 +116,8 @@ block:
let let
a1 = some(newJNull()) a1 = some(newJNull())
a2 = none(JsonNode) a2 = none(JsonNode)
doAssert $($$a1).to[:Option[JsonNode]] == "Some(null)" doAssert $($$a1).to[:Option[JsonNode]] == "some(null)"
doAssert $($$a2).to[:Option[JsonNode]] == "None[JsonNode]" doAssert $($$a2).to[:Option[JsonNode]] == "none(JsonNode)"
doAssert ($$a1).to[:Option[JsonNode]] == some(newJNull()) doAssert ($$a1).to[:Option[JsonNode]] == some(newJNull())
doAssert ($$a2).to[:Option[JsonNode]] == none(JsonNode) doAssert ($$a2).to[:Option[JsonNode]] == none(JsonNode)

View file

@ -82,8 +82,8 @@ proc main() =
doAssert(stringNone.get("Correct") == "Correct") doAssert(stringNone.get("Correct") == "Correct")
block stringify: block stringify:
doAssert($(some("Correct")) == "Some(\"Correct\")") doAssert($(some("Correct")) == "some(\"Correct\")")
doAssert($(stringNone) == "None[string]") doAssert($(stringNone) == "none(string)")
disableJsVm: disableJsVm:
block map_with_a_void_result: block map_with_a_void_result:
@ -155,7 +155,7 @@ proc main() =
name: string name: string
let nobody = none(Named) let nobody = none(Named)
doAssert($nobody == "None[Named]") doAssert($nobody == "none(Named)")
# "$ on type with name()" # "$ on type with name()"
block: block:
@ -163,7 +163,7 @@ proc main() =
myname: string myname: string
let noperson = none(Person) let noperson = none(Person)
doAssert($noperson == "None[Person]") doAssert($noperson == "none(Person)")
# "Ref type with overloaded `==`" # "Ref type with overloaded `==`"
block: block:
@ -190,7 +190,7 @@ proc main() =
block: block:
let x = none(cstring) let x = none(cstring)
doAssert x.isNone doAssert x.isNone
doAssert $x == "None[cstring]" doAssert $x == "none(cstring)"
static: main() static: main()