master-wiki/Readwise/All You Need Is Data and Functions.md

31 lines
3.2 KiB
Markdown

# All You Need Is Data and Functions
![rw-book-cover](https://readwise-assets.s3.amazonaws.com/static/images/article1.be68295a7e40.png)
## Metadata
- Author: [[mckayla.blog]]
- Full Title: All You Need Is Data and Functions
- Category: #articles
- Document Tags: [[dev]] [[star]]
- URL: https://mckayla.blog/posts/all-you-need-is-data-and-functions.html
> [!note]
> **Background:** I would like to implement the ideas from "All you need is data and functions" by mckayla.blog in my programming project.
> ### Key Takeaways:
> 🛠️ **Emphasize Type Over Traits:** Instead of relying on traits, create a data-type that encapsulates the desired behavior. Then, implement a conversion function to transform your data-type into the trait-type when needed.
> 🔄 **Utilize Implicit Conversions:** Understand that trait-types allow for implicit conversions, which can simplify your code. This means the language can handle the transformation from your data-type to the trait-type automatically, reducing boilerplate and improving readability.
> 📜 **Represent Traits with Types:** Recognize that traits can be viewed as types themselves. For example, a Display trait can be represented by a String type alongside a function that converts your original type into a String, enabling clearer and more direct handling of type-specific logic.
> ---
> 1. How does the omission of traits in Gleam influence the way developers approach generic programming compared to languages like Rust?
> 2. In what ways might the simplicity of Gleam's design, focusing on data and functions, lead to both advantages and disadvantages in software development?
> 3. How does Gleam's approach to immutability and function-based programming compare with the mutable state and trait systems commonly found in other languages?
> [!tldr]
> The document explores the concept of traits in programming languages, using the example of the Gleam language. Traits are compared to types and functions, emphasizing how traits can be represented and achieved through types and functions. The discussion covers examples from Rust and Gleam to illustrate how traits can be replaced by types and functions in a language like Gleam, where simplicity and low concept count are valued. The document concludes that data and functions can effectively replace the need for traits, particularly in languages like Gleam.
## Highlights
**traits are just types**. Our `Display` trait in this example, can be represented by the `String` type, and a function which converts from our original type to a `String`. - ([View Highlight](https://read.readwise.io/read/01j6axgxbj76x7jbz4zsc45tdv))
The biggest difference between most trait/interface systems and trait-types is that the language is essentially doing implicit conversions for you, from your data-type to the trait-type. - ([View Highlight](https://read.readwise.io/read/01j6ccvpskph772e8r479esbq4))
Instead of a trait, just make a type that implements the generic behavior you want, and then write a function to convert your data-type into your trait-type. If you need some data-type specific logic, then pass around functions as necessary (usually from your conversion function). - ([View Highlight](https://read.readwise.io/read/01j6ccz00s0t2aydshas3xw962))