Skip to content
Prev Previous commit
Next Next commit
Formatting
  • Loading branch information
buenaflor committed Nov 26, 2025
commit 855d9f8fdf43a6c30b11cddcdadb13d4a74e225e
Original file line number Diff line number Diff line change
Expand Up @@ -72,103 +72,3 @@ class SentryItemTypeSerializationTest {
return SentryItemType.Deserializer().deserialize(reader, fixture.logger)
}
}



## 1. **Sentinel Object Pattern**

```dart
// Create a private sentinel marker
const _undefined = Object();

static Span startSpan(String name, {
Map<String, SentryAttribute>? attributes,
Object? parentSpan = _undefined, // Uses Object? to accept Span?, null, or undefined
bool active = true,
}) {
final Span? actualParentSpan;
if (identical(parentSpan, _undefined)) {
// Not provided - use default behavior (e.g., get from current scope)
actualParentSpan = _getCurrentSpan();
} else {
// Explicitly provided (could be null or a Span)
actualParentSpan = parentSpan as Span?;
}
// ...
}
```

## 2. **Wrapper Class (Optional/Maybe)**

```dart
class Optional<T> {
final T? value;
final bool isSet;

const Optional.absent() : value = null, isSet = false;
const Optional.of(this.value) : isSet = true;
}

static Span startSpan(String name, {
Optional<Span> parentSpan = const Optional.absent(),
}) {
if (parentSpan.isSet) {
// Explicitly provided (value could be null or a Span)
} else {
// Not provided
}
}
```

## 3. **Sealed Classes (Dart 3+)** — Most Type-Safe

```dart
sealed class ParentSpanOption {}
class UseCurrentSpan extends ParentSpanOption {} // undefined - use default
class ExplicitSpan extends ParentSpanOption {
final Span? span; // can be null or a span
ExplicitSpan(this.span);
}

static Span startSpan(String name, {
ParentSpanOption parentSpan = const UseCurrentSpan(),
}) {
switch (parentSpan) {
case UseCurrentSpan():
// Not provided - use default behavior
case ExplicitSpan(span: final span):
// Explicitly provided (span or null)
}
}
```

## 4. **Separate Boolean Flag**

```dart
static Span startSpan(String name, {
Span? parentSpan,
bool parentSpanProvided = false, // true means parentSpan was explicitly set
bool active = true,
}) {
if (parentSpanProvided) {
// Use parentSpan (even if null)
} else {
// Not provided - use default
}
}
```

## 5. **Record Type (Dart 3+)**

```dart
static Span startSpan(String name, {
(bool isSet, Span? value)? parentSpan, // null = undefined, (true, x) = explicit
}) {
if (parentSpan == null) {
// undefined
} else {
final (_, span) = parentSpan;
// explicitly set to `span` (which could be null)
}
}
```
Loading