7. An optional field can fail without breaking the struct
An optional field constrains a field without demanding it, so when its constraint becomes impossible only that field is ruled out; a required field in the same state makes the whole struct an error.

7. An optional field can fail without breaking the struct

An optional field constrains a field without demanding it, so when its constraint becomes impossible only that field is ruled out; a required field in the same state makes the whole struct an error.

Schemas from different sources can disagree about a field nobody uses without making every configuration invalid.

A question mark makes a field optional. If port is there, it must be an int. It may also be absent.

A field without the mark is required. It must be there.

Make two required fields disagree. Port cannot be eighty and ninety. The whole struct is an error.

Make two optional fields disagree. No value is an instance of both, so the field can never be filled in. But the struct is fine without it.

It is an optional box on a form, with rules nobody can meet. You leave it blank, and hand the form in.

Fill it in anyway, and then it is an error.

Check yourself. What is the result of unifying port?: 80 with port?: 90, and what changes if the fields are required?

Do you like what you are reading? Subscribe to receive updates.

Unsubscribe anytime