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

Add documentation for enum predicate methods #816

Merged
merged 8 commits into from
Feb 6, 2025
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 43 additions & 6 deletions docs/syntax_and_semantics/enum.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,23 +97,60 @@ puts Color.new(10) # => prints "10"

This method is mainly intended to convert integers from C to enums in Crystal.

## Methods
## Question methods

Just like a class or a struct, you can define methods for enums:
An enum automatically defines question methods for each member, using
`String#underscore` for the method name.

!!! note
In the case of regular enums, this compares by equality (`==`). In the case of flags enums, this invokes `includes?`.

For example:

```crystal
enum Color
Red
Green
Blue
end

color = Color::Blue
color.red? # => false
color.blue? # => true

@[Flags]
enum IOMode
Read
Write
Async
end

def red?
self == Color::Red
mode = IOMode::Read | IOMode::Async
mode.read? # => true
mode.write? # => false
mode.async? # => true
```

## Methods

Just like a class or a struct, you can define methods for enums:

```crystal
enum ButtonSize
Sm
Md
Lg

def label
case self
in .sm? then "small"
in .md? then "medium"
in .lg? then "large"
end
end

Color::Red.red? # => true
Color::Blue.red? # => false
ButtonSize::Sm.label # => "small"
ButtonSize::Lg.label # => "large"
```

Class variables are allowed, but instance variables are not.
Expand Down
8 changes: 4 additions & 4 deletions docs/syntax_and_semantics/platform_support.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ and drop into *Tier 2*.
| ------ | ----------- | ------------------ | ------- |
| `aarch64-darwin` | Aarch64 macOS<br> (Apple Silicon) | 11+ *(testing only on 14)* | :material-checkbox-marked-circle: tests<br> :material-checkbox-marked-circle: builds |
| `x86_64-darwin` | x64 macOS<br> (Intel) | 11+<br> *(testing only on 13; expected to work on 10.7+)* | :material-checkbox-marked-circle: tests<br> :material-checkbox-marked-circle: builds |
| `x86_64-linux-gnu` | x64 Linux | kernel 4.14+, GNU libc 2.26+<br> *(expected to work on kernel 2.6.18+)* | :material-checkbox-marked-circle: tests<br> :material-checkbox-marked-circle: builds |
| `x86_64-linux-musl` | x64 Linux | kernel 4.14+, MUSL libc 1.2+<br> *(expected to work on kernel 2.6.18+)* | :material-checkbox-marked-circle: tests<br> :material-checkbox-marked-circle: builds |
| `x86_64-linux-gnu` | x64 Linux | kernel 4.14+, GNU libc 2.26+<br> *(expected to work on kernel 2.6.22+)* | :material-checkbox-marked-circle: tests<br> :material-checkbox-marked-circle: builds |
| `x86_64-linux-musl` | x64 Linux | kernel 4.14+, MUSL libc 1.2+<br> *(expected to work on kernel 2.6.22+)* | :material-checkbox-marked-circle: tests<br> :material-checkbox-marked-circle: builds |

***

Expand All @@ -38,8 +38,8 @@ Details are described in the *Comment* column.
| `aarch64-linux-gnu` | Aarch64 Linux | GNU libc 2.26+ | :material-checkbox-marked-circle: tests<br> :material-selection-ellipse: builds |
| `aarch64-linux-musl` | Aarch64 Linux | MUSL libc 1.2+ | :material-checkbox-marked-circle: tests<br> :material-selection-ellipse: builds |
| `arm-linux-gnueabihf` | Aarch32 Linux<br> (hardfloat) | GNU libc 2.26+ | :material-selection-ellipse: tests<br> :material-selection-ellipse: builds |
| `i386-linux-gnu` | x86 Linux | kernel 4.14+, GNU libc 2.26+<br> *(expected to work on kernel 2.6.18+)* | :material-selection-ellipse: tests<br> :material-selection-ellipse: builds |
| `i386-linux-musl` | x86 Linux | kernel 4.14+, MUSL libc 1.2+<br> *(expected to work on kernel 2.6.18+)* | :material-selection-ellipse: tests<br> :material-selection-ellipse: builds |
| `i386-linux-gnu` | x86 Linux | kernel 4.14+, GNU libc 2.26+<br> *(expected to work on kernel 2.6.22+)* | :material-selection-ellipse: tests<br> :material-selection-ellipse: builds |
| `i386-linux-musl` | x86 Linux | kernel 4.14+, MUSL libc 1.2+<br> *(expected to work on kernel 2.6.22+)* | :material-selection-ellipse: tests<br> :material-selection-ellipse: builds |
| `x86_64-openbsd` | x64 OpenBSD | 6+ | :material-selection-ellipse: tests<br> :material-selection-ellipse: builds |
| `x86_64-freebsd` | x64 FreeBSD | 12+ | :material-selection-ellipse: tests<br> :material-selection-ellipse: builds |

Expand Down