5 ms·
In Rust, you have to import a trait (in this case active_support::Period) into the file that uses 2.days(). It's not a global change. If you see 2.days(), you
by wycats 13y ago
In Rust, you have to import a trait (in this case active_support::Period) into the file that uses 2.days(). It's not a global change.
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.rs#L7 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.rs#L30-L32 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.
- kybernetikos 13y agoYes, I took the fact that this was not a global modification of a widely used data type as a given, since to do so would be crazy in any 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.
- wycats 13y agoThis must be a question of aesthetics because the 2.days().from_now() seems perfectly obvious to me. Maybe someone can help me understand why it seems confusing?
- Touche 13y agoIt's non-obvious to me what 2.days() returns, that's the problem.
- matthewmacleod 13y agoI don't think that's a problem: avoiding features or fiddling with syntax in order to make the code more accessible to people who are not familiar with the language or its libraries isn't going to gain much in the long run. Languages and libraries should be geared towards long-term developer happiness, rather than a shallow learning curve. Anyway, I know as close as possible to nothing about Rust. It seems blindingly obvious to me that `2.days()` returns some kind of duration, and `2.days().from_now()` returns some kind of time. So I've looked at the code, and that appears to be correct - `2.days()` returns a `TimeChange`, and `2.days().from_now()` returns a Time. I can't think of any other reasonable interpretation.
- ivanca 13y agoYeah, you _had_ to look at the code to realize that; when it could have been new TimeChange.fromString("2 days from now") Which would have been more helpful for actual development, not only for a dumbed-down syntax aiming for easier comprehension.
- comex 13y ago> Yes, I took the fact that this was not a global modification of a widely used data type as a given, since to do so would be crazy in any language. Yet this is what Ruby does, AFAIK.
- matthewmacleod 13y agoYet this is what Ruby does, AFAIK. Numeric#days is an ActiveSupport method, not core Ruby. But yes, it does patch the behaviour into a core class. This is a bit of a legacy of Ruby programming, and it's definitely bad to pollute the global namespace. However, I expect we'll see eventual use of Refinements (http://www.rubyinside.com/ruby-refinements-an-overview-of-a-new-proposed-ruby-feature-3978.html http://www.rubyinside.com/ruby-refinements-an-overview-of-a-...) now that they are available; this will allow classes to override core methods without affecting other classes out of scope.
- philwelch 13y agoRefinements do a little less than previously advertised three years ago, and as far as language features go I don't think they're worth the performance implications.
- deleted 13y ago[deleted]
- pcwalton 13y ago> 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. Well, all you have to do in Rust is to look at the imports, since you have to import the trait defining the method at the call site. Because Rust modules never span multiple files, and imports must be at the top of the block that they're used in, that import will always be before the call. There are also no global imports in Rust (at least, without using a deprecated feature flag), so the trait in question is always named explicitly.
- draegtun 13y agoPerl's autobox is lexical so it is also not a global change. For eg. 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)