Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create gleam-for-go-users guide #321

Open
wants to merge 10 commits into
base: main
Choose a base branch
from

Conversation

gamebox
Copy link

@gamebox gamebox commented Mar 8, 2024

This is a first draft and needs some editing and fact checking.

Closes #413

This is an initial version that is mostly a copy of gleam-for-php-users.md
Made a lot of progress...
@gamebox gamebox marked this pull request as ready for review March 8, 2024 15:24
Copy link
Member

@lpil lpil left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nice! This must have taken some time to write! Fantastic work.

I've left a bunch of small notes inline 🙏

Thank you

Comment on lines +146 to +149
In Gleam, `let` and `=` can be used for pattern matching, but you'll get
compile errors if there's a type mismatch, and a runtime error if there's
a value mismatch. For assertions, the equivalent `let assert` keyword is
preferred.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not just preferred! It's enforced. Could you rework to make it clear that let won't cause a runtime error.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Want to make this clear, if I have a value from Erlang that does not match the interface put on that code, and I do a let with pattern that does not match that value it does not cause a runtime error?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The compiler won't let you match a let pattern if it doesn't match all possible cases. let assert will cause a runtime error if the pattern doesn't match

cheatsheets/gleam-for-go-users.md Outdated Show resolved Hide resolved
cheatsheets/gleam-for-go-users.md Outdated Show resolved Hide resolved
cheatsheets/gleam-for-go-users.md Outdated Show resolved Hide resolved
cheatsheets/gleam-for-go-users.md Outdated Show resolved Hide resolved
cheatsheets/gleam-for-go-users.md Outdated Show resolved Hide resolved
cheatsheets/gleam-for-go-users.md Outdated Show resolved Hide resolved
cheatsheets/gleam-for-go-users.md Outdated Show resolved Hide resolved
cheatsheets/gleam-for-go-users.md Outdated Show resolved Hide resolved
cheatsheets/gleam-for-go-users.md Outdated Show resolved Hide resolved
@nii236
Copy link

nii236 commented Apr 22, 2024

@gamebox are you planning on updating this PR?

@gamebox
Copy link
Author

gamebox commented Apr 22, 2024 via email

cheatsheets/gleam-for-go-users.md Show resolved Hide resolved

In Go, you can choose to not export a type and then they will be considered
opaque to the user of your package. Just have the type have a lowercase
identifier.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will be opaque from outside the package, which is the same thing as private? I might be misunderstanding

cheatsheets/gleam-for-go-users.md Show resolved Hide resolved
cheatsheets/gleam-for-go-users.md Outdated Show resolved Hide resolved
cheatsheets/gleam-for-go-users.md Outdated Show resolved Hide resolved
cheatsheets/gleam-for-go-users.md Outdated Show resolved Hide resolved
gamebox and others added 2 commits May 10, 2024 05:34
This will save some editing work, before I address the meatier comments.

Co-authored-by: Louis Pilfold <[email protected]>
@gamebox
Copy link
Author

gamebox commented May 10, 2024

This should be ready for your review when you get a chance @lpil

@gamebox
Copy link
Author

gamebox commented Jul 22, 2024

I'd really love to close this out, if you get a chance @lpil .

Copy link
Member

@lpil lpil left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello! Sorry for the delay.

It seems that not all my comments have been addressed here. Let me know when the PR is ready

cheatsheets/gleam-for-go-users.md Outdated Show resolved Hide resolved
@lpil lpil marked this pull request as draft July 25, 2024 12:24
@gamebox gamebox marked this pull request as ready for review July 25, 2024 13:32
cheatsheets/gleam-for-go-users.md Show resolved Hide resolved
cheatsheets/gleam-for-go-users.md Outdated Show resolved Hide resolved
cheatsheets/gleam-for-go-users.md Outdated Show resolved Hide resolved
cheatsheets/gleam-for-go-users.md Outdated Show resolved Hide resolved
cheatsheets/gleam-for-go-users.md Outdated Show resolved Hide resolved
cheatsheets/gleam-for-go-users.md Outdated Show resolved Hide resolved
@MiniAppleTheApple
Copy link
Contributor

isn't it better to use the equivalent/similar example in the Unions section?

@gamebox
Copy link
Author

gamebox commented Aug 4, 2024

@MiniAppleTheApple Well, there really isn't a similar approach unless you mean a struct that has a tag and then contains pointers to structs for all possible "variants" or contains all of the fields relevant to all variants. But I personally think it's more honest to say "This really isn't a thing you would do in Go". Plus, union semantics are just different then either of the two options I described above.

@gamebox
Copy link
Author

gamebox commented Aug 4, 2024

All conversations are resolved, and suggestions merged. Looks like now this should be good for a final review.

@MiniAppleTheApple
Copy link
Contributor

@MiniAppleTheApple Well, there really isn't a similar approach unless you mean a struct that has a tag and then contains pointers to structs for all possible "variants" or contains all of the fields relevant to all variants. But I personally think it's more honest to say "This really isn't a thing you would do in Go". Plus, union semantics are just different then either of the two options I described above.

Even though there isn't a 100% similar way, you can still approximate both example code as close as possible. I will give a suggestion later.

Comment on lines +32 to +38
- [Unions](#unions) IN PROGRESS
- [Opaque custom types](#opaque-custom-types) IN PROGRESS
- [Modules](#modules) IN PROGRESS
- [Imports](#imports) IN PROGRESS
- [Named imports](#named-imports) IN PROGRESS
- [Unqualified imports](#unqualified-imports) IN PROGRESS
- [Architecture](#architecture) IN PROGRESS
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these actually "in progress"? Or should this note be removed?

Comment on lines +1371 to +1384
- Go is an imperative programming language, with only very limited object-oriented facilities
provided. Gleam offers only functional code style, though it can appear
imperative and reads easily thanks to pipes.
- In Gleam, data structures are never mutated but always updated into new
structures. This allows processes that fail to simply restart as there are no
mutated objects that can be in an invalid state and take the whole
application down (such as in languages like Ruby or Go).
- Gleam offers syntax to make it easy to extract data out of custom types and
update data into new copies of custom types without ever mutating variables.
Go sometimes directly mutates references of simple values such as when using
pointers or global variables, though this is very much not idiomatic.
- Gleam allows to rebind variables freely to make it easy to update data
structures by making a copy and binding it to the existing variable.
- Go features a massive, powerful standard library centered around simple interfaces.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These aren't really dot points, but paragraphs.

I would:

  • Remove the dot points
  • Move the last paragraph to the second paragraph so the Go and Gleam paragraphs are kept together


To iterate a few foundational differences (Go VS Gleam):

1. Programming model: imperative VS functional immutable
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A common Go-ism is something like:

  • Declare struct
  • Declare constructor
  • Declare methods on struct

It would be handy to see how you'd achieve this in Gleam.

I know that for Gleam some people recommend a "context" struct that you pass in as the first argument which contains what would normally be Go struct fields: Database connections, loggers, HTTP clients etc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Cheatsheet: Gleam for Go users
6 participants