2024-12-16 01:34:28 +00:00
# Dart Mirrors vs Platform Reflection Comparison
2024-12-21 18:01:22 +00:00
This document compares our Platform Reflection library with Dart's built-in `dart:mirrors` package, highlighting key differences, trade-offs, and migration paths.
2024-12-16 01:34:28 +00:00
2024-12-21 18:01:22 +00:00
## Core Features Comparison
2024-12-16 01:34:28 +00:00
| Feature | dart:mirrors | Platform Reflection | Notes |
2024-12-21 18:01:22 +00:00
|---------|--------------|---------------------|-------|
| Class Reflection | ✅ Full | ✅ Full | Requires registration in Platform Reflection |
| Method Invocation | ✅ Full | ✅ Full | Complete parity |
| Property Access | ✅ Full | ✅ Full | Complete parity |
| Constructor Invocation | ✅ Full | ✅ Full | Requires registration in Platform Reflection |
| Type Information | ✅ Full | ✅ Full | Complete parity |
| Metadata | ✅ Full | ⚠️ Limited | Basic metadata support in Platform Reflection |
| Generic Types | ✅ Full | ⚠️ Limited | Basic generic support in Platform Reflection |
| AOT Compilation | ❌ No | ✅ Yes | Platform Reflection supports AOT compilation |
## Key Implementation Differences
2024-12-16 01:34:28 +00:00
### Registration System
```dart
// dart:mirrors
// No registration needed
@reflectable
class MyClass {}
// Platform Reflection
@reflectable
class MyClass {}
// Requires explicit registration
Reflector.register(MyClass);
Reflector.registerProperty(MyClass, 'prop', String);
Reflector.registerMethod(MyClass, 'method', [int]);
2024-12-21 18:01:22 +00:00
Reflector.registerConstructor(MyClass, '', parameterTypes: []);
2024-12-16 01:34:28 +00:00
```
2024-12-21 18:01:22 +00:00
### Reflection Usage
2024-12-16 01:34:28 +00:00
```dart
// dart:mirrors
2024-12-21 18:01:22 +00:00
import 'dart:mirrors';
2024-12-16 01:34:28 +00:00
2024-12-21 18:01:22 +00:00
final instance = MyClass();
final mirror = reflect(instance);
final value = mirror.getField(#prop).reflectee;
mirror.invoke(#method, [42]);
2024-12-16 01:34:28 +00:00
// Platform Reflection
2024-12-25 21:31:41 +00:00
import 'package:platform_mirrors/mirrors.dart';
2024-12-16 01:34:28 +00:00
2024-12-21 18:01:22 +00:00
final reflector = RuntimeReflector.instance;
final instance = reflector.createInstance(MyClass, positionalArgs: []) as MyClass;
final mirror = reflector.reflect(instance);
final value = mirror.getField(const Symbol('prop')).reflectee;
mirror.invoke(const Symbol('method'), [42]);
2024-12-16 01:34:28 +00:00
```
## Performance Characteristics
| Aspect | dart:mirrors | Platform Reflection | Winner |
2024-12-21 18:01:22 +00:00
|--------|--------------|---------------------|--------|
2024-12-16 01:34:28 +00:00
| Startup time | ❌ Slower | ✅ Faster | Platform Reflection |
| Runtime performance | ❌ Slower | ✅ Faster | Platform Reflection |
| Memory usage | ❌ Higher | ✅ Lower | Platform Reflection |
| Tree shaking | ❌ Poor | ✅ Good | Platform Reflection |
## Platform Support
| Platform | dart:mirrors | Platform Reflection | Winner |
2024-12-21 18:01:22 +00:00
|----------|--------------|---------------------|--------|
2024-12-16 01:34:28 +00:00
| VM | ✅ Yes | ✅ Yes | Tie |
| Web | ❌ No | ✅ Yes | Platform Reflection |
| Flutter | ❌ No | ✅ Yes | Platform Reflection |
| AOT | ❌ No | ✅ Yes | Platform Reflection |
## Use Cases
| Use Case | dart:mirrors | Platform Reflection | Better Choice |
2024-12-21 18:01:22 +00:00
|----------|--------------|---------------------|---------------|
| Dependency injection | ✅ Simpler | ⚠️ More setup | Depends on platform needs |
| Serialization | ✅ Simpler | ⚠️ More setup | Depends on platform needs |
2024-12-16 01:34:28 +00:00
| Testing/Mocking | ✅ More flexible | ✅ More controlled | Depends on needs |
| Production apps | ❌ Limited platforms | ✅ All platforms | Platform Reflection |
## Migration Path
2024-12-21 18:01:22 +00:00
To migrate from dart:mirrors to Platform Reflection:
1. Replace imports:
```dart
// Before
import 'dart:mirrors';
// After
2024-12-25 21:31:41 +00:00
import 'package:platform_mirrors/mirrors.dart';
2024-12-21 18:01:22 +00:00
```
2. Add registration for each reflectable class:
```dart
void registerReflectables() {
Reflector.register(MyClass);
Reflector.registerProperty(MyClass, 'prop', String);
Reflector.registerMethod(MyClass, 'method', [int]);
Reflector.registerConstructor(MyClass, '', parameterTypes: []);
}
```
3. Update reflection calls:
```dart
// Before
final mirror = reflect(instance);
final value = mirror.getField(#prop).reflectee;
// After
final reflector = RuntimeReflector.instance;
final mirror = reflector.reflect(instance);
final value = mirror.getField(const Symbol('prop')).reflectee;
```
4. Replace MirrorSystem usage:
```dart
// Before
final classMirror = reflectClass(MyClass);
// After
final classMirror = RuntimeReflector.instance.reflectClass(MyClass);
```
5. Update your build process to ensure registration functions are called at startup.
2024-12-16 01:34:28 +00:00
## Trade-offs
### Advantages of Platform Reflection
2024-12-21 18:01:22 +00:00
1. Works on all platforms (VM, Web, Flutter, AOT)
2. Better performance and smaller code size
3. Supports AOT compilation
4. More controlled reflection surface
2024-12-16 01:34:28 +00:00
### Advantages of dart:mirrors
1. No registration needed
2024-12-21 18:01:22 +00:00
2. More dynamic capabilities
3. Simpler API for some use cases
4. Better for certain development tools
2024-12-16 01:34:28 +00:00
## Conclusion
2024-12-21 18:01:22 +00:00
Platform Reflection offers a production-ready alternative to dart:mirrors with cross-platform support, better performance, and AOT compilation compatibility. The main trade-off is the need for explicit registration, which provides more control but requires more setup.
2024-12-16 01:34:28 +00:00
Choose Platform Reflection when:
2024-12-21 18:01:22 +00:00
- You need cross-platform or AOT compilation support
- Performance and code size are critical
- You want more control over the reflection surface
Consider dart:mirrors when:
- You're only targeting the Dart VM
- You need maximum runtime flexibility
- You're building certain types of development tools
Remember, the choice between Platform Reflection and dart:mirrors often comes down to your specific platform requirements and performance needs.