https://github.com/w2sv/kotlin-delegates
Some rather experimental kotlin delegates.
https://github.com/w2sv/kotlin-delegates
delegates kotlin kotlin-library
Last synced: 9 months ago
JSON representation
Some rather experimental kotlin delegates.
- Host: GitHub
- URL: https://github.com/w2sv/kotlin-delegates
- Owner: w2sv
- Created: 2024-02-03T12:29:03.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2024-08-18T14:01:18.000Z (over 1 year ago)
- Last Synced: 2025-03-23T18:54:18.834Z (10 months ago)
- Topics: delegates, kotlin, kotlin-library
- Language: Kotlin
- Homepage:
- Size: 55.7 KB
- Stars: 3
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Kotlin-Delegates
Just some rather experimental custom kotlin delegates. Use with caution.
## Usage Examples
### AutoSwitch
`AutoSwitch` is a delegate that automatically inverts its value upon calling its getter if the current value is equal to the specified switch condition.
```kotlin
// Create an AutoSwitch delegate with an initial value of false and switch condition true
var flag by AutoSwitch(value = false, switchOn = true)
// Access the value, automatically inverting it if it equals the switch condition
println(flag) // Output: false
println(flag) // Output: true
```
### AutoSwitch.Mapped
`AutoSwitch.Mapped` combines the functionality of `AutoSwitch` with that of a map delegate, allowing you to toggle boolean values stored in a mutable map.
```kotlin
// Create a mutable map to store boolean values
val booleanMap = mutableMapOf("isEnabled" to false, "isActivated" to true)
// Create an AutoSwitch.Mapped delegate using the map and a switch condition
val isEnabled by AutoSwitch.Mapped(booleanMap, switchOn = true)
// Access the value, automatically toggling it in the map
println(isEnabled) // Output: false
println(isEnabled) // Output: true
// The boolean value in the map has been toggled
println(booleanMap["isEnabled"]) // Output: true
```
### Consumable
`Consumable` is a delegate for objects that should be retrieved exactly once. After getting its value, it will be automatically set to null.
```kotlin
// Create a Consumable delegate with an initial value
val data by Consumable("Hello, World!")
// Access the value, consuming it and setting it to null
println(data) // Output: Hello, World!
println(data) // Output: null
// Check if the value has been consumed
println(data.isConsumable) // Output: false
```
### MapObservable
`MapObservable` is a delegate that combines the functionality of a map delegate with that of an observable. It notifies a callback function when a property's value changes.
```kotlin
// Create a mutable map to store values
val dataMap = mutableMapOf("counter" to 0)
// Create a MapObservable delegate with a callback function
val counter by MapObservable(dataMap) { property, oldValue, newValue ->
println("${property.name} changed from $oldValue to $newValue")
}
// Access the value, triggering the callback function
counter = 42
// Output: counter changed from 0 to 42
```