5 ms·
Static checking of units in Servo
- portmanteaufu 12y ago> Furthermore, the units are used only at compile time. At run time, a Length<Inch, f32> value is stored in memory as just a single f32 floating point value, with no additional data. The units are “phantom types” that are never instantiated and have no run-time behavior. I love that you get all of this safety with zero runtime cost.
- dllthomas 12y agoThis is the difference between types and tags (sometimes they're conflated).
- illumen 12y agoThis code is buggy because they are using floats internally. A 32bit float can not represent the exact measurements specified. This is a perfect example of why just statically type checking your code does not make it correct.
- haberman 12y agoEvery time I read about Rust I have to fight the urge to drop everything and just chase it. So much awesome going on over there.
- coolsunglasses 12y agoWe've been using stuff like phantom types in Haskell for years, where have you been? http://www.haskell.org/haskellwiki/Phantom_type http://www.haskell.org/haskellwiki/Phantom_type
- wmf 12y agoBeing scared away by laziness, monads, and people generally talking like math geniuses, I guess.
- coolsunglasses 12y agoYou're at least being honest by saying it's about fear - because it is. I teach Haskell to experienced programmers and people that have never written code before. I know nothing about math, I dropped out of uni after one quarter, I have not studied math since that time. Most of my students didn't know any math and weren't any worse for it. https://github.com/bitemyapp/learnhaskell https://github.com/bitemyapp/learnhaskell If you start going through the guide and get stuck, I will help you. Only fear can stop you, be brave!
- codygman 12y agoTo add to this, fear is one of the reasons I've been procrastinating about finishing a type-class related exercise in Beginning Haskell. There is something terrifying about feeling like you won't be able to solve a problem. However, from experience I know how much better off I'll be after getting over that and solving the exercise ;)
- haberman 12y agoI have mad respect for Haskell as an idea incubator, but it's not what I'd reach for as a systems programmer: https://news.ycombinator.com/item?id=7895890 https://news.ycombinator.com/item?id=7895890 Rust is awesome because it's bringing powerful ideas like this to a systems language.
- coolsunglasses 12y agoSystems work is the only good reason I've heard not to use Haskell but it's not a good reason for not having learned Haskell. I've heard from people that learned Haskell independently or went through my guide alike that benefited greatly from having learned Haskell despite not using it at work. Types are a more compact and faithful representation of code semantics than what most non-Haskell programmers are working with internally. Haskell also teaches you, beyond thinking in terms of types (heh), how to identify patterns & abstractions that most languages wouldn't allow you to credibly leverage.
- coldtea 12y ago>I've heard from people that learned Haskell independently or went through my guide alike that benefited greatly from having learned Haskell despite not using it at work. Could also be a failure to admit they wasted their time, because of sunk costs et al. Not meaning it as snark -- it can always be a valid hypothesis when we're talking about people.
- coolsunglasses 12y agoNo, I know what you mean and I've run into that. I take people don't use/write Haskell regularly and claim to understand it less seriously for this reason. But people that I think got something out of it seem to be benefiting. One is a startup CTO that was using Clojure before - he's ship-of-theseus'ing his way out.
- dllthomas 12y agoI've been using Haskell "at work" on my side projects (which involve other people and a live website, not just toying around), and I'd say that having learned it has impacted the C that I write in my day job. I wouldn't say I'd wasted the time, with or without an impact on my C code.
- coldtea 12y ago>We've been using stuff like phantom types in Haskell for years, where have you been? Lusting over a language that borrows its syntax from the Algol we all love and doesn't require a degree in Type Theory? Oh, and without laziness, and with speed and no-GC guarantees...
- dllthomas 12y agoThe language in common use with the closest syntax to Algol is probably Bourne shell.
- coolsunglasses 12y agoOne or two of your five objections are valid and not because you're well-informed. Let me know if you'd like to know more.
- coldtea 12y agoLet's see: 1) Not algol derived syntax (check) 2) Speed (check -- I don't care about the: "you can write it totally unidiomatic and squeeze it to almost C or even better for some microbenchmark" situation). 3) Degree in type theory (meant as a joke anyway, refers to the steep learning curve of the tons of concepts introduced). 4) Laziness (don't care about the "you can switch it off" situation). 5) Has a GC (check). Is there anything I really missed? In real life, classical use, as opposed to "if you really try and want it, you can also do it this way"?
- CyberShadow 12y agoHave you looked at D yet? It scores 4/5 by your mark, and they're working on the fifth point. Here's the documentation for the proposed units module. I believe it has the same principles as Boost units: http://klickverbot.at/code/units/std_units.html http://klickverbot.at/code/units/std_units.html
- bjz_ 12y agoI think he was making a more general statement than just in regards to the article. Being able to use a subset of the awesomeness of Haskell in a systems language is very refreshing.
- DCKing 12y agoNot really a great reply as 'phantom types' are possible in the majority (all?) of typed languages with generics. Even C++ has them - that is mentioned in the article. What I think the OP was expressing is that the various articles written on people writing great code in Rust inspires people to work with it. Admittedly phantom types are not a distinguishing feature of Rust, but it still makes a nice read that people are elegantly solving real problems in the language.
- annesa 12y agohttps://blog.mozilla.org/research/2014/06/23/static-checking-of-units-in-servo/ https://blog.mozilla.org/research/2014/06/23/static-checking...
- mihai_ionic 12y agoBoost has a neat library that does this and more: http://www.boost.org/doc/libs/release/libs/units/ http://www.boost.org/doc/libs/release/libs/units/
- orf 12y agoBut C++ is not cool so who cares.