Proposal: A way to eliminate CS0031 #2459
-
Long version explaining everythingFor the last 1-2 years I've been using C#, C and friends alongside each other and I think I found the most annoying thing about C#, it's CS0031. Or at least CS0031 is a symptom of a problem that exists in C++, too. It is the problem of not being able to write simple code. In C I can do something like FILE *f = open_file();
if(f) read_from(f);
else return error; while in C# I would have to do something like try
{
FileStream f = OpenFile();
ReadFrom(f);
}
catch
{
return error;
} And that isn't even the worst part about it, because you could still argue that exceptions are better than returning zero values. But there are many use-cases where you could produce just simpler and functionally 100% equal code, I just grep-ed through some projects of mine and the "== 0" syndrome occures a pretty often, just all the TLDRIt has caused many of my angry moments that in C# you can't use your non-binary values (actually all non- |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments
-
This was a design decision from the beginning due to how often this ends up being a bug for people. I myself just had such a bug because i wrote
This would create 'dialects' of the language, and that's something the language team does not want to do (esp. for something as narrow as this). |
Beta Was this translation helpful? Give feedback.
-
You can emulate this behavior by returning a |
Beta Was this translation helpful? Give feedback.
-
You can still write simple code if you put your mind to it... without requiring implicit conversions to boolean all over the place. if (TryOpenFile(out var f)) ReadFrom(f);
else return error; |
Beta Was this translation helpful? Give feedback.
-
@chrissxyt - Chris, this is one of C#'s strengths, it is very formal in areas where languages like C and C++ are too informal. You seem to be unhappy with the exception pattern too, but this is superior to error codes for a host of reasons one of them being that it's harder for a caller to disregard an error. Your request amounts to making C# weaker in order to make it more attractive for your current coding practices. |
Beta Was this translation helpful? Give feedback.
-
In fact, there's no need to even write such a type as libraries exist, such as my own SuccincT library, that provide such a type for you. |
Beta Was this translation helpful? Give feedback.
This was a design decision from the beginning due to how often this ends up being a bug for people. I myself just had such a bug because i wrote
if (existing = newVal)
in C++ because i was intending to do an==
check, but ended up with an assignment, with a conditional check on the value of that assignment.This would create 'dialects' of the language, and that's something the language team does not want to do (esp. for something as narrow as this).