Skip to content

Commit 9909f53

Browse files
enabling easier access to Const inner value
1 parent a95bb43 commit 9909f53

2 files changed

Lines changed: 6 additions & 1 deletion

File tree

Changelog.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
66

77
## [Unreleased]
88

9+
### Added
10+
- `Base.get` and `Base.getindex` are now both implemented for `Const` (like they are implemented already for `Identity`), to simplify working with `Const`.
911

1012
### Fixed
1113
- `Option(3)` works now

src/Const.jl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,12 @@ returns true only if given an instance of [`DataTypesBasic.Const`](@ref)
2525
Base.isconst(::Const) = true
2626
Base.isconst(other) = false
2727

28+
# length 0 corresponds directly to Base.iterate
2829
Base.length(::Const) = 0
2930

30-
# as the length of Const is 0, we intentionally do not support `Base.get`
31+
# for convenience we support different access functions so that users do not have to remember the name of the single field "value"
32+
Base.get(c::Const) = c.value
33+
Base.getindex(c::Const) = c.value
3134

3235
# const just does nothing, i.e. leaves everything constant
3336
Base.iterate(c::Const) = nothing

0 commit comments

Comments
 (0)