Why are templates not allowed to “use” bindings?

According to spec, the use binding requires an identifier (as opposed to let ) instead of a template. Why is this? Here is an example script that does not work.

 type Disposable = Resource of IDisposable let f disposable = use (Resource d) = disposable //ERROR: 'use' bindings must be of the form 'use <var> = <expr>' () 
+1
source share
1 answer

I think the likely answer is that many templates do not make sense. For example, how do you expect the compiler to process the following code?

 type DisposablePair = DisposablePair of IDisposable * IDisposable let f disposablePair = use (DisposablePair(x,y)) = disposablePair () 

Your strange error message is probably due to the fact that even if you used let binding, you need to bind (Resource d) and not Resource(d) (the compiler thinks you are declaring a new function).

For what it's worth, I find the inability to use the underscore pattern as sometimes annoying (especially when working with IDisposable instances that exist only to delimit areas, such as System.Transactions.TransactionScope ). One way to generalize use bindings to handle underscores and several other situations is to require the right side of the use binding to be IDisposable , but to allow any pattern on the left side, so that:

 use p = v in e 

will syntactically translate to something like

 let ((p:System.IDisposable) as d) = v in try e finally d.Dispose() 
+1
source

Source: https://habr.com/ru/post/1385439/


All Articles