New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Simpler way to specify leaveOpen in StreamWriter constructor #17157
Comments
There already is a The constructor system for this class is a bit messed up. These mistakes were made in the 1.0 days. Option 3 seems best because it does not add more confusing overloads. Should the "default" buffer size be specified by |
@GSPP I think that
I haven't looked at other types. |
Right, I was concerned about the "hole" but I was not able to express that.
Could we make the buffer size arguments Maybe we could make a new We also could change the booleans to |
I think so, I can't think of any situation where it would break compatibility. And it's not a bad idea, assuming this will become the accepted way for optional value type parameters in .Net Core/.Net Framework going forward. |
We need to think it through from source compat point of view and finalize the proposal & review it. @JeremyKuhne can you please do it? |
@JeremyKuhne, this is assigned to you. Are you actively working on this? I'm assuming not since it's been assigned to you for two and a half years ;) |
That's a fair assumption. :) Clearly this slipped under my radar. I have hit this numerous times myself. My vote here is for the targeted fix for the main pain point: public StreamWriter(Stream stream, bool leaveOpen); While on one hand having a more usable/flexible constructor (or perhaps even factory methods) would be nice I think we could end up duplicating all the options in |
Just realized that adding a bool would butt heads with |
For consistency with
A new const All of the |
It isn't public on
I like that option. We can do the same for Summary namespace System.IO
{
public class StreamReader : TextReader
{
public bool LeaveOpen { get; set; }
}
public class StreamWriter : TextWriter
{
public bool LeaveOpen { get; set; }
}
} |
Yes, I missed that. It's an internal constant on
I considered suggesting an options class (like EnumerationOptions) but that felt too heavy handed and intrusive of a change for this specific request. If other configuration options get introduced, then it's definitely a better way to go than having a bunch of properties/constructor args/additional constructors. For this request, just making the |
We'd prefer the first option (i.e. default constructors), reason being:
The counter argument is that it might make it harder for folks to find bugs because we'd now accept |
The approved API is to add the default values and change the behavior of null encoding and -1 for buffer size.
namespace System.IO
{
public class StreamReader : TextReader
{
public StreamReader(Stream stream, Encoding encoding = null, bool detectEncodingFromByteOrderMarks = true, int bufferSize -1, bool leaveOpen = false);
}
public class StreamWriter : TextWriter
{
public StreamWriter(Stream stream, Encoding encoding = null, int bufferSize = -1, bool leaveOpen = false);
}
} |
@terrajobst and @JeremyKuhne, given the approved API, does that mean the decision in https://github.com/dotnet/corefx/issues/470#issuecomment-147809240 is no longer a concern?
|
In some cases we'll be blocked due to ambiguity with existing constructors I'm sure. Outside of that we can use optional arguments as long as we (1) put them on the constructor with the most arguments and (2) move the arguments forward if we add a longer constructor in the future. @terrajobst, is that a good summary? |
@JeremyKuhne is it still up-for-grabs? |
@jimdemis Yes it is- want to take care of it? |
@JeremyKuhne yes I do. Should I make two pull requests for this one? (in corefx and coreclr) |
@jimdemis I sent you collaborator invite - once you accept, please ping us here - we will be able to assign it to you then. |
@karelz done |
Should |
* Added optional/default parameters for StreamWriter/StreamReader Fix #8173 * removed tests * Added tests for constructors with optional arguments * Added tests and reverted null encoding throws
Currently, the
StreamWriter
constructor has the following overloads:I believe a relatively common need is to set
leaveOpen
totrue
, while keeping bothencoding
andbufferSize
at their default values.But the process of doing that goes something like this:
leaveOpen
.StreamWriter(Stream stream)
, figure out that the default encoding is UTF-8. (Though it doesn't mention it's actually UTF-8 without BOM.)StreamWriter
.new StreamWriter(stream, Encoding.UTF8, 1024, true)
.I don't think that's acceptable, so I'm proposing to make
new StreamWriter(stream, leaveOpen: true)
work.I can see few options on how to do this:
Assuming optional arguments are okay (they probably aren't, see dotnet/corefx#470), make use of them by changing the full overload to:
This would require relaxing
encoding
andbufferSize
to acceptnull
and-1
, respectively, but I think that's okay.I think this is the most convenient option, since it allows one to easily specify any combination of options.
Add a new constructor overload:
Adding some other overloads might make sense too.
Don't add anything, just relax
encoding
andbufferSize
as in option 1.I think this is the most conservative option, but also least convenient, since it would require writing
new StreamWriter(stream, null, -1, true)
.The text was updated successfully, but these errors were encountered: