Language proposal: DSL-like method invokes and custom code blocks wrappers #8746
Replies: 14 comments
-
How do you propose to handle control statements within the translated lambdas? public string M() {
with readLock(rl) {
// do something
return "foo";
}
} |
Beta Was this translation helpful? Give feedback.
-
Also, in the same vein as #1151 |
Beta Was this translation helpful? Give feedback.
-
@HaloFour public string M() {
with readLock(rl) {
// do something
return "foo";
}
} would be translated to public string M() {
string __return_slot_1 = default(string);
bool __is_return_1 = false;
readLock(() => {
// do something
__return_slot_1 = "foo";
__is_return_1 = true;
return;
}, rl);
if (__is_return_1)
{
return __return_slot_1;
}
} |
Beta Was this translation helpful? Give feedback.
-
IMHO that could get quite janky and there would likely be scenarios that are ambiguous. For example, in your |
Beta Was this translation helpful? Give feedback.
-
@HaloFour
It shouldn't. Compiler shouldn't automatically rethrow any exception from the wrapped code block. |
Beta Was this translation helpful? Give feedback.
-
Isn't this issue almost the same as #85? |
Beta Was this translation helpful? Give feedback.
-
@jnm2 My proposal is somewhat extended to support optional named wrapped blocks. |
Beta Was this translation helpful? Give feedback.
-
Ah, I see. on error()
{
continue;
} ❔ 😄 |
Beta Was this translation helpful? Give feedback.
-
@jnm2 all control statements could be easily propagated by exploiting lambda's closures to pass information out of lambda and then check it later. on error()
{
continue;
} could be translated to something like bool __cstatement_flag_1 = false;
customGuard(() => {
// do something
}, () => {
// here is "on error" lambda
__cstatement_flag_1 = true;
return;
});
if (__cstatement_flag_1)
{
continue;
} |
Beta Was this translation helpful? Give feedback.
-
Very similar in intent to my proposal for block parameters (#589), though with different syntax. |
Beta Was this translation helpful? Give feedback.
-
I prefer your proposed sytax over #1151. What I don't like is the asymetry in the parenthesised parameters. with readLock(x) here on fail(x) is an argument to the action. This should be possible: with guard
{
yield return something;
} and with guard
{
var x = await something;
} and static void doTwice(Action a)
{
a();
a();
}
var i = 0;
with doTwice
{
i++;
return i; // the function should return 1 and not 2
} If one of these example is not possible, it's better to stick with simple lambdas, IMHO. |
Beta Was this translation helpful? Give feedback.
-
@quinmars Anyway, currently it's almost unreadable when method have several delegate arguments (it's common in functional-heavy code) and you try to support delegate arguments with lambdas. |
Beta Was this translation helpful? Give feedback.
-
Probably it's related to the new proposal: #3025 |
Beta Was this translation helpful? Give feedback.
-
I wonder if Ruby-style |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Allow
with
keyword to be used to invoke methods inlock
-like orusing
-like manner.Such method should have first argument of delegate type with no parameters and possible return value.
Any other named delegate arguments are treated as optional additional wrapped code blocks with the same name as argument. If additional block is absent at invoke site, then
null
is passed for argument value.Optional wrapped blocks are invoked with
on
keyword at call-site.Arguments of any other types are used as regular arguments for invoke. If there are none non-delegate arguments, then
()
atwith
oron
blocks could be omitted.Examples:
Motivation
More clean and DSL-like callback invokes when it's apply and when lambda syntax is clumsy and unnatural - especially when there are many named optional delegate arguments and it's very unhelpful to use stream of positional arguments to pass lambda-functions.
Beta Was this translation helpful? Give feedback.
All reactions