4 ms·
Good examples: 2.days().from_now() (Rust) vs. 2.days.from_now (Ruby) I'd probably still prefer Smalltalk: 2 days fromNow. It seems to me tha
by mpweiher 13y ago
Good examples:
2.days().from_now() (Rust)
vs.
2.days.from_now (Ruby)
I'd probably still prefer Smalltalk:
2 days fromNow.
It seems to me that many distinctions we still suffer from are quite arbitrary. Yes, having numbers be objects (boxed or tagged) is one way of achieving this sort of uniform handling, but you can also achieve it statically.
Just like you don't have to switch to a "scripting" language for expressive file manipulation:
file:hello.world := 'Hello World!'.
EDIT: typo.
- steveklabnik 13y ago> It seems to me that many distinctions we still suffer from are quite arbitrary. Agreed. And it's nice to see newer languages which bring these kinds of theoretical improvements into practical languages we as developers can actually use, rather than just as papers. I don't want to come off as anti-theory, I love theory. It's just nice to see it finally trickling down.
- mpweiher 13y agoYes, indeed. My last example is from Objective-Smalltalk [1], which straddles academia [2] and practicality [3]. The basic idea is that you really need both the practical application to show the problems and then the time to think about solutions. [1] http://objective.st http://objective.st [2] http://dl.acm.org/citation.cfm?doid=2508168.2508169 http://dl.acm.org/citation.cfm?doid=2508168.2508169, https://www.hpi.uni-potsdam.de/hirschfeld/publications/media/WeiherHirschfeld_2013_PolymorphicIdentifiersUniformResourceAccessInObjectiveSmalltalk_AcmDL.pdf https://www.hpi.uni-potsdam.de/hirschfeld/publications/media... [3] http://metaobject.com http://metaobject.com (used in most products) EDIT: added sentence about approach.
- MichaelGG 13y agoI guess we'd call these "type extensions"? F# supports this just fine, and C# can do a bit, too. Overall, I haven't found I need type extensions that much, if I have flexible function handling. It's the same level of expressiveness to write "fromNow 2 days". (fromNow having signature (int -> (int -> TimeSpan) -> DateTime). Or even "2 |> days |> fromNow" (fromNow being TimeSpan -> DateTime). Actually, this example is pretty terrible, as even C#'s basic format would be "DateTime.Now.AddDays(2)" which seems pretty fine to me - add a top-level binding for now and it's on the same level of conciseness. Also, adding type extensions to integers for time seems quite ugly. Most 3rd party libraries I've seen don't use type extensions in a graceful manner, and end up slapping members on everything and it's just silly. (And I'm ignoring the fact that using .Now in .NET is rarely correct since it's rare when you don't want to use whatever random timezone the server's configured for, instead of UTC.)
- bad_user 13y ago"2.days.from_now" is different from DateTime.Now.AddDays(2), because it can be broken in two parts: - 2.days is a duration, or a time delta, or time span, or whatever you want to call it - from_now is a method called on this duration value This construct "2.days" is useful on its own. It's actually more useful than "DateTime.Now", as durations are often part of public interfaces, whereas usage of concrete timestamps is very often hidden. The equivalent in C# for it would be "new TimeSpan(2, 0, 0, 0)". That's much more unreadable than just "2.days" and often, instead of thinking about the problem, developers fallback to specifying time spans as Ints in seconds or similar. On TimeZone, "2.days.from_now" is a timestamp. If the underlying library is not totally fucked up, like "java.util.Date" is, then it does have an attached timezone. Or in case this yields a Unix timestamp, it's really not a Unix timestamp unless it was calculated since 1970-01-01 00:00:00 UTC.
- viblo 13y agoI agree that the constructor isn't very easy to read for TimeSpans. But there's a shorthand, you can write "TimeSpan.FromDays(2)" to create a span of 2 days.
- MichaelGG 13y agoYou wouldn't write new TimeSpan(2,0,0,0), you'd write TimeSpan.FromDays(2). There's nothing stopping you from binding a function "days" to wrap TimeSpan.FromDays. If this is what people consider to be some pinnacle or notable point in expressiveness, yikes.
- bad_user 13y agoOh, but we can expand on that ... scala> 2.days + 10.minutes res: FiniteDuration = 2890 minutes I just don't get it. So a literal like "0.2f" to denote a Float (and not a Long) is acceptable. A literal like "300.5m" to denote a decimal and not a floating-point is acceptable. But somehow other kinds of literals for other units of measurement are not, because Dear God, magic, magic!!!
- riffraff 13y agoscala also allows, AFAIK, 2 days fromNow with the additional advantage compared to ruby/smalltalk that "days" is only available for ints in places where you import the extensions.