On Tuesday, 19 July 2022 at 17:05:27 UTC, Kagamin wrote:
Also what's the difference between null and empty phone number?


In a relational database, `NULL` is not the same that `""`... and `NULL` is not the same that `0`. Are semantically different and there are database invariants (like foreign keys) based on it. Trying to "mix" this concepts in a database is a mistake.

When you treat with Domain Models, you try to represent this semantics in all levels of your software... including APIs

If your address has not a reference to the city... then there is a `city_code` field with `NULL` value in your database and the Address model object has a city property representing this Null value with the tools that language/library offers to you: `Nullable!City` or `Sumtype!(Null, City)` or `Optional<City>` or ...).

`Null` is then a valid value state that has nothing related to *`null` pointers*... to avoid confusion, I talk about `Null` instead `null`.

I, personally, prefer to use Union types (algebraic types) like `int | Null | ...` because it is the best option to introduce the Undefined state (that allows statically typed data to represent the absence of a property).

`Sumtype!(Undefined,Null,int)`  instead  `Optional!(Nullable!int)`

Then there is the `match!` syntax, the unified way to treat JSON serialization and the easy way to implement a custom "dot" accessor that propagates the Null or Undefined state in a chain of references:

```d
person.ns.address.ns.city.ns.name.match!(
  (string name){ ... }
  (Null) {...}
  (Undefined) { ... }
)

```




Reply via email to