Hacker News new | ask | show | jobs
by mafalda 1679 days ago
I’m not a daily go programmer. I’ve learned the language two years ago and know some the background behind it. Based on that I don’t see a place for sum types in Go because it does not solve a problem that is hard to solve with modern Go code.

But considering the proposal itself. I would approach it much differently.

Sum types could be represented as a closed enum.

  type Result enum int{…}
  const ( Ok Result = iota{interface{}}, Err Result = iota{string} )

The initialization would be done like:

  Ok{10}
  Err{“Something went wrong”}
The variable itself would act just like and other enum with unpacking being done somewhat close to this:

  Ok{variableName} := result
Where the variableName would be set to the zero value in case of a bad match.

The enum prefix would enforce a single const block and no extension outside the module package.

1 comments

You are seeing it through the lense of an application developer. As a library author the world looks different though - here you cannot know/define the types in advance.