Archive for May, 2018

Rust: Lifetimes Sugar

Sunday, May 27th, 2018

One of the Rust language features is explicit object lifetimes that help compiler correctly track memory usage and free objects without using garbage collector. A neat idea but it leads to lifetime specifiers being used everywhere including places where compiler should be smart enough to deal with them without explicit mentions in every place.

Maybe I’m using Rust wrong but in most of the cases I create objects that have no need for lifetime specifier or the objects that have the same lifetime for both its members and itself. Thus I argue that in addition to generic lifetime specifier 'a (or whatever the name you give it) and obviously named 'static there should be 'self that specifies the lifetime to be exactly the same as the object itself.

So, instead of current:

struct Foo<'a> {
  myref: &'a [u8],
  subobj: Bar<'a>,
}

impl<'a> Foo<'a> {
  pub fn new(myref: &'a [u8], subobj: Bar<'a>) -> Self { ... }
}

it should be possible to write:

struct Foo {
  myref: &'self [u8],
  subobj: Bar,
}

impl Foo {
  pub fn new(myref: &'self [u8], subobj: Bar) -> Self { ... }
}

I am not sure whether compiler needs to perform some additional things in such objects compared to objects without no lifetime specifier but it should be easy to assign proper lifetime after parsing the structure definition anyway and I’m pretty sure the compiler does something like this anyway.

And I see only these reasons why this has not been done yet:

  • Considerations for compiler simplicity (i.e. parsing process should be kept as simple as possible)—I still think it should be easy for compiler to recognize the lifetime definition by the time structure declaration parsing is over and it’s used externally (i.e. for objects using this one);
  • Considerations for language clarity and consistency (i.e. it’s immediately obvious when you look at the object that it deals with lifetimes but not with the proposed change). I’d argue that explicit lifetimes should be kept for complex cases only, when you have to juggle lifetimes from several complex sources, and the objects with references not outliving themselves should be fine;
  • Simple oversight (i.e. “we did not think of such simplification”) or developers’ bias (i.e. “we got used to writing lifetime specifiers everywhere that we didn’t think it annoys anybody”). You should be able to guess what I have to say about such argument.

So all in all I’d be happy to either hear why it cannot be done (beside the compatibility with the existing code) or see it implemented. But most likely this will be ignored (and I’m fine with that too).

BeNiLux Railways: An Impression

Tuesday, May 22nd, 2018

So I had a chance to visit Belgium and Netherlands and what I’ve seen there makes me write this post.

Luxembourg

I visited it some years ago and it looked quite decent to me, nothing particularly strange.

Belgium

Previously I only went to Brussels for FOSDEM but this time I travelled around a bit and saw places outside the capital too.

So, they have nice touches like typeface used for station names, various kinds of trains (though I haven’t seen their outdated train that used to go between Liege and Aachen) and very interesting rail station in Antwerpen.

The only strange thing is that they hang out timetables for workdays and weekdays separately (at least in Bruxelles Nord).

The only stupid thing I saw is ticket machines having a special button for international trains and when you press it it tells you that you can’t buy an international train ticket there. And Belgium is such a small country that it’s hard to travel in any direction for an hour and not cross some border (or get into the sea). One would expect that buying tickets to neighbouring lands would be easier, especially for such close countries like Belgium and Netherlands.

Netherlands

Now this country looks like everything there was designed by idiots.

First, trains. By themselves they are not that bad but they have the best counterintuitive designed door open buttons. First, they are labelled the same: half-opened (or half-closed) doors with small arrows showing opening or closing. So if you have not a very good sight (like me) you’ll be confused. But buttons are colour-coded! Yes, and while on German trains it’s intuitive green—open, red—close (or just a single button for open/close), Dutch trains have yellow button for opening doors and green (or blue for random Japanese) button for closing. Honestly, it should be intuitive to have green button to open doors so you can go. And I’d like to hear a reason behind this beside “well, cannabis is legal in Netherlands”.

Next, timetables. Those are confusing as well. At least in Rotterdam timetables are hanged separately for each fork (well, it should be a line but most of them are drawn as forks which probably means the train parts separate at some point and head in two different directions)—maybe it’s this convoluted system made them invent InterCity Direct too (don’t ask me how that’s different from normal InterCity). And the separate timetable for international trains. Confusing.

And since that was not enough stupidity, they decided to install turnstiles in Rotterdam Centraal so in order to enter or leave the station you need to scan your ticket (yes, it’s like what you have in underground systems but in this case for rail station). And it might be the only station there with such a feature, I saw nothing like that in Amsterdam C when I visited couple of years ago or in The Hague two days ago.

Speaking of The Hague, they have the stupid station name—Den Haag HS where last two letters stay for Hollands Spoor or Dutch Rail. I know only two cases where such naming makes sense:

  • you have a station in the same town belonging to different railway operators e.g. in Basel you have the main station operated by CFF and so it’s called Basel SBB, French railways have their own section there called Elsässerbahnhof or Bâle SNCF and there’s a station used to belong to Baden Railways that is still called Basel Badischer Bahnhof;
  • you had a competing rail operator and the name stuck (a variation of the above really)—e.g. stations on track Bullay (DB)—Traben-Trarbach(DB) are called so because there was another rail line (on the other side of Mosel) with the same stations and when it was closed nobody wanted to rename stations just because;
  • you’re SNCF and you want to mark your stations because they’re yours and no foreign train should set wheel there!

And as far as I know none of this applies to The Hague. I suspect it happened because they have built a new station later (more than a century later) that they designated as central one and could not make a good name for the old station. It’s like in Germany they’d rename station Hamburg-Altona to Hamburg Hbf and Hamburg Hbf to Hamburg DB. In other words, pointless and stupid.

Overall, it was an interesting experience travelling Belgium and Netherlands but I did not expect that much stupidity from the latter. Anyway, the next post should be about Rust.