Replies: 4 comments 1 reply
|
I agree, it is nice to have but requiring it would be a problem. The inline declaration is kinda nice but i'd probably only allow it in places like would feel weird to me (the fact D's i think i'd like that |
|
I looked through some 10 year C# old project from my company to see all the way it is used. Most of usages are in the var divResult = Math.DivRem(a, b, out var remResult);
Math.CosSin(value, out var cosValue, out var sinValue);
// Although this method returns bool we might not care since it might be enough that result will be default 0 if parsing fails.
int.TryParse(someString, out int result);
var value = dict.TryGet(key, out var result) ? result.value : someCustomDefaultValue;
// The example you don't like might be useful just for readability:
// instead of
if (SomeVeryLongClassName.Something && someLongDictionaryName.TryGet(key, out var result)) ...
// it might help to break it into two lines:
var hasResult = someLongDictionaryName.TryGet(key, out var result);
if (SomeVeryLongClassName.Something && hasResult) ...The reason I am mentioning these cases is because I think it will much easier for implementation to just support |
|
yeah |
|
Why isn't C# making their interface towards using the nullable type instead of these C like interfaces. Were these made before nullable was a thing in C#? ValueType? value = dict.TryGet(key);
if (value != null) {
dict[key] = new Something();
}Is an optional type the preferable way? |
Uh oh!
There was an error while loading. Please reload this page.
There are a number of features from C# that I really miss in D and this is the first one that I might be able to make a pull request for.
Currently in D if we want to call a function that has an out parameter we need to declare that variable before the call. In C# that is not necessary which led, among other things, to a great pattern of TryGet methods like:
Where TryGet method returns a bool true if the given key exists and false otherwise and if it does exist the new value variable gets to receive a a value under the given key. The scope of the value variable is the block contain the whole if statement which allows also for a pattern like this:
It also makes it equivalent to declaring the variable before the if and can probably be lowered to that in the compiler.
Now in D the out parameters are not marked in any special ways on a call site which would make above example look like
if (dict.TryGet(key, value)). This in my opinion makes the calling code harder to read and understand. I would like that we have to specifyoutandrefon call sites as well but that would be a nasty breaking change. Maybe we can make it optional but emit a warning if missing, maybe we can have an option that will force it and report an error instead...Would love to hear some opinions on this before I try to tackle it. Thank you :)
All reactions