7,974 reputation
761122
bio website area51.stackexchange.com/…
location Brazil
age 44
visits member for 3 years, 7 months
seen yesterday

former Moderator on Programmers.SE


Jan
26
reviewed Approve suggested edit on Mapping values over a curve
Jan
24
awarded  Good Question
Dec
11
awarded  Favorite Question
Nov
23
awarded  Informed
Nov
21
awarded  Nice Question
Oct
30
comment Why can't Java/C# implement RAII?
I totally understand now.
Oct
30
comment Why can't Java/C# implement RAII?
Yes, exactly what I said. I understand you now. There are problems for other situations, sure. Probably the compiler would need to forbid some constructs. Compilers do this every time. If this is good or bad is matter of discussion which doesn't fit here. I'm not sure if this new example is unsolvable (I have no enough information to discuss this). So you could say write using is easier and I can agree, but the point of this question it is if is is feasible. If you ask me if I want get rid of using on C#, I say a big NO. I'm talking by language design pov.
Oct
30
comment Why can't Java/C# implement RAII?
@RobertHarvey IMHO your answer is not wrong either, just it was not fit in this question. For me it's about language design. I agree with you that C# or even Java team shouldn't waste resources in that feature. I see the usefulness of this feature and I see the cost for the language and for the implementation.
Oct
30
comment Why can't Java/C# implement RAII?
My understanding, changing you example to just a return, is that the compiler would insert a try just before the resource acquisition (line 3 at your example) and the finally-dispose block just before the end of scope (line 6). No problem here, agree? Back to your example. The compiler see a transfer, it couldn't insert try-finally here but the caller will receive a (pointer to) File object and assuming the caller is not transferring this object again, compiler will insert try-finally pattern there. In other words every IDisposable object not transferred need to apply try-finally pattern.
Oct
30
comment Why can't Java/C# implement RAII?
I fail to see what you said now (i'm not saying you are wrong). The object has a resource, not the reference.
Oct
30
comment Why can't Java/C# implement RAII?
@mike20 but if you need to be aware that a type is a resource and you are forced to write something to tell to compiler to do a proper work you're explicit. I disagree with Robert in almost everything he says in this question but he is right saying this answer just get rid of braces and a nested scope and when he's saying the using is not a sugar.
Oct
30
comment Why can't Java/C# implement RAII?
blogs.msdn.com/b/oldnewthing/archive/2010/08/10/10048150.aspx is a relevant page to this question.
Oct
30
comment Why can't Java/C# implement RAII?
I fail to see implicitness which is the original requirement from OP. "more implicit than" is strange to me, or it is implicit or it is explicit. Disclaimer: I am just interested in language design here, I'm not saying that C# or Java should implement this.
Oct
30
comment Why can't Java/C# implement RAII?
It's similar, not exact duplicate.
Oct
30
comment Why can't Java/C# implement RAII?
Why you need copy/move constructor? File stills a reference type. In that situation f which is a pointer is copied to the caller and it is responsible to dispose the resource (the compiler implicitly would put a try-finally-dispose pattern in the caller instead)
Sep
8
awarded  Yearling
Aug
27
awarded  Popular Question
Aug
2
awarded  Popular Question
Jul
14
awarded  Good Question
May
15
awarded  Good Answer