Replies: 28 comments
-
|
Beta Was this translation helpful? Give feedback.
-
@aluanhaddad Any reasons why? |
Beta Was this translation helpful? Give feedback.
-
I would def find value in a lock-expression. |
Beta Was this translation helpful? Give feedback.
-
I've thought about lock expressions a lot recently. Using expressions not so much. |
Beta Was this translation helpful? Give feedback.
-
You could take a page out of the F# playbook and do something like this: static T Lock<T>(object syncObj, Func<T> fn) {
lock (syncObj) {
return fn();
}
}
var obj = new object();
var res = Lock(obj, M); |
Beta Was this translation helpful? Give feedback.
-
As long as that is the only time in your career where you invoke a delegate inside a lock statement, I promise not to have a heart attack. 😁 |
Beta Was this translation helpful? Give feedback.
-
@jnm2 How is that any different than |
Beta Was this translation helpful? Give feedback.
-
@FunctionalFirst Your code sample is fine! I'm wary of delegates inside a lock in general. It can lead to reentry issues or deadlocks in corner cases depending who gave you the delegate. For example, the delegate might raise an event. |
Beta Was this translation helpful? Give feedback.
-
Now that I think about it, I don't know, both seem reasonable. I just feel like locks should be very rare and don't need a sugar. |
Beta Was this translation helpful? Give feedback.
-
One place where I really miss lock expressions is in expression bodied properties. I use those a lot, but it is not possible whenever I need to use a lock. I would like how lock expressions would make the code style more consistent. |
Beta Was this translation helpful? Give feedback.
-
Inconsistency is a bad trait of any programming language. If it works for |
Beta Was this translation helpful? Give feedback.
-
@GeirGrusom I used to think that, but I'm not actually sure why that's really an important factor anymore. |
Beta Was this translation helpful? Give feedback.
-
I hate stumbling upon things that don't work simply because it doesn't. SQL dialects are filled to the brim with those, and encountering inconsistencies doesn't exactly brighten my day. I never say "Oh how wise of the language designers to make that inconsistent with the language syntax". More commonly it makes me talk trash about that language at the water cooler. |
Beta Was this translation helpful? Give feedback.
-
@GeirGrusom |
Beta Was this translation helpful? Give feedback.
-
I don't think that's useful. convinience in doing so when you need it isn't harmful and the "friction" wouldn't help you to overcome the requirement. consider this example: lock(o)
M(CriticalMethod(), M()); here you have held the lock for uncritical sections as well. either you need to manually capture the result in a local, int result;
lock (o)
result = CriticalMethod();
M(result, M()); or have the compiler generate it for you, M(lock (o) CriticalMethod(), M()); so you release the lock as soon as you're done with it without adding any complexity to the flow of the code. |
Beta Was this translation helpful? Give feedback.
-
We need to bring back support for #552. |
Beta Was this translation helpful? Give feedback.
-
Hehe #552 (comment) |
Beta Was this translation helpful? Give feedback.
-
I know, that's why I said we. 😆 |
Beta Was this translation helpful? Give feedback.
-
You're last example doesn't work unless |
Beta Was this translation helpful? Give feedback.
-
that's what this issue is about.
it's already allowed. (it uses an embedded-statement)
how? |
Beta Was this translation helpful? Give feedback.
-
Oh yeah. Sometimes one gets wrapped up in a new set of comments that you forget what the original proposal was about.
How many other control statements in the language followed by an embedded statement are expressions or can be expressions? |
Beta Was this translation helpful? Give feedback.
-
This would be a completely separate syntax form, just like #101 (btw, lock and using are not a control statement.) |
Beta Was this translation helpful? Give feedback.
-
There is a precedent for having an expression version of that kind of statement: ternary expression for In both those cases, the expression version has different syntax than the statement version, but I don't think there's a reason for that here. The language is moving to be more expression-friendly and this would be another step in that direction. |
Beta Was this translation helpful? Give feedback.
-
with introduction of switch expressions, the need for something like "block expression" is more visible. while switch expressions and :? have specialized syntax and are extremely common, for others I think we don't need a new syntax form, we should just be able to wrap statements inside some kind of "block expression". |
Beta Was this translation helpful? Give feedback.
-
@HaloFour is there a scenario when it is hard to disambiguate |
Beta Was this translation helpful? Give feedback.
-
A core problem is that 'otherwise' case comes up a lot during editing :) |
Beta Was this translation helpful? Give feedback.
-
@CyrusNajmabadi are you saying it would slow down typing performance in IDE around |
Beta Was this translation helpful? Give feedback.
-
It's not a matter of perf. It's a matter of ambiguity and error recovery during editing scenarios. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
instead of
This lets you use type inferred locals which can also be made readonly (#188).
Beta Was this translation helpful? Give feedback.
All reactions