Something like ADL from C++?
Derek Fawcus
dfawcus+dlang at employees.org
Thu Dec 5 19:07:42 UTC 2024
On Tuesday, 3 December 2024 at 19:34:21 UTC, Alexandru Ermicioi
wrote:
> On Tuesday, 3 December 2024 at 11:55:50 UTC, Manu wrote:
>> Has anyone ever experimented with a pattern like this?
>> Essentially, I can't work out how to expand /combine an
>> overload set to include symbols from multiple imports....
>
> as a compromise you could have an uda that packs custom
> serialisation defined on type itself or field of that type,
> then you can make serializer employ it when it sees it
> somewhere.
That is essentially what Go does for its serialisation (Marshal
and Unmarshal) routines.
Only the public fields of a struct can be processed, and how to
handle a field in a non default fashion is marked by an attribute
- an extra string.
```Go
type DelSite struct {
SiteID types.SiteID `json:"site-id" validate:"min=1"`
}
```
One of those is then encoded as follows, where 's' happens to be
a struct ptr:
```Go
var b []byte
b, err := json.Marshal(s)
```
It is using run time reflection, but I see no obvious reason why
compile time reflection could not be used instead, since that is
what D has.
More information about the Digitalmars-d
mailing list