Hey,

I thought I provide some "realworld" feedback for the Go 1.18 beta and 
generics.

I recently spend some time implementing a concurrency framework that mimics 
Scala's Futures and Promises 
<https://docs.scala-lang.org/overviews/core/futures.html> API in Go as a 
learning exercise.

You can find the source code at https://github.com/joa/go18beta

In general it was very easy to get started with Go 1.18 and the added 
generics. Initially surprising was the fact that member methods can't be 
further parametrized but given the implementation decisions this makes 
sense.

The absence of type erasure (disclaimer: I'm not a fan of type erasure!) 
made it harder to implement certain aspects. The code contains a linked 
list of callback[T] structs and I wanted to have a shared terminal or nil 
callback so to speak. I did get around this by creating a global variable 
of callback[any] that I can use with help of unsafe.Pointer in some places. 
It would be interesting to learn whether this is expected to work at all. I 
assume it only works here because callback[T] is never converted to an 
interface tuple and I also never access nilCallback's fields.

Another aspect of this are some typical ADTs (like try.Try or 
option.Option) and for a method FlatMap[T, U](in Option[T])Option[U] I 
should in theory be able to re-use any Option[T] as an Option[U] as long as 
the compiler can prove T == U. I was not able to find any information about 
how to establish such a constraint or whether that's possible at all. 
Reusing the original instance would obviously crash at runtime when T != U 
since the interface type is different. 

In general this was a nice learning exercise and helpful in understanding 
some of the implementation decisions. While debatable whether a Future[T] 
would be idiomatic Go I think a type like Try[T] is actually interesting as 
it ensures that any panic is automatically recovered. 

While working on this project I did not experience any bugs other other 
unexpected behavior. I must say though that I did not use the constraints 
at all. Co- and contravariance would've been useful here but I also 
understand this is not part of this release.

Anyways, that's just some feedback. Maybe you find this helpful or 
interesting.


Best,

Joa

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/0dd15089-d148-43b4-b58a-41dd608eba26n%40googlegroups.com.

Reply via email to