I really often use:
try
try
with
finally
so I’m interesting if is possible to make new syntax operator to not write “try” two times.
let mytry foo bar foobar =
try
try
foo
with
| _ -> bar // weird part here, I want to have a match
finally foobar
mytry
<| foo
<| | :? SocketException ->
| _ -> // ok it looks funny but how to realize it?
<| foobar
the problems I see here are
- non-common syntax, in mytry there is no try with finally keywords, just <| <| <| for each, but it’s lesser trouble I guess
- with: I don’t know how can I realize this part. even how it will look if I can realize it…
The question is whether you really need
try/finally. Most of the timetry/finallyis used for disposing resources even when exceptions occur. But you can always replace it by theusekeyword.For example:
can be rewritten as:
For the learning purpose, you can define
mytryusing high-order functions as follows:But it doesn’t look really nice on above example: