-
-
Save JamesTryand/2630087 to your computer and use it in GitHub Desktop.
VB.NET funcs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Just found out that VB.NET can infer function types correctly. I.e. this won't work in C#: | |
var withMemoryStream = | |
(Action<MemoryStream> f) => () => { | |
using (var ms = new MemoryStream()) | |
f(ms); | |
}; | |
("Cannot assign lambda expression to implicitly-typed local variable", because it can't tell a Func<T> from an Expression<Func<T>>). | |
But the exact same code in VB.NET does work: | |
Dim withMemoryStream = | |
Function(f As Action(Of MemoryStream)) _ | |
Sub() | |
Using ms = New MemoryStream | |
f(ms) | |
End Using | |
End Sub | |
Not bad, VB.NET, not bad. | |
People usually type explicitly their Funcs in C#: | |
Func<Action<MemoryStream>, Action> withMemoryStream = | |
f => () => { | |
using (var ms = new MemoryStream()) | |
f(ms); | |
}; | |
But I never leave home without my trusty extensions to prod the C# type inferencer: | |
public static class L { | |
public static Func<A,R> F<A,R>(Func<A,R> f) { | |
return f; | |
} | |
// overloads for other type arity... | |
public static Action A(Action a) { | |
return a; | |
} | |
} | |
var withMemoryStream = | |
L.F((Action<MemoryStream> f) => | |
L.A(() => { | |
using (var ms = new MemoryStream()) | |
f(ms); | |
})); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment