get method
Accesses a property or method of this value.
Implementation
@override
Object? get(String identifier) {
// Access standard enum properties
switch (identifier) {
case 'index':
return index;
case 'name':
return name;
// RC-4: Object-level properties that all objects support
case 'hashCode':
return nativeValue.hashCode;
case 'runtimeType':
return enumType;
default:
// 1. Check for custom instance getters
final getterAdapter =
_getters[identifier] ?? enumType.getters[identifier];
if (getterAdapter != null) {
try {
// Note: The visitor is null here because get() has no visitor context.
// If the adapter needs the visitor, the interface will need to be revised.
return getterAdapter(null, nativeValue);
} catch (e) {
throw RuntimeD4rtException(
'Error executing bridged getter "$identifier" on ${enumType.name}.$name: $e');
}
}
// 2. Check for instance methods (for cases like .toString() called as a getter)
// Or to return a callable function representing the method.
final methodAdapter =
_methods[identifier] ?? enumType.methods[identifier];
if (methodAdapter != null) {
// Return a function that wraps the bridged method call
// (or handle directly if it's a getter disguised as a method, like toString)
if (identifier == 'toString') {
// Special case for toString() via get
// We could call the toString adapter if it exists, otherwise fallback
try {
// Try to call the toString adapter (which should not need args)
final env = Environment();
return methodAdapter(
InterpreterVisitor(
globalEnvironment: env,
moduleContext: NoOpModuleContext(globalEnvironment: env)),
nativeValue,
[],
{},
null);
} catch (_) {
// Fallback if the adapter does not exist or fails
return '${enumType.name}.$name';
}
}
// For other methods, we could return a callable function here if needed.
// For now, throw an error if trying to access a method via get.
throw RuntimeD4rtException(
'Cannot access method "$identifier" as a property on enum value ${enumType.name}.$name. Use call syntax ().');
}
// 3. If it's neither a standard property, nor a custom getter/method
throw RuntimeD4rtException(
'Property "$identifier" not found on enum value ${enumType.name}.$name');
}
}