Ensure that =? works with types that do not have a default value
This commit is contained in:
parent
d7a757a8cb
commit
cfe4c6fc95
|
@ -4,15 +4,19 @@ import std/macros
|
|||
proc option[T](option: Option[T]): Option[T] =
|
||||
option
|
||||
|
||||
proc placeholder(T: type): T =
|
||||
discard
|
||||
|
||||
template bindLet(name, expression): bool =
|
||||
let option = expression.option
|
||||
const default = typeof(option.unsafeGet()).default
|
||||
let name {.used.} = if option.isSome: option.unsafeGet() else: default
|
||||
type T = typeof(option.unsafeGet())
|
||||
let name {.used.} = if option.isSome: option.unsafeGet() else: placeholder(T)
|
||||
option.isSome
|
||||
|
||||
template bindVar(name, expression): bool =
|
||||
let option = expression.option
|
||||
var name {.used.} : typeof(option.unsafeGet())
|
||||
type T = typeof(option.unsafeGet())
|
||||
var name {.used.} : T = placeholder(T)
|
||||
if option.isSome:
|
||||
name = option.unsafeGet()
|
||||
option.isSome
|
||||
|
|
|
@ -0,0 +1,2 @@
|
|||
--warningAsError[UnsafeDefault]:"on"
|
||||
--warningAsError[ProveInit]:"on"
|
|
@ -162,6 +162,20 @@ suite "optionals":
|
|||
|
||||
check called
|
||||
|
||||
test "=? works with types that do not have a default value":
|
||||
type NoDefault {.requiresInit.} = distinct int
|
||||
proc `==`(a,b: NoDefault): bool {.borrow.}
|
||||
|
||||
if a =? some NoDefault(42):
|
||||
check a == NoDefault(42)
|
||||
else:
|
||||
fail()
|
||||
|
||||
if var a =? some NoDefault(42):
|
||||
check a == NoDefault(42)
|
||||
else:
|
||||
fail()
|
||||
|
||||
test "without statement can be used for early returns":
|
||||
proc test1 =
|
||||
without a =? 42.some:
|
||||
|
|
Loading…
Reference in New Issue