-
Notifications
You must be signed in to change notification settings - Fork 152
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
Construction from scalar arguments vs. aggregate objects #41
Comments
Because then it would be possible to write something like: let t = new CivilTime({ minute:30 }); // which hour?
let d = new CivilDate({ day: 31 }); // which year? which month? The only way to deal with this would be to assume some default values - which IMHO wouldn't be obvious or intuitive. |
As to the options bag for the math functions, #23 has the discussion where we agreed on that approach. Thanks. |
@mj1856 Well, what about throwing an exception for cases like that? |
I suppose that would be possible, though I'm not sure it is necessary. There is case to be made for user familiarity with the Each approach has its pros and cons. I'm curious what others think. Do you prefer: var dt = new CivilDateTime(2017, 12, 31, 23, 59, 59, 999, 999999); or var dt = new CivilDateTime({
year: 2017,
month: 12,
day: 31,
hour: 23,
minute: 59,
second: 59,
millisecond: 999,
nanosecond: 999999
}); |
(I raised the issue out of an aesthetic preference for the second one, though I'm happy to defer to others here) |
I certainly prefer the normal arguments rather than an options bag. The example above shows how verbose this can be. You'll also always be omitting from one units and lesser if you see what I mean; there is never a situation where someone would want to specify hours and seconds but not minutes, and if you did I'd say it's bad practice to do so. |
I think normal arguments make more sense when the arguments are not optional. |
+1 - options objects are great for non-required args, but for required args, there's not really much of a benefit in using options objects imo. |
I tend to agree that options doesn't make sense when fields are required. |
I tend to agree with positional here, but a small counterpoint is that it's a bit worse for programmatic use. If you're like me, you end up doing stuff like this a lot: // This is the most naturally way to keep this data around
// or of pulling off a wire
var someTimeData = {
year: 2017,
month: 12,
day: 31,
hour: 23,
minute: 59,
second: 59,
millisecond: 999,
nanosecond: 999999
};
// grr, ok, at least I have ES6, imagine this without it
{year, month, day, hour, minute, second, millisecond, nanosecond} = someTimeData;
// now I can do the thing
new CivilDateTime(year, month, day, hour, minute, second, millisecond, nanosecond);
// So it more naturally takes an array, which seems definitely worse
someTimeData = [
2017,
12,
31,
23,
59,
59,
999,
999999
];
// And then you figure out how to apply() but with a constructor,
// which is something weird like
new (Function.prototype.bind.apply(CivilDateTime, someTimeData)); Anyway, I don't think that trumps how positional parameters naturally enforce requiredness, but it's a thought. |
A static const likeADate = {
year: 2017,
month: 12,
day: 31,
hour: 23,
minute: 59,
second: 59,
millisecond: 999,
nanosecond: 999999
};
const dt = CivilDateTime.from(likeADate); Something to think about in the future. Similar to
I wouldn't personally, but there could be situations where an object might have some parts of a time. class Something {
constructor(){
this.year = 2017;
this.month = 11;
}
get dateTime(){
return CivilDateTime.from(this);
//return new CivilDateTime(this);
}
} |
I like that the constructors define scalar parameters of decreasing resolution, but we could consider (re)introducing static |
Seems like we've settled on positional arguments. Do we want to add this |
|
Imo any function taking more than a certain amount of parameters of the same type becomes hard to read and error-prone if it takes those as positional parameters instead of an options bag. Especially in the case of Duration, it makes it unneccessarily hard to create durations of milliseconds, seconds, minutes, hours, days etc because you always need to specify everything starting with years, and what semantic the last passed parameter has is hard to infer when reading the code: new Temporal.Duration(0,0,0,0,10) // how long is this?
// vs
new Temporal.Duration({ minutes: 10 }) And this is only minutes, now imagine this when working with mili- or nanoseconds. See also @mj1856's example. Imo this is something that is bad about the current |
This is obsolete. Conclusion:
|
In this proposal, constructors take positional parameters, but methods like
add
take an options bag with named parameters. I was wondering why this option was chosen. Why do constructors not also take such an options bag?The text was updated successfully, but these errors were encountered: