null == "" is true?

Antonio antonio at abrevia.net
Tue Jul 19 10:29:40 UTC 2022


On Tuesday, 19 July 2022 at 08:10:25 UTC, Kagamin wrote:
> On Monday, 18 July 2022 at 21:23:32 UTC, Antonio wrote:
>> I will study it in detail and report (if required). May be, I 
>> will write the DTO problem with D article if I find time in 
>> august.
>
> In my experience null and empty in DTOs usually play the same 
> logical role. It's a very contrived technical difference 
> without practical usage, such distinction is way beyond any 
> business logic. Even if you implement this distinction, I'm not 
> sure anybody will carefully pay attention to it. In languages 
> that make difference between null and empty, null is often 
> replaced with empty to work around problems with null, such 
> codebase can't properly preserve null values.


When you have to "patch" information partially (i.e.: update only 
the name and the phonenumber, but not the birthdate) or you must 
return in a REST partial information (because graphql or custom 
REST) there is only 2 ways to represent this "possible missing 
properties"

* Maps (key->value) or similiar (i.e.:JSon objects):  You can 
include or not keys in the map:  If you don't want to update the 
birthdate, don't include the birthdate key in the DTO.

* Structs (or any kind of structured data that can be validated 
at compile time):  There is no possibility to say that some 
properties of the struct can be not present... Well, you can if 
you begin to manage Algebraic Types (Union types):   `height:  
int | undefined`

NULL is not the same that UNDEFINED

The distintion is really important:  NULL is a valid value (i.e.: 
The person phonenumber is NULL in database)... Of course, you can 
represent this concept natively in you language (Nullable, 
Optional, Maybe ...) but it is not the same that UNDEFINED... 
because UNDFINED says "This property has not been assigned to 
DTO... do not take it into account".

The summary is that a DTO that works like a Map needs to 
represent the absent key ant this is not the same that the Null 
value

Example:
```d
struct Null { /*...*/ }
struct Undefined { /*...*/ }
struct ContactDto {
  DtoVal!(Undefined, string) name
  DtoVal!(Undefined, Null, string) phonenumber,
  DtoVal!(Undefined, AddressDto) address
}
// ...
ContactDto data = {phonenumber:Null(), 
address:{city:{code:"BCN"}}};
updateContact(id, data);

```





More information about the Digitalmars-d-learn mailing list