- From: Denis Migdal <notifications@github.com>
- Date: Mon, 09 Dec 2024 02:18:08 -0800
- To: whatwg/fetch <fetch@noreply.github.com>
- Cc: Subscribed <subscribed@noreply.github.com>
- Message-ID: <whatwg/fetch/issues/1792/2527500352@github.com>
> I'm not sure how `undefined` would change things here, although I agree that we should probably have used that. But we'd have the same mismatch as `undefined` and the empty string are not the same. Setting an header as `undefined` would mean it should be set as "not being defined", i.e. should be removed from the headers. But that's an issue of JS itself (so we can't fix it), as `a.foo = undefined` is different from `delete a.foo`, which sometimes causes some strange behaviors... > But also, maybe `set`/`get` symmetry is not that important? If you have a pointer I'd love to read more on the arguments in favor. Can't find a source, I think "symmetry" isn't the truly correct term as we can do : ```js a.set(name, x); a.get(name) !== x; // indeed, we can have some cast in the mutator. ``` Still, I'll continue to use this term below. To make a quick argument in favor: Intuitively, you expect to be able to do : ```js a.set("foo", b.get("foo") ); a.get("foo") === b.get("foo"); ``` Having get/set symmetry enables the API to be less counter-intuitive, and to prevent bugs. If you break this symmetry, you have to warn the dev: In strong typed language, you can do : ```ts function setter(a: string) {} function getter(): string|null {} setter(getter()); // raises an error during building phase. ``` If you do it in JS, you may raise an exception when you receive `null`, but that'd only occurs at runtime, which would likely become a bug. Alternatively, you may do : ```ts function setter(a) {} function getter(default_value) {} setter(getter("x")); ``` Where it is clear that the developer breaks symmetry as he provides a default value. Using it without default value would indicate the dev used it while being sure it has a value, therefore if the value hasn't been defined, it'd raises an error. But such behavior need be consistent (so not really possible in JS now). Though it may be possible to do: ```ts function getter(default_value) {} // if not given (i.e. args.length == 0), behave as currently. getter(RaiseError); // if default_value is RaiseError, then an exception is raised if the value isn't defined. // or function getter(default_value, raise_error_if_not_defined = false) {} getter(null,true); // meh ``` But that's not ideal. For JS, you could have have something like: ```js src.copyTo(dst, "foo", "faa"); // copy "foo" and "faa" (if defined, ofc) src.copyTo(dst, (name) => true); // with a filter. src.copyAllTo(dst, "foo", "faa"); // copy all except for "foo" and "faa". // dst.copyFrom(src, ...) also possible. // src.copy(...) to build a new instance, also possible. ``` But it'd be better if it was a generic helper function exploiting `.entries()`: ```js function copyTo(src, dst, filter) {} function copy(a, filter) {} // build a new instance. ``` So that's my 2 cents. -- Reply to this email directly or view it on GitHub: https://github.com/whatwg/fetch/issues/1792#issuecomment-2527500352 You are receiving this because you are subscribed to this thread. Message ID: <whatwg/fetch/issues/1792/2527500352@github.com>
Received on Monday, 9 December 2024 10:18:12 UTC