Comments (17)
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.
from proposal-temporal.
I think normal arguments make more sense when the arguments are not optional.
from proposal-temporal.
@mj1856 Well, what about throwing an exception for cases like that?
from proposal-temporal.
I tend to agree that options doesn't make sense when fields are required.
from proposal-temporal.
I like that the constructors define scalar parameters of decreasing resolution, but we could consider (re)introducing static fromObject
methods that define a single parameter and read fields from the corresponding argument, giving equal treatment to Temporal type instances and plain objects.
from proposal-temporal.
As to the options bag for the math functions, #23 has the discussion where we agreed on that approach. Thanks.
from proposal-temporal.
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 Date
object constructor. Ours would be similar, except for the months field switching from being 0-based to 1-based. Would that be reason enough to switch?
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
});
from proposal-temporal.
(I raised the issue out of an aesthetic preference for the second one, though I'm happy to defer to others here)
from proposal-temporal.
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.
from proposal-temporal.
+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.
from proposal-temporal.
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.
from proposal-temporal.
@icambron Would you really store/transfer instants as an object like this? I'd use an ISO-8601 serialization.
from proposal-temporal.
A static .from()
operator might be a good compromise.
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 Promise.resolve()
, or Array.from()
.
Would you really store/transfer instants as an object like this?
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);
}
}
from proposal-temporal.
Seems like we've settled on positional arguments. Do we want to add this fromObject
method (which sounds like a good idea to me) or not?
from proposal-temporal.
fromObject
seem redundant. just from
would be better.
from proposal-temporal.
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 Date
API (old and Java-like, not JS-idiomatic). The new Intl, DateTimeFormat and Temporal methods APIs are all way more ergonomic to use and read. Please consider using options objects for the constructors too.
from proposal-temporal.
This is obsolete. Conclusion:
- constructors has positional arguments only
- static
.from
methods take property bags to create objects
from proposal-temporal.
Related Issues (20)
- does `ZonedDateTime.from` with `offset: reject` always throw when the datetime is in a DST transition gap? if so, is that intended?
- NudgeToCalendarUnit: Wrong roundedSign for (roundedUnit - total == 0)? HOT 1
- NudgeToDayOrTime: Incorrect assertion in step 1
- Polyfill: Buddhist, Ethiopic-Amete-Alem, Indian, Islamic, and Persian calendar return non-undefind era HOT 3
- CalendarFieldDescriptors: Require eraYear to be alway positive? HOT 8
- Polyfill: Allow alternate era codes
- RoundRelativeDuration: Disallow edge case in roundingIncrement within year/month HOT 5
- SystemDateTime, SystemZonedDateTime: "calendarLike" argument is always constant string "iso8601"
- Polyfill : undefined weekOfYear and yearOfWeek with calendarId = "islamic-civil" HOT 3
- Move AvailableCalendars from 402 into 262
- Implement calendar canonicalization to match 402
- ZonedDateTime.from() / .with() overflow option default HOT 1
- ZonedDateTime.startOfDay: is it guaranteed to be the first instant in a calendar day? HOT 17
- background on why there is only one Duration type? HOT 5
- ZonedDateTime.withPlainTime() Documentation HOT 1
- Duration rounding throws an error HOT 9
- GetTemporalOverflowOption(options) looks redundant in 9.2.2 HOT 1
- calendarName to showCalendar rename? HOT 3
- GetRoundingIncrementOption: Call ToIntegerWithTruncation?
- Remaining editorial items on TimeZone and Calendar removals HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from proposal-temporal.