How do I extract values from a class? #1370
-
|
Good evening! After progressing further along in working on a schema, I hit a small snag; I have an enum of strings that contain some IDs for conditions, something like this: typealias TargettedConditionEnum =
"CHECK_TARGET_STATUS" | // Checks the list of statuses on a target
"CHECK_RANGE_TO_TARGET" | // How far is the target from the caster's origin?
"CHECK_TARGET_HEALTH_PERCENT" // How healthy is the target?
// ...
abstract class BaseCondition {
invert: Boolean = false // Inverts the condition, default is false.
params: Any
}
abstract class CheckCondition extends BaseCondition {
conditionID: regex_patterns.CheckID
}
class RangeParams {
comparison: enums.NumericComparison(this != "EqualTo") // Checking exact distances is difficult
value: Number(isPositive)
}
class RangeCondition extends CheckCondition {
conditionID: "CHECK_RANGE_TO_TARGET" = "CHECK_RANGE_TO_TARGET"
params: RangeParams
}What I would like to is effectively make the typealias TargettedConditionEnum =
RangeCondition.conditionID |
StatusCondition.conditionID |
// ... etc
// Or, from a functional approach
typealias TargettedConditionEnum =
GetVariable<String>(RangeCondition, "conditionID") |
GetVariable<String>(StatusCondition, "conditionID") |
// ... etcI'm still fairly inexperienced with Pkl, so I'm not sure if this is possible. If it is, then I would greatly appreciate knowing how to do such a thing. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
|
Pkl doesn't support this today. One way to DRY up your code is to create a typealias that you share in both locations, e.g. typealias RangeConditionId = "CHECK_RANGE_TO_TARGET"
typealias TargetStatusId = "CHECK_TARGET_STATUS"
typealias TargetedConditionEnum = RangeConditionId | TargetStatusId
class RangeCondition {
conditionID: RangeConditionId
}
class StatusCondition {
conditionID: TargetStatusId
}The type of feature you're looking for (e.g. Scala's type projection, TypeScript's |
Beta Was this translation helpful? Give feedback.
Pkl doesn't support this today. One way to DRY up your code is to create a typealias that you share in both locations, e.g.
The type of feature you're looking for (e.g. Scala's type projection, TypeScript's
Pick) is quite useful, so, perhaps we can add it to Pkl some day. But right now it's quite unclear when something like this would land.