You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
the following is a very useful idiom for debugging, where adding defer: echo("result:", result) at top of body of a function allows printing its result, regardless of whether the function can return in many places:
use case1: debugging
procfoo(a:Arg): auto=defer: echo("result:", result)
# restOfCode which may return in multiple places
use case1: contracts
procfoo(a:Arg): auto=defer: doAssertresult.isSomeProperty
# restOfCode which may return in multiple places
it's yet another advantage of defer over try/finally as all it does is add a single line of code instead of having to wrap lots of code into a try/finally (and then reverting back when we're done debugging).
However it has 1 limitation: it requires either an explicit type (see case3) or an explicit dummy initialization before defer (see case4); otherwise it gives Error: type mismatch: got <untyped>
Could that limitation be lifted? since the defer unwraps to try/finally, it would seem like it could, and be smarter about type inference in this case
whendefined(case2):
procfoo(a:int):auto=# Error: type mismatch: got <untyped>defer: echo("result:", result)
if a==0:
return42if a==1:
result=123elif a==2:
return101echofoo(1)
whendefined(case3):
# explicit return type: okprocfoo(a:int):int=defer: echo("result:", result)
if a==0:
return42if a==1:
result=123elif a==2:
return101let a =foo(1)
whendefined(case4):
# initialization of result before defer: okprocfoo(a:int):auto=iffalse: result=0defer: echo("result:", result)
if a==0:
return42if a==1:
result=123elif a==2:
return101let a =foo(1)
timotheecour
changed the title
result type inference for defer, to help with a very useful debugging idiom
result type inference for defer, to help with a very useful debugging / assertion idiom
Aug 28, 2018
timotheecour
changed the title
result type inference for defer, to help with a very useful debugging / assertion idiom
result type inference for defer, to help with a very useful debugging / contracts idiom
Aug 28, 2018
the following is a very useful idiom for debugging, where adding
defer: echo("result:", result)
at top of body of a function allows printing its result, regardless of whether the function can return in many places:it's yet another advantage of defer over try/finally as all it does is add a single line of code instead of having to wrap lots of code into a try/finally (and then reverting back when we're done debugging).
However it has 1 limitation: it requires either an explicit type (see
case3
) or an explicit dummy initialization beforedefer
(seecase4
); otherwise it givesError: type mismatch: got <untyped>
Could that limitation be lifted? since the
defer
unwraps to try/finally, it would seem like it could, and be smarter about type inference in this caselinks
The text was updated successfully, but these errors were encountered: