sdk
sdk copied to clipboard
Idea: print('a', 'b', 'c') without varargs
What if we changed print
to work like Object.hash
?
Instead of
/// Prints a string representation of the object to the console.
void print(Object? object) {
String line = "$object";
var toZone = printToZone;
if (toZone == null) {
printToConsole(line);
} else {
toZone(line);
}
}
we could have
static void print(Object? object1, Object? object2,
[Object? object3 = sentinelValue,
Object? object4 = sentinelValue,
Object? object5 = sentinelValue,
Object? object6 = sentinelValue,
Object? object7 = sentinelValue,
Object? object8 = sentinelValue,
Object? object9 = sentinelValue,
Object? object10 = sentinelValue,
Object? object11 = sentinelValue,
Object? object12 = sentinelValue,
Object? object13 = sentinelValue,
Object? object14 = sentinelValue,
Object? object15 = sentinelValue,
Object? object16 = sentinelValue,
Object? object17 = sentinelValue,
Object? object18 = sentinelValue,
Object? object19 = sentinelValue,
Object? object20 = sentinelValue]) {
if (sentinelValue == object3) {
return printWeAlreadyKnow('$object1 $object2 $object3');
}
if (sentinelValue == object4) {
return printWeAlreadyKnow('$object1 $object2 $object3 $object4');
}
...
}
cc @lrhn
This will be a breaking change if it requires a change to Zone.print
, so it will have to convert the inputs to a string immediately, as shown here. That means not logging them separately to the console on the web.
I don't think it's necessary. You can already do print("${e1} ${e2} ${e3}")
instead of print(e1, e2, e3)
. Not a big difference, you get to control the spacing, and it's less overhead when you don't use the extra parameters.
(And you can just copy write the function above if you really need it).
I don't think it will, we could just modify String line = "$object";
to account for multiple objects. I know about print("${e1} ${e2} ${e3}")
or [e1, e2, e3].forEach(print)
, but I still think that could be useful, many languages support it and every week or so I type print('hello', e1)
then get an error because I could do that in TypeScript/Swift/etc but not in Dart. I think it would be a really small change that would help a lot of people (like myself). I usually start writing a string, then want the variable, need the $ at the beginning, then I want a field from the variable, need to go back and add { } at start and end, while just doing print('hello', e3) would do it for me.
it's less overhead when you don't use the extra parameters
I agree, but Dart supports 20 parameters in hash that is used everywhere, I think print would be fine, it is usually not even recommended to go to production code. It would be an additional if for most of the cases (no second variable, stop here).
I don't see the point of this, for hash that you are comparing to it is not possible to do anything similar to string interpolation, and this syntax gives no extra value to the user over string interpolation?
i like use print([a,b,c,d])
: ) , but you idea is good .
My dream:
void print2(
[Object? arg1,
Object? arg2,
Object? arg3,
Object? arg4,
Object? arg5,
Object? arg6,
Object? arg7,
Object? arg8,
Object? arg9,
Object? arg10,
Object? arg11,
Object? arg12,
Object? arg13,
Object? arg14,
Object? arg15,
Object? arg16,
Object? arg17,
Object? arg18,
Object? arg19,
Object? arg20]) {
// Helper method to convert each object to a string.
String objectToString(Object? object) => object?.toString() ?? 'null';
// Combine the string representations of all objects separated by spaces.
String combinedString = [
arg1,
arg2,
arg3,
arg4,
arg5,
arg6,
arg7,
arg8,
arg9,
arg10,
arg11,
arg12,
arg13,
arg14,
arg15,
arg16,
arg17,
arg18,
arg19,
arg20
].where((obj) => obj != null).map(objectToString).join(' ');
// Call the original print function.
print(combinedString);
}
You can just use that dream code yourself, now that you already have it.
(But you might consider whether you want print(null);
to print the empty string instead of "null". Can make debugging harder. The objectToString
does precisely the same as just calling .toString()
on the object, but you removed all the null
s before even calling it.)
You are right, probably the sentinel idea will work better.
I can use, but I can't replace print, so it is one more thing to remember :(
Another one found a way different way to do this. Still feels hacky, but you can see people clearly miss this feature: https://x.com/rodydavis/status/1757916193414058024?s=20