-
Notifications
You must be signed in to change notification settings - Fork 13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow Object and Object?/dynamic is models, functions and exceptions #35
Comments
Hey @marcglasberg, great feedback and thanks for writing this up. My Discord comment probably should have said:
It's true that we could allow I do think there is a model which can satisfy all uses cases coming with extension types, which would allow an explicit opt-in to this behavior without any runtime overhead. For example, you could have the following: extension type JsonValue(Object? value) {}
class TranslatableUserException implements Exception {
factory TranslatableUserException(String message,
[Object? value1,
Object? value2,
Object? value3,
Object? value4,
Object? value5]) =>
TranslatableUserException._(message, JsonValue(value1), JsonValue(value2),
JsonValue(value3), JsonValue(value4), JsonValue(value5));
const TranslatableUserException._(this.message,
this.value1, this.value2, this.value3, this.value4, this.value5);
final String message;
final JsonValue value1, value2, value3, value4, value5;
} While this is more verbose, it provides a level of type-safety that cannot be guaranteed with Thoughts? |
This solution is great, I love it. I was just reading this file: https://github.com/celest-dev/celest/blob/main/packages/celest_core/test/json_value_test.dart which uses JsonValue
final class JsonObject extends JsonValue {
/// Creates a [JsonObject] from [wrapped].
const JsonObject(this.wrapped)
: _path = const [],
assert(
wrapped == null ||
wrapped is String ||
wrapped is int ||
wrapped is double ||
wrapped is bool ||
wrapped is List<Object?> ||
wrapped is Map<String, Object?>,
'Unsupported JSON value: $wrapped');
const JsonObject._(this.wrapped, [this._path = const []]);
@override
final Object? wrapped;
@override
final List<String> _path;
} |
Yes! My goal was to make And I'll make sure to add a constructor like that for easy usage 💯 |
You mean dart-lang/language#2727 ? I personally don't mind the overhead, though. |
Yes, that should be released in Dart 3.3 soon 👍 The overhead is really two-fold with classes vs. extension types
What I mean is: // With classes
class JsonString extends JsonValue {
const JsonString(String value): super(value);
}
const String s = 'abc';
const JsonString js = JsonString(s);
print(js == s); // false
print(js is String); // false
// With extension types
extension type const JsonString(String value) implements JsonValue {}
const String s = 'abc';
const JsonString js = JsonString(s);
print(js == s); // true
print(js is String); // true They are basically marker types which only exist at compile type, which means I can treat every It's doable, but since Dart 3.3 is right around the corner, I wanted to save myself the energy 😆 |
Perfect! |
@dnys1 Dart 3.3 is out! :) |
On it! 😄 |
The |
@dnys1 said (in Discord):
I already have a use case for this, as one of my exception types needs to have a few
Object?
fields:I'm not sure you do need to know all the types statically. Suppose your function is
doSomething(Object? value)
and you get one of these JSON values from the request:{ 123 }
or{ "abc" }
or{ true }
. Why can't you simply desserialize this asdoSomething(123)
,doSomething(true)
respectively?I understand you can't use any object there, but maybe allow for the common values accepted in JSON (plus Date?), and throw an error at runtime if an invalid value is used. In the future you can use an IDE plugin (#34) to show an error in the code editor if an invalid value type is directly used.
The text was updated successfully, but these errors were encountered: