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

first pass at cheatsheet for scala #348

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

chadselph
Copy link

Hello,

For your consideration, I've written a cheatsheet for Scala programmers. I think by most measures Scala is the most mainstream fp language in industry and has a lot of similarity with Gleam. Both are highly influenced by ML family of languages but use a more C-style syntax.

I'm not sure how you decide which languages deserve a dedicated cheatsheet, and I realize this is more (basically untestable) code that needs to be maintained. So I will be understanding if you're not accepting these types of contributions.

There's also probably some mistakes, I based this off of the PHP cheatsheet but read several others to get ideas for what else to cover. I tried to keep in mind the audience should be Scala programmers looking into Gleam and not as much the other direction, so there's tons of omissions about Scala's huge type system.

Additionally, I think it would make sense to draw a comparison between the use expressions and Scala's for comprehensions, but I didn't see an obvious spot for that. Another minor thing to add is the todo vs Scala's ??? (which seems to have inspired todo) but that seems less important for real-world code.

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.

Thank you! Terribly sorry for the delay, my inbox overwhelmed me

/**
* a very special trait.
*/
trait Foo {}
Copy link
Member

Choose a reason for hiding this comment

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

Could you remove the foobar references please.

Copy link
Author

Choose a reason for hiding this comment

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

yea I think these just came from using php as a starting point; will remove them

size2 = 1
```

Scala uses `val` for immutable bindings and `var` for mutable bindings.
Copy link
Member

Choose a reason for hiding this comment

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

Let's remove explanations and examples of var and the scoping rule below, as we don't need to teach Scala and there's no equivalent in Gleam.

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.

let assert is required, not preferred. let does not result in a runtime error as it does not permit value mismatches

Copy link
Author

Choose a reason for hiding this comment

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

I'll take a pass at re-writing this, but it's also included in the Elm, Elixir and several other pages, so you might want to update those too.

Comment on lines 236 to 243
For methods with no arguments, you may omit the parenthesis

```scala

def noArgs = "Surprise."

```

Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
For methods with no arguments, you may omit the parenthesis
```scala
def noArgs = "Surprise."
```

We don't need to teach Scala

Comment on lines 472 to 494
```scala
def main() = {
val x = {
someFunction(1)
2
}
// Parenthesis are used to change precedence of arithmetic operators
// Although curly braces would work here too.
val y = x * (x + 10)
y
}
```

or using indentation in Scala 3

```scala
def main() =
val x =
someFunction(1)
2
val y = x * (x + 10)
y
```
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
```scala
def main() = {
val x = {
someFunction(1)
2
}
// Parenthesis are used to change precedence of arithmetic operators
// Although curly braces would work here too.
val y = x * (x + 10)
y
}
```
or using indentation in Scala 3
```scala
def main() =
val x =
someFunction(1)
2
val y = x * (x + 10)
y
```
```scala
def main() = {
val x = {
someFunction(1)
2
}
val y = x * (x + 10)
y
}

Can drop some of the info on Scala

}
```

As all expressions the case expression will return the matched value.
Copy link
Member

Choose a reason for hiding this comment

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

What does this mean?

Copy link
Author

Choose a reason for hiding this comment

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

yeah this is from PHP as well and I think the author just meant the case expressions are expressions so

let x = case y {
...
}

x is getting assigned to the "matched value". But I'm going to remove this because "matched value" really sounds more like the input (i.e. y) than the right hand side of the ->

cheatsheets/gleam-for-scala-users.md Outdated Show resolved Hide resolved
cheatsheets/gleam-for-scala-users.md Outdated Show resolved Hide resolved
cheatsheets/gleam-for-scala-users.md Outdated Show resolved Hide resolved
cheatsheets/gleam-for-scala-users.md Outdated Show resolved Hide resolved
@lpil lpil marked this pull request as draft July 25, 2024 12:56
@chadselph
Copy link
Author

Thanks for the thorough review! I'll try to get these edits done soon

@chadselph
Copy link
Author

Sorry that took me way longer than it should have. If I find some more time I can open a separate PR to backport some of these fixes to the Elixir/PHP/Elm pages.

@chadselph chadselph requested a review from lpil November 18, 2024 17:39
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.

2 participants