Creating a serde-like framework for Go using generics?

285 views
Skip to first unread message

Keith Wipf

unread,
Jan 12, 2021, 3:29:58 PM1/12/21
to golan...@googlegroups.com
If you've ever worked with Rust, you've probably sceen the serde framework.
My question is, once Go has generics, would it be possible to write
something like this in Go? This seems like something that could really
benefit the language.
The only drawback I can see so far as that you'd have to manually
write serialize and deserialize functions for your types, but that
seems a fair trade off for getting good performance without using a
code generator.
Also, will Go generics support specialization? I thought that I saw
somewhere that you would be able to switch on a type parameter at
compile time. I'm thinking you'd need this to serialize ints and other
primitive types because you can't implement any serialize functions on
those without making a new type.
Thanks

Ian Lance Taylor

unread,
Jan 12, 2021, 3:50:59 PM1/12/21
to Keith Wipf, golang-nuts
On Tue, Jan 12, 2021 at 12:29 PM Keith Wipf <keith...@gmail.com> wrote:
>
> Also, will Go generics support specialization? I thought that I saw
> somewhere that you would be able to switch on a type parameter at
> compile time. I'm thinking you'd need this to serialize ints and other
> primitive types because you can't implement any serialize functions on
> those without making a new type.

The current type parameters design draft does not support
specialization. Specialization would significantly increase the
complexity, as it would be necessary to clearly define when a
specialization would apply.

In Go, serialization is more likely to be implemented using
reflection, as is done today, without generics, by packages like
encoding/json.

Ian

Sebastien Binet

unread,
Jan 12, 2021, 4:05:29 PM1/12/21
to keith...@gmail.com, golan...@googlegroups.com
With 'go/types' and the like, one can relatively easily automatically generate the (de)serialization code.

See:
- go-hep.org/x/hep/brio

IIRC, specialization is off the table at the moment.

-s








-------- Original Message --------
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/CAJRCDozXFVfTUOfEeC1tE9xrdB4KQEzAXpfHgOGA26wFPq6jjQ%40mail.gmail.com.

Reply all
Reply to author
Forward
0 new messages