199 points · by steveklabnik · 12y ago · 146 comments
Python is a beautiful, clean language. But the same restrictions that make it nice and clean mean that it’s hard to write beautiful, clean libraries. Ruby, on the other hand, is a complicated, ugly language. But that complexity allows you to write really clean, nice, easy-to-use libraries.
The Ruby metaprogramming magic makes for really nice syntax, which is what made me choose it over Python all those years ago, but it complicates understanding of what's going on - as I matured as an engineer, maintained a codebase over several years, several times spend long hours debugging issues coming from metaprogramming pitfalls, I shift more and more to the Python approach of building APIs in the simplest possible way, as much as possible sticking to simple function calls and no really fancy metaprogramming just to get a cleaner syntax, e.g. compare Rails has_many :foo with Django's models.ForeignKey(Foo) and the internal implementations of those. Same thing with using "BDD" testing frameworks like RSpec vs. traditional assert stuff, what is the nice syntax worth if tricky bugs hit you where you most want to avoid it, in tests. Libraries like RSpec might be "nice", but they are certainly not "easy-to-use", and I don't think I would call them quite "clean", if I look at their source code.
In other words, while clean syntax is luring, in the long run clean and simple semantics is what I think is really important.
Rubyists try too hard to emulate natural language syntax, but in the process forget that natural languages are not particularly clean - in fact, more often than not they are highly dependent on contextual information, not just semantically, but even syntactically.
Ultimately, natural languages and programming languages serve very different purposes: communication in natural languages is highly dependent on context for resolving ambiguity (e.g., if we took this discussion to a non-programming forum, everybody else would be like "WTF is going on?"), while programming languages are all about making it possible to be pedantically precise without reducing convenience.
* Category functions in Objective-C
* .NET extension methods
I agree. And I think that Rust has one of the simplest semantics around in programming languages: the method resolution rules are quite simple, because there is no method overloading and all extension methods have to be explicitly imported to be used. These two restrictions are important for maintainability: the former means that the compiler "never guesses"—multiple applicable methods are a compile time error—and the latter means that you never have to guess about where a method comes from, because it was always defined either alongside the type or defined as part of a trait that was explicitly imported at the call site.
Whenever a project takes me into modifying that codebase I get that sinking feeling you get when you realize you left your wallet at a restaurant or that your car has been towed.
http://www.infoq.com/presentations/Language-Expressiveness
tl;dw: there are many types of expressiveness in programming languages, and they're all important.
Likewise syntax is important too, but often downplayed by the PLT community. Case in point: your post!
Django models are definitely more self-documenting than Rails models, which seems like a result of the Python legibility culture.
That seems like a odd restriction/asymmetry. What's the rationale?
fn add1(x: &int) -> int {
*x + 1
}
fn main() {
println!("{:?}", [ 1, 2, 3, 4, 5 ].map(add1));
}I'd agree that unconstrained metaprogramming can make code hard to read, and Ruby especially tends to go overboard. One of the goals of Objective-Smalltalk is to create a language with cleaner extensions points, making semantically consistent extension/adaptation easier. We'll see how it works out :-)
I was thinking specifically of the expression problem:
> The Expression Problem is a new name for an old problem. The goal is to define a datatype by cases, where one can add new cases to the datatype and new functions over the datatype, without recompiling existing code, and while retaining static type safety (e.g., no casts).
http://en.wikipedia.org/wiki/Expression_problem
Now, I'm not saying Rust solves the expression problem, nor that what I'm calling "expressiveness" here maps to it, even. Just that we can easily argue over what "expressive" means.
> Libraries like RSpec might be "nice", but they are certainly not "easy-to-use", and I don't think I would call them quite "clean", if I look at their source code.
The whole point of the quote is that the source code is ugly. I think we're in violent agreement here.
Also, it's not only about ugly internals, I wrote several RSpec tests that looked like a perfectly reasonable "natural-languigish" expression of what I wanted to, but did not work at all, because of the tricky mechanics underneath happened not to support this particular combination of incantations. In the end you have to understand all the ugly internals to know which of the "nice" calls will work and which not, and I all the time see people shooting themselves in the foot because of not having this understanding, e.g. not wrapping some bit in a lambda in Ruby and having something evaluated at the wrong time etc.
It doesn't actually complicate the language to allow stuff like this: it just naturally fell out of having traits and methods, which we needed for other reasons.
You can say this about any programming language feature or concept.
Stuff like this is the opposite of expressive to me because cute expressions that read like english scream "please take a moment to analyze this and make sure it does what it sounds like." I am instantly suspicious of code that is so expressive that you can just intuit out its purpose without thinking it through. Code is too subtle for treating it that way.
2.days().from_now()
says that we are performing the action 'days' on the object '2'. What could that possibly do? Well there're a bunch of options and none of them are obvious. It's weird and counter-intuitive. And then we go and perform the action 'from_now' on whatever the result of that was.
I don't mind 'fluent' style interfaces as long as they don't mess up the normal meanings of what objects and methods are, but sadly they usually do. Another classic sign of a bad interface is that with many of these fluent interfaces it's not obvious whether a particular thing should be a chained method call or argument, and if it's an argument, whether it should be got by calling a method, using a field or what.
Code should read like clear code. English should read like clear English. These are not the same things, and efforts to make them the same usually make a mess of both.
This stinks of principle over pragmatism.
Using the sugar offered by expressions like this requires a bit of knowledge of the development environment. I accept that it's a bit of a different syntax from some other languages, but that doesn't make it unclear at all.
Speaking to Ruby (where ActiveSupport is from), we all know that it's a language that focuses on providing flexible syntax and "multiple ways of doing things", in contrast to some other languages which restrict flexibility to provide more consistently structured code. I don't think it's a bad thing at all to have these options available, and I often find my intentions are much clearer when writing, say:
if post.created_at < 5.minutes.ago
as opposed to Python: if(post.created_at < (datetime.now() - timedelta(minutes=5)))
or Objective C: NSDate *now = [NSDate date];
NSDateComponents *delta = [[NSDateComponents alloc] init];
[delta setMinute:-5];
NSDate *fiveMinutesAgo = [[NSCalendar currentCalendar] dateByAddingComponents:delta toDate:now options:0];
if ([[post created_at] compare: fiveMinutesAgo] == NSOrderedAscending)
That's where the magic comes in, and it makes Ruby ideal for things like web templating, while perhaps making it less suitable for other uses.TLDR: various constructs like this have their place, they're not inherently bad, but do require knowledge of the tools.
if post.created_at < 5.minutes.ago
That reads like "if post was created less than five minutes ago", i.e. if the post is more recent than five minutes. However, if "5.minutes.ago" gives you a timestamp, as is reasonable, then that actually means "if post was created more than five minutes ago", i.e. the very opposite of what it would mean in English. Whoops!A more sensible syntax might be:
if post.creation_date < now - 5*minutes
"ago", "from_now", "since_now" etc. are especially despicable in a language where you can just use + and -. if post.created_at < datetime.now() - timedelta(minutes=5):
I agree with your argument in principle though. Just wanted to point out if you're trying to make a point about readability / ease of use with code examples, you should try as hard as possible to make each example optimized to avoid accidentally creating a straw man.Also, one criticism of the Ruby version is the invisibility of the types involved. What type is returned by 5.minutes vs 5.minutes.ago? How do I know I can compare the second to a date attribute from ActiveRecord? The Python version is more verbose, but I think it's more clear that I'm using two datetime's and a timedelta.
if post.create_at < 5.minutes.ago
if post.created_at < minutes_ago(5)
if ([post created_at] compare: [minutes_ago 5])If you see 2.days(), you know where to look for the definition, and the type system means that you can easily learn what 2.days() returns. In this case, 2.days() returns a `TimeChange` object (https://github.com/wycats/rust-activesupport/blob/master/dsl...).
`TimeChange` implements a `from_now` method here: https://github.com/wycats/rust-activesupport/blob/master/lib... which returns a `Time`.
This is a lot easier to reason about than global modifications to Fixnum in an untyped language.
And sure, you can always look up in the type system or (in a less well typed language) the documentation to see what it does. The point of writing clear code is that doing so should be a last resort and not something you force on developers in order to understand what's going on.
Readability is what we're talking about here, and TimeChange.days(2) or new TimeChange(2, Days) are both more readable options than 2.days() because a programmer knows what is happening without querying information that isn't written in front of them.
use 5.016;
use warnings;
{
use autobox;
use autobox::DateTime::Duration;
say 2->days->ago; # 2013-12-27T14:03:47 (stringified output of DateTime object)
}
say 2->days->ago; # Can't call method "days".... (runtime error)And yes, I was talking OOP since we were talking about ruby. Functional or logic programs also have their own rules that tell you how to understand them. If you want to create good programs you shouldn't mess with those basic rules in order to make the code look more like something it actually isn't.
It does what the language definition says it does: it tells the compiler to search through traits that were explicitly imported to find a method called "days" and calls that. (Since "int" is a built-in type, it has no methods except for those provided via traits that were imported, so traits are the only potential source of these methods.) If there is more than one matching method, Rust never guesses: it instead signals an error.
> I don't mind 'fluent' style interfaces as long as they don't mess up the normal meanings of what objects and methods are, but sadly they usually do.
In this case they don't. It falls out of what traits and methods already provide in Rust.
Every time I have to write something like
Calendar cal = Calendar.getCalendar();
cal.setDate(new Date());
cal.addDate(-2, Calendar.DAYS);
Date date = cal.getDate();
I want to claw my eyes out. Is this what you consider more clear, less cutesy, somehow more productive than just 2.days.ago ?
http://www.reddit.com/r/programming/comments/21pnl/just_say_...
I still do not have good answers to the questions I asked in that first comment.
Autocompletion clearly show what you can and can't chain, but admittedly it helps a little less when the DSL asks for a parameter of some type and you don't know how to start building one. Perhaps one way to evaluate the learning curve of an API is to count how many non-standard types appear in the signatures of its methods, excluding the `this` parameter.
This is how it works in C# (and .NET in general)
DateTime.Now.AddDays(-2)
Not wanting to overload integers with calendar functionality doesn't mean you have to make it so cumbersome.
Edit: typo
DateTime.Now - TimeSpan.FromDays(2) date.today() - timedelta(days=2) Time.now - 2.days
Time.now.advance(days: -2)All these questions and more are why you should question "english as a DSL" methods. Granted, there's a ton of great libraries out there which do this well. Take Unfiltered's handling of parsing out requests for pattern matching:
case POST(Path(Seg("api" :: "calls" :: name :: Nil))) =>
this is "englishy" without having you scratch your head. I've also seen case POST(Path("/api/auth")) & Authenticate(user) =>
which then causes a DB access to happen in a configured implicit value. Now try to reason out why something is not hitting a URL or being denied when you first get into a project. Unless there's someone to explain it to you, it just happens like "magic."TL;DR How much magic do you want to deal with?
Also, this example is simply wrong ...
case POST(Path("/api/auth")) & Authenticate(user) =>
It's wrong, because this is a route matcher, so if the user is not authenticated, the server would throw an HTTP 404 Not Found status. HTTP 404 Not Found is not meant to be thrown in case the user is not authenticated. There are more relevant, more correct status codes for that, like 401 Unauthorized or 403 Forbidden, or heck, in case of classic websites, a 302 redirect to a login page is in order. So you're basically giving broken examples and calling them "magic", but the examples are obviously broken, the first one in terms of API design, the second one in terms of functionality.I really do understand your hint. But the examples you're giving are bad because they rely on totally non-obvious mutable state that's passed implicitly. This is indeed "magic", however it has nothing to do with syntactic sugar.
Btw, in Scala 2.10, thanks to String interpolation, you can also construct route-matchers such as this on [1]:
case POST(p"/api/calls/$name/") =>
Doesn't work by default, you really have to build your own stuff for this to work (see the linked example). Or if you want to throw in regular expressions, you can either do this: case POST(p"/api/calls/${Integer(id)}/") =>
Or you can have like a mini-DSL with regular expressions built-in: case POST(p"/api/calls/$id<\d+>/?") => // note the optional ending slash
Yeah, you can do this. And it's certainly much nicer to me, more flexible and you can also make it faster than pattern-matching on Lists, as in your Unfiltered example. Is this magic? I don't see a local, mutable and implicitly-passed context, so for me it doesn't qualify.Also, whenever I see "2.days" or "4.kilobytes" in Scala code, it's certainly nicer than seeing "172800 /* 2 days in secs /" or "4096 / 4 kb */". Given that these are statically type-checked extension functions that do not modify the original Int type and that do not rely on some mutable local state passed implicitly, is this magic? My IDE doesn't think so.
[1] https://github.com/alexandru/shifter/blob/master/web-api/src...
Personally I don't think it's inherently good or inherently bad, it's just one of those things that makes good code better (concise & expressive) and bad code worse (inscrutable & brittle). Glass-half-full and glass-half-empty are both valid opinions on the matter, and I strongly suspect which way you leans towards depends heavily on the quality of other people's architecture that you have to deal with on a daily basis.
Almost never.
Where it makes code clearer and more concise, or where it simplifies an often-used idiom, and where it outweighs the benefit of sticking to principles—as I believe it does here—then breaking those principles is acceptable, in languages where coding conventions support this.
> Now, I should say that I almost never use 2.days, but the point is that it’s a benchmark of expressiveness: if you can write it, you can do all sorts of other flexible things.
I tend to use MiniTest, not RSpec. But I'm very glad RSpec can be built.
The result is the ability to express
assert { :apple == :orange }
assert { [:apple, :banana, :pear].include? :orange }
and let the computer figure out the appropriate error message and expected-vs-actual diff.Here's an example implementation:
public static class DateExtension
{
public static TimeSpan Days(this int daysCount)
{
return new TimeSpan(daysCount, 0, 0, 0); //days, hours, mins, secs for those wondering
}
public static DateTime FromNow(this TimeSpan addTime)
{
return DateTime.Now.Add(addTime);
}
}
usage is the same as Rust: 2.Days().FromNow();
Basically if this impresses you, you really should give modern C# a whirl as there's a lot more you can do than that.Much of how it does stuff now is how my perfect language would do it. There are bits of Ruby I love, and Rust is looking more exciting to me than Go, but the C# team really have done some amazing work.
C# is a great language, but I don't use Microsoft technology, and it's effectively Windows only.
list.sorted
list.max
list.sum
The problems with languages like C# (or Java) go beyond the lack of type-classes or similar. For example, given a type such as List<T>, you're restricted to define methods only "for all T", as these languages don't allow you to define methods only "for some T". Examples include methods such as "sorted" or "max", relying on type T implement some sort of Ordering interface, or "sum", relying on type T to be some sort of Number.So even if you would have a way to specify that types X, Y and Z have implementations for this and that set of extension methods, it would still be of somewhat limited use, given that one wouldn't be able to also use this in code making heavy use of generics, much like how interfaces themselves aren't used much in code making use of generics, because code making use of generics is usually some dumb Container<T>, where almost nothing about T is known.
The best part about type classes is that you don't have to control/modify the type's implementation, in order to make it implement a certain type-class. Type-classes are like open interfaces. If the builders of C# never bothered to provide you with some common interface you want for built-in types, that's OK, because you can make one up.
Scala also has implicit conversions and "view bounds". Implicit conversions go beyond extension methods and View Bounds allow one to specify that some type X can be viewed as some type Y (either Y inherits from X, or there exists an implicit conversion from X to Y). Something like:
def sample[X <% Y](item: X) = item.callMethodFromY
But these are mostly useful for builtins and deprecated for general-purpose usage in favor of context bounds (type-classes). It's still nice that builtins such as Array[T] is viewable as a Seq[T] and Strings are viewable (in true FP tradition) as sequences of chars, allowing one to do this: "some string".map(_.toUpper) // => "SOME STRING"
What's interesting about the above example is that mapping over a string, produces a string, highlighting another Scala feature that goes beyond type-classes: "some string".map(_.toString)
// => Seq(s, o, m, e, " ", s, t, r, i, n, g)
This is the CanBuildFrom pattern. See this awesome example from M. Odersky himself: http://stackoverflow.com/a/1728140Yep.
> Scala and Haskel have them.
Correction: Haskell has them, Rust has a limited form of them, and Scala encodes them using a horrible hack.
> The problems with languages like C# (or Java) go beyond the lack of type-classes or similar. For example, given a type such as List<T>, you're restricted to define methods only "for all T", as these languages don't allow you to define methods only "for some T". Examples include methods such as "sorted", relying on type T to be part of the Ordering type-class, or "max", relying on type T to be part of the Number type-class.
Both C# and Java have bounded generics. The problem is that bounded quantification is fundamentally tied to class/interface hierarchies.
> The best part about type classes is that you don't have to control/modify the type's implementation, in order to make it implement a certain type-class. Type-classes are like open interfaces.
I know.
> Scala also has implicit conversions and "view bounds".
To be honest, I am not fond of those. (And, in general, I am not fond of the way Scala does things.) If I want to view an X as a Y, a regular function "f : X -> Y" can already do that. The notion of subtyping roughly corresponds to the idea that certain functions of the form "upcast : Subtype -> Supertype" are worth implicitly applying. I do not agree with this idea.
In retrospect, sort of a horrible idea, but I wonder, is there a type system out there expressive enough to allow that behavior?
After a few moments thought I realized why it wouldn't be possible in languages that use a fixed v-table, but it should be possible in languages that use more flexible calling techniques!
Or am I thinking about this too much and this is basically what type classes would buy me?
I cannot agree enough. This is not new for any C# dev.
Expressiveness is not as great as ruby, but certainly on par with Rust
LINQ extension methods by Jon Skeet - https://code.google.com/p/morelinq/
I would recommend reading this stackoverflow post to see some great extension methods.
http://stackoverflow.com/questions/271398/what-are-your-favo...
For a much further-reaching solution to this problem look at C++ user-defined literals, combined with what Bjarne Stroustrup calls "type rich programming".
now() + 3days + 21s + 102ms
This is not to say that c++ is the only language that can do this (although it does do it very well).
See the following video for a simple overview (apologies for toe-curling introduction): http://channel9.msdn.com/Events/GoingNative/GoingNative-2012...
...also the video above is worth watching just for the vector vs. list comparison.
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.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.)
- 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.
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.
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.
[2] http://dl.acm.org/citation.cfm?doid=2508168.2508169, https://www.hpi.uni-potsdam.de/hirschfeld/publications/media...
[3] http://metaobject.com (used in most products)
EDIT: added sentence about approach.
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.One thing worth noting is that the implementation isn't globally extending `uint` with `.days()`. In order to write `2.days()`, you need to first import the trait that provides the implementation.
From that perspective, it's not much different from importing a function like `days_from_now` that you call as `days_from_now(2)`.
2.months().from_now()
printing a date one year in the past? date: active_support::date::Date{year: 2013, month: 2u, day: 28u}, hours: 0u, minutes: 59u, seconds: 6u, nanos: 243388962u void Main()
{
Console.WriteLine(2.Days().Ago());
Console.WriteLine(4.Days().FromNow());
}
//This can be a library, mmkay?
public static class Extensions
{
public static TimeSpan Days(this int days){ return new TimeSpan(days, 0,0,0); }
public static DateTime FromNow(this TimeSpan howMuch){ return DateTime.Now + howMuch; }
public static DateTime Ago(this TimeSpan howMuch){ return DateTime.Now - howMuch; }
}
Results in: 26/12/2013 22:05:10
01/01/2014 22:05:10
Is there a better example? public static class Extensions {
public static void Print(this object o) { Console.WriteLine(o); }
}
2.Days().Ago().Print();
4.Days().FromNow().Print();By extension, if an object knows how to convert itself into another object, it implements a method for doing do. In Smalltalk there is a convention of putting all such methods inside "converting" protocol. For great example of how is this useful you can look at the Collection and it's subclasses in Smalltalk - all collections have many "asOtherKindOfCollection" methds, which are either inherited or implemented, depending on a collection. It's great to be able to send "asSet" to any collection that comes your way and the way Smalltalk does it is natural in purely object oriented languages.
That and the fact that creating IntsToRange or StringToDayOfWeek "static" classes is actually even worse leads to the design where Integers have `to:` and `days` methods.
Actually, there is one more probable cause: in Smalltalk and some other languages extending already defined types is easy, really easy, easier even than creating a new, empty class. The methods are grouped into "protocols", which are grouped into packages and it's - not quite - largely orthogonal to the classes. Combine that with powerful IDE, VC support (Monticello, Metacello) etc. and you have no reason whatsoever not to extend even the most basic classes.
In general I find it cleaner when less primitive classes can represent themselves as more or equally primitive classes, but not vice-versa. to_integer, to_float, to_list, to_boolean, etc. But on the other extreme, if your application entails establishing database connections, implementing String#to_database_connection would be ridiculous.
For me, integers are much more primitive than date and time objects. `2.days` would be equivalent to implementing `2.meters` in a CAD application, but I suppose you might be OK with that.
But even granting that an integer can transform itself into a representation of a time span, i.e. `2.days` is okay, `2.days.ago` is right out. That's not just an Integer representing itself as a span of time. Now all of a sudden Integer has to have knowledge and, even worse, opinions about the current date and time (which is one of the most hazardous boundary cases in all of software).
In terms of concepts, yes, but in terms of language syntax and semantics they are the same (in Smalltalk).
> `2.days` is okay, `2.days.ago` is right out
Yeah. But not because it "looks bad", and not because Integer would need to know anything more than it does already (it wouldn't), but because `2 days ago` is actually less flexible and harder to maintain.
Consider:
Date today - 2 days. "27 December 2013"
DateAndTime now - 2 days. "2013-12-27T04:43:42.679+01:00"
We can get either Date or DateAndTime without changing anything. With proper planning the superclass of both Date and DateAndTime can have a generic - method which will work for all the cases. With the `2.days.ago` we're tied to one result type. We need to code around it, giving it a default argument with a class or remembering the "kind" of Duration.Anyway, if it's ok to have a function which takes integer it's equally ok to have an Integer have a method. Especially when you just can't have a function, like in Smalltalk and the only alternative is writing yet another class with one method in it.
(-> 2 days from-now)
The great thing is that you don't have the weird semantic problems that the rust or ruby examples have. There are just two functions here, days and from-now.
fn days(&self) -> TimeChange {
TimeChange::new().days(*self as f32)
}
So while you are right, it is basically sugar for days(2).from_now(), but you couldn't call the function that way. It must be attached to an instance of an uint.Also in order to get the English looking version you have to implement those function specially using traits. Whereas with the clojure version they are just normal functions, but the threading macro gives you that English readability you are looking for.
We plan to implement that, it's just not implemented yet: http://www.reddit.com/r/rust/comments/14pucx/rfc_method_call...
> Whereas with the clojure version they are just normal functions, but the threading macro gives you that English readability you are looking for.
You could do that with a macro in Rust as well. We don't make ordinary functions acquire that sugar for namespacing reasons: you can have multiple methods called e.g. "get" in scope as long as they're attached to different types, but that isn't true for functions.
FWIW: I believe that the Rust folks want to allow support for calling the functions directly.
2 days hence
4 weeks ago
etc.
http://docs.factorcode.org/content/article-calendar.htmlBecause I've been working in D for a while, here's how you'd do it in D.
import core.time: Duration, dur;
import std.datetime: Clock, SysTime;
Duration days(int number) {
return dur!"days"(number);
}
SysTime from_now(Duration duration) {
return Clock.currTime + duration;
}
void main() {
import std.stdio: writeln;
writeln(2.days.from_now);
}
Runnable pastebin link: http://dpaste.dzfl.pl/9b84f820Is D allowing you to write the function call "days(2)" as "2.days"?
(As an aside, Nimrod allows you to do specifically that; first-arg.function-name(...) == function-name(first-arg,...).)
But slowly, this has started to change. I think that it’s that I tend to do more infrastructure and plumbing work now. I’ve run into more and more situations where types would be helpful, or picking the mutable state path has caused headaches.
Explaining the attraction of dynamic debugging to static type die-hards is a lot like explaining the attractions of static typing to dynamic programming die-hards. You can only get a sense of what the other side is talking about through lots of hands-on experience. Where it's really valuable is often an edge-case, but often it's a really expensive edge-case! (Goes both ways.)
This from an old Smalltalker. If you do enough Smalltalk, especially in a large established code base, you eventually see situations where you wish there were type specifications. However, you still wish that more statically compiled language people were more open minded and less knee jerk when you talk about the magic you can do. (Which also means that the open minded ones are particularly valuable.)
The ultimate aim is to write the nextgen browser engine not an operating system kernel or some embedded code for a system with 4kB RAM.
@(2).days.fromNow;
or
@(2).SH_days.SH_fromNow;