Replies: 72 comments 1 reply
-
Alternatively, we could just relax parsing rules to allow Javascript-like IIFE. You'd have to always specify a target type, though (lambda functions don't work with // currently gives a "Method name expected" error
int x = (() => {
try { return int.Parse(text); }
catch(Exception e) { return -1; }
})();
// could look a bit nicer with the delegate syntax, where a block body is mandatory, so no need to wrap the function in extra parens
// drawback: you have to scroll down to find out that the expression is actually invoked
int x = delegate {
try { return int.Parse(text); }
catch(Exception e) { return -1; }
}(); |
Beta Was this translation helpful? Give feedback.
-
@apskim At first I thought about that syntax But I have 3 problems come in my mind first. This syntax make it being delegate. So it will make a stack and consume performance and the worst is it will not be able to access second. The last third. By that syntax is cannot |
Beta Was this translation helpful? Give feedback.
-
Proposed also here. Regarding syntax, here's another suggestion:
Using a block prefix (
|
Beta Was this translation helpful? Give feedback.
-
I understand the costs of using delegates for this but given the amount of times you would use this the impact on performance would probably be meaningless.
I can't see you pass anything in your examples but besides this delegates can access ref parameters, do you speak about the built-in/predefined delegate types?
You're right
The proposed syntax doesn't make it obvious that you make a function call so given the circumstances I'd say seeing Bottom line I think that once the language might have better support for type inference for delegate types and support for iterator blocks, combining this with my suggestion #148 would probably cover this case. |
Beta Was this translation helpful? Give feedback.
-
@eyalsk You again..... I was already said in the post itself that
I was just propose that we should have new syntax that should solve all those problem. And you came downvote without reading then said that
That's what I am doing right now. Anonymous local function, by any syntax, must be distinct from delegate. So it will do what delegate can't But, as always, you just come to mess with my thread with your naiveness that we should just use delegate and overlook all the problem I want to solve Simply put. Your #148 is not solve these problems. It can't access So I think it better be the real anonymous local function instead And if you don't know, like the time you don't know that In C# this is valid syntax void A()
{
var a = 0;
{
var b = a;
DoAnything();
}
DoMorething(a);
} |
Beta Was this translation helpful? Give feedback.
-
@eyalsk You always make the same mistake by naively overlook and never understand anything when I propose any proposal Same as when you never look at This again
While I was already state that
You still need example even for the obvious mentioning delegate and lambda cannot access |
Beta Was this translation helpful? Give feedback.
-
@Thaina are local functions not what you are proposing here? |
Beta Was this translation helpful? Give feedback.
-
@orthoxerox Actually I think that, it is really just inline. It actually direct code than local function. Just make a sub block that can return or yield. So it really should not push stack like a function That why I was thinking at first it should call This actually could be syntactic sugar so it might be transpiled instead of create the real object out of it |
Beta Was this translation helpful? Give feedback.
-
I don't understand the value of this proposed feature now that C# has local functions. |
Beta Was this translation helpful? Give feedback.
-
@gafter Well, yes but then you need to define it and then execute it whereas here I guess it offers a syntax that does both in "one go". p.s. This is the reason I argued that it's more natural to extend the support of anonymous delegates or lambda in the language than adding yet another syntax for something the former may cover as shown by @apskim. |
Beta Was this translation helpful? Give feedback.
-
@gafter I could say that, by your reason, local function would already solve Same go for many other case that local function can be used. Sometimes that local function is just run at only one place So It more natural to "define it and then execute it whereas" and "does both in one go" (thanks @eyalsk for having a words to borrowed). And we also don't need to name it That's why at first I think it is Well, I have seen you don't understand why people want anonymous local function from the start so that explain it. You always think local function itself is enough |
Beta Was this translation helpful? Give feedback.
-
@eyalsk Why I don't know what you try to defend about your proposal. I was even support it As I always said, you always try to suggest me to use axe in place of chisel while I already know it could use axe but I said it should use chisel By using anonymous delegate immediate call, how can you Delegate call normally cannot intake local It more intuitive to be at least local function immediate call, not delegate, it defeat the point of this proposal And the main reason of this proposal is. It must not be delegate. What if this logic was inside the loop that will run 1000 times per second? It has no reason to waste performance here. It should just inline in all case. And this feature will be useless if just making a short block will result in performance waste so we need to fallback to local function when it critical. We need to have code consistency. It better if we can always use feature without caring that it will impact performance And I have seen you not try to understand Also I always said why you wrong. You just overlook it. You just not care. You even skip when I prove what you misunderstand. How can I make you understand when you not try to because you think you already understand everything |
Beta Was this translation helpful? Give feedback.
-
I understand but I asked you to give me an example because I don't understand what you mean here, for example it's perfectly valid to have this
I understand that too so I told you maybe it would make sense to create a proposal so this could be possible.
I don't want it to be special I just want to extend the support of delegate in the language through the syntax of lambdas and I think that this would cover this proposal but of course we disagree on this. :)
I'll just state what I think about performance: Premature optimization is the root of all evil. There are bunch of features that are pretty heavy and yet people use them all the time like async/await it generates bunch of things and allocations and yet people use this. You can't always use structs, you can't always use classes, you can't always use LINQ. You always need to do benchmark tests especially in hot spots so I don't think this would be any different although I'm not sure about the benefit of this over say local functions or/and extend the support of delegates in the language to allow for something like this.
I don't know what to say but you make the assumption that I don't understand just because I think that this fits better into the closure world which in C# is offered through delegates.
Thaina I think that the issue here is you make the assumption that I'm wrong or/and that I don't understand or/and that I overlook things and/or that I don't care where I always try to understand and when I'm wrong I don't have issues admitting that but atm I don't think that I'm misunderstanding you, I'm just disagreeing with you. BTW, I don't have anything against you, I like some of your suggestions and this is why I'm still here willing to discuss it with you. p.s. It may be a good idea to delete all our posts above and start discussing the feature rather than one another, what do you think? :) I've deleted my posts, people don't need to read all this. |
Beta Was this translation helpful? Give feedback.
-
This is what I call not understand as I said you just never accept that you don't really understand Seriously, I didn't said that you don't understand anything. I said that you don't understand everything. You might understand manythings about what I propose but it not every OK? In terms of var youNotUnderStandAnything = !yourUnderstandness.Any((part) => part); // false
var youNotUnderStandEverything = !yourUnderstandness.Every((part) => part); // true But whenever I point out that you don't really understand. You are the one that always drama to defend yourself that I was attacked you. I am arrogance and so on This always happen when you argue with me
I create this proposal to make it possible. That is the point of this proposal that by avoiding delegate is better approach
You cannot extend support to delegate to intake local ref variable because being delegate will never guarantee to call in the same stack. You still not try to understand the problem at all. Or did you really don't know what is
It will not need And that mantra, But Java people also using that mantra In this case I would consider it is the premature optimization in the same level as having I don't think you would agree but you should at least understand
I just have a prove that you misunderstand the word |
Beta Was this translation helpful? Give feedback.
-
Clearly. 😉 |
Beta Was this translation helpful? Give feedback.
-
I think Rust has a nice syntax for doing this; it'll be useful in a whole bunch of situations for C# too. It allows you to use a block of code as an expression by omitting the semicolon on the last expression. Here's how that might look in C#: var person =
{
var firstName = "Joe";
var surname = "Bloggs";
new Person(firstName, surname)
}; |
Beta Was this translation helpful? Give feedback.
-
@Richiban Interesting but what if we want to make if/switch clause of try/catch block? |
Beta Was this translation helpful? Give feedback.
-
@Richiban, But the idea has merit. Taking my code sample from #1174, such an expression block could be used to simplify using blocks even further: public void Foo()
{
using (var connection = { var c = connection.Open(); c.Open(); return c; })
using (var command =
{
var c = connection.CreateCommand();
c.CommandText = "SELECT FOO FROM BAR" ;
return c;
})
using (var reader = command.ExecuteReader())
{
while (reader.Read())
{
ProcessRecord(reader);
}
}
} |
Beta Was this translation helpful? Give feedback.
-
@DavidArno is it really possible to have only I felt like it was already syntax. Maybe I remember it wrong |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
Hmm, existing syntax destroys another great idea. Hitting a Back to the drawing board... |
Beta Was this translation helpful? Give feedback.
-
Currently now I think var obj = true switch {
case true => {
try { return int.Parse(text); }
catch(Exception e) { return -1; }
}
} Well, could we just var obj = using { // yes, using nothing and bracket is the syntax
try { return int.Parse(text); }
catch(Exception e) { return -1; }
} |
Beta Was this translation helpful? Give feedback.
-
There is no expression that begins with |
Beta Was this translation helpful? Give feedback.
-
@gafter Does the var obj = true switch {
case false => 0;
case true => {
return 1;
}
} |
Beta Was this translation helpful? Give feedback.
-
No, |
Beta Was this translation helpful? Give feedback.
-
That's so sad. So I would still go on with this proposal |
Beta Was this translation helpful? Give feedback.
-
Alternatively, could we use var x = let {
try
{
return int.Parse(text);
}
catch(Exception e)
{
return -1;
}
};
let { // can be void
if(!needToDoSomething)
return;
// DoSomething
} |
Beta Was this translation helpful? Give feedback.
-
Just an idea, in this syntax Edit: also, while throwing ideas around, to make it visually distinct, instead of |
Beta Was this translation helpful? Give feedback.
-
@amis92 from {
if(!needToDoSomething)
select; // seem weird
// DoSomething
} |
Beta Was this translation helpful? Give feedback.
-
Java has this syntax var x = switch (shape.getType()) {
case RECTANGLE -> {
var rect = (Rectangle) shape;
yield rect.getWidth() * rect.getHeight();
};
case CIRCLE -> {
var circle = (Circle) shape;
var radius = c.getDiameter() / 2;
yield Math.PI * radius * radius;
};
default -> throw new IllegalOperationException();
}; I wish that C# should adopt the same syntax instead of Declaration Expressions. And I would like to have the same syntax for this issue var x = { // normal code block
var rect = (Rectangle) shape;
yield rect.getWidth() * rect.getHeight(); // use yield to distinguish from return
}; |
Beta Was this translation helpful? Give feedback.
-
I have seen some proposal (include mine) wanting to have a value set from block syntax
And the approach mostly suggest that we should modified some syntax into one-line-able syntax
inline try
#220 for exampleWhich I have seen it more and more complicate to fit everything in one line
So I want to propose alternative. We should have some syntax that could make a block to imitate function. The block that make a scope variable not leaked out. Can return value(s) and do complicate task like local function. But immediately execute like a code there
I don't know what operator we could use, please bear with my
=:
for nowThis block syntax work like normal
{}
scope. It can access outside variable but variable declared in that block would not leaked. And can usereturn
to return value from the block to variable outsideIt should also be able to return void to just do a logic that could be cut short
And I wish we could yield
Beta Was this translation helpful? Give feedback.
All reactions