Add hasValue function to Nullable
Hi,
Could you add a function hasValue to the Nullable<T> type? It should be the opposite of isNull. It is so much more readable to write: if someVar.hasValue and otherVar.hasValue then doSomething(someVar.value, othervar)
instead of if (not somevar.isnull) and (not othervalue.isnull) then
Comments (9)
-
repo owner -
reporter I am sorry, you are right. I meant adding an isNull method ;-) I manually added it and mixed up what was the original one. It seems redundant but having both functions makes code more readable instead of using "not"
-
repo owner By that argumentation you could duplicate every boolean property ("Hey, you have Enabled, can you please Disabled which is the opposite? And while at it please also add Inactive and Invisible") ;)
Also by the code you showed I cannot imagine many places where you explicitly check for IsNull than rather for HasValue given that you have functions like GetValueOrDefault where you can provide a value in case the nullable does not contain one.
-
reporter By that argumentation you could duplicate every boolean property You have point there. Still "isNull" for a nullable seems a logical method to call.
Also by the code you showed I cannot imagine many places where you explicitly check for IsNull than rather for HasValue I do have a lot of code where the value of properties is replaced by something else when property itself is null. I am currently porting this code from TMS Aurelius to the Spring4D framework. The Nullable<T> of Aurelius does have both 'isNull' and 'hasValue', so I added it to the Spring4D Nullable<T> to make my live easier. I guess I will have to refactor the code to make it only use hasValue.
-
reporter sorry, I duplicated your responses, but the post stripped some of the formatting
-
repo owner What does the code typically look like when it uses
if someVar.IsNull then
? -
reporter My object has a property Inkoopkorting (discount), when it is set (has a value) I want to use its value, otherwise I calculate the value using other properties. My code is full of this kind of constructs. I could refactor them, and it would be equally readable to be honest, it's just a lot work ;-)
function TBasisRegel.GetInkoopkorting: Single; begin if FInkoopkorting.IsNull then begin if (FBrutoInkprijs.ValueOrDefault<>0) and FNettoInkprijs.HasValue then result := (1-(FNettoInkprijs.Value/FBrutoInkprijs.value))*100 else result := 0; end else result := FInkoopkorting.value; end;
I could refactor to
if FInkoopkorting.hasvalue then result := FInkoopkorting.value else begin if (FBrutoInkprijs.ValueOrDefault<>0) and FNettoInkprijs.HasValue then result := (1-(FNettoInkprijs.Value/FBrutoInkprijs.value))*100 else result := 0; end
I went through my code and the more complex if statements all use hasvalue. So it would make more sense to have a hasValue if you could have only one. So forget I asked, I will refactor my Aurelius code so I only use hasvalue.
-
repo owner It's just reversing the true and false branch of the if statement then.
FWIW you can also write this:
if not FInkoopkorting.TryGetValue(Result) then begin if (FBrutoInkprijs.ValueOrDefault<>0) and FNettoInkprijs.HasValue then result := (1-(FNettoInkprijs.Value/FBrutoInkprijs.value))*100 else result := 0; end
I will close this as won't fix. I think for migrating your code it might be helpful to add the IsNull property and keep or refactor later. But imo there is no reason to add it to the library.
-
repo owner - changed status to wontfix
- Log in to comment
I think you are confusing something. Nullable<T> has no property called IsNull but only the HasValue property.