6 ms·
Past, present, and future of Sorbet type syntax
- dismalaf 2y agoInteresting article, but to me it really defeats the point of Ruby. The hyper-dynamic "everything is an object" in the Smalltalk sense of the definition is much of what makes Ruby great. I hate this idea that Ruby needs to be more like Python or Typescript; if you like those languages use those languages. I get that a lack of types is a problem for massive organizations but turning dynamic languages into typed languages is a time sink for solo developers and small organizations for zero performance benefit. If I wanted a typed language to build web apps with I'd use Java or something. Hopefully Matz sticks to his guns and never allows type annotations at the language and VM level.
- kodablah 2y ago> I hate this idea that Ruby needs to be more like Python or Typescript It's not be more like those, it's be more like helpful, author-friendly programming which is very much Ruby's ethos. Every time I think about ripping out all of the RBS sig files in my project because I'm tired of maintaining them (I can't use Sorbet for a few reasons), Steep catches a `nil` error ahead of time. Sure we can all say "why didn't you have test coverage?" but ideally you want all help you can get.
- PaulHoule 2y agoAs a Pythoner the most direct value I get out of types is the IDE being smart about autocompletion, so if I'm writing with db.session() as session: ... use the session ... I can type session. and the IDE knows what kind of object it is and offers me valid choices. If there's a trouble with it, it's that many Pythonic idioms are too subtle, so I can't fully specify an API like collection = db["some_collection"] collection.filter(lambda doc: doc["amount"].as_integer() > 500) collection.filter({"name": "whatever"}) collection.filter({"field": lambda f: f["subfield"] = jsonb({"a":1, "b":"2})}) not to mention I'd like to be able to vary what gets returned using the same API as SQLAlchemy so I could write collection.filter(..., yield_per=100) and have the type system know it is returning an iterator that yields iterators that yields rows as opposed to an iterator that yields rows. It is simple, cheap and reusable code to forward a few fetch-control arguments to SQLAlchemy and add some unmarshalling of rows into documents but if I want types to work right I need an API that looks more like Java.
- maleldil 2y agoIf I understand correctly, you can do this with overloads. They don't change the function implementation, but you can type different combinations of parameters and return types.
- jimbokun 2y ago> Sure we can all say "why didn't you have test coverage?" Well types are a form of test performed by the compiler.
- jaredsohn 2y agoLLMs can probably help maintain them so that probably could be solved if you start using LLMs more. This maybe already exists, but it would be nice if RBS or Sorbet had a command you could run that checks that all methods have types and tries to 'fix' anything missing via help from an LLM. You'd still be able to review the changes before committing it, just like with lint autofixing. Also you'd need to set up an LLM API key and be comfortable sharing your code with it.
- jbverschoor 2y agoIt’s all about message passing. Why not allow a compile time check if something respond to a message. Types/interfaces are good. Failing early is a good thing. The earliest you can fail is during editing, and then compile/interpret time. In Objective-C, you also pass messages to an object, and it could be anything you want. But it would output warnings that an object/class did not respond to a certain message. You could ignore it, but it would always result in a runtime error obviously. [someObject missingMethod] Swift is a much nicer language in terms of typing, and I have started replacing some smaller Ruby scripts with Swift. Mostly out of performance needs though. (Single file swift scripts) The lack of proper typechecking is one of the main reasons I would not use Ruby, especially in larger teams. Unittests are fun and all, but they serve a different purpose.
- dismalaf 2y ago> The lack of proper typechecking is one of the main reasons I would not use Ruby, especially in larger teams. Cool. I'm using Ruby for a startup because it's 10x more productive for building a webapp versus a compiled language and because I'm solo. And when I want typing I use something like C++ because I don't want to lose productivity without a major performance boost.
- corytheboyd 2y ago> I'm using Ruby for a startup because it's 10x more productive for building a webapp versus a compiled language I’ve been a Ruby/Rails dev for about 14 years. Just started working on Java projects at work and… holy shit, it slows down the entire organization and they don’t even seem to know how much of it is because of the terrible immovable object that is the mountain of Java code they made. Before this experience I had always assumed that rigid types alone would be a boon… oh how wrong I was. The rigidity just leads to people writing 10x more garbage to get simple things done, which causes a 10x worse maintainability problem. The rate at which I hear “oh lol just another NPE” is astounding, how is this constant occurrence not interpreted as a complete failure of the “type” system???? Maybe a crack team of Java Experts wouldn’t do this, but the reality of most software companies is that people of ALL skill levels are writing code. Poorly written Ruby is a whole lot easier to figure out (as an expert of Ruby) than 10x the amount of code in Java. I hate this.
- Lio 2y agoI respect this view but I can really see the utility of gradual typing as your project grows and matures. I also wonder if we’ll eventually be able to pass type information to the JIT. If that helps ruby grow I’m all for it.
- dismalaf 2y ago> I also wonder if we’ll eventually be able to pass type information to the JIT. It's not like Ruby doesn't have types and the JIT can't determine them. It just happens at runtime, literally the defining feature of dynamic languages.
- Lio 2y agoThat’s true but it would be useful to upfront specify exactly what’s in an array and exactly how big it will be for the entire life of the array.
- __float 2y ago> Hopefully Matz sticks to his guns and never allows type annotations at the language and VM level. Why? Just because they _exist_ doesn't mean you'd need to use them.
- arp242 2y agoI find types useful especially for smaller solo projects: because the test suite is much less complete, or even non-existent. Types give quick feedback for a bunch of silly but common errors. It's entirely unclear to me that types reduce developer velocity on small projects; it seems roughly the same to me. I certainly don't see how it's a "time sink". But in the end: if you don't like it, just don't use it? Ruby has never been about restricting how people use the language.
- tiffanyh 2y ago[flagged]
- PaulHoule 2y agoBut the amount of money going by is so huge they can take a tiny percent of it to pay their server bills. Dynamic languages in large systems have been controversial since there have been dynamic languages. It was routine for heroic AI systems of the golden age (1980s) to be prototyped in Lisp and then be rewritten in C++ for speed. You'd hear stories about the people who wrote 500,000 lines of Tcl/Tk to control an offshore oil drilling rig and the folks who think "C++ is a great language for programming in the large" but who don't seem to mind having a 40 minute build.
- igouy 2y agoFor example • … manages the retirement savings of 5.5 million users • … for a value of 23 billion Euros • … with a team of 30 developers over 7 years https://erwan.lemonnier.se/talks/pluto.html https://erwan.lemonnier.se/talks/pluto.html https://janvitek.org/talks/dls09.pdf https://janvitek.org/talks/dls09.pdf
- dgb23 2y ago> But the amount of money going by is so huge they can take a tiny percent of it to pay their server bills. I think that's an oversimplification that doesn't do justice to the complexity or difficulty of the issue. Both sides of the argument can make a really good case. Slow programs (due to language or other factors) increase server costs, but that's only part of the problem. They also increase operational and architectural complexity, meaning you spend more time, effort and specialized expertise on these secondary effects. Note that some of that shimmers through in the article, mentioning "CI flakiness": It's probable that the baseline performance and its effects is a factor there. Performance has a viral effect on everything, positively or negatively. They also complain about async not being a part of the story here, which is also a performance issue. On the other hand, this shop has hundreds of developers. That means deep expertise, institutional knowledge, tooling, familiarity with each line of code and a particular style that they follow. Ruby is also a language that is optimized for programmer experience, and is apparently a fun language to work with. All those things are very essential investments, individually and as a whole. Then we can assume that they use Ruby like most glue languages are used and either write C/C++ modules where appropriate or attach high performing components into their architecture (DBs, queues, caches etc.) The reason I'm harping on this point is because I think it's important to acknowledge a more complete reality than the "hardware is cheap, people are expensive" statement. I assume it's very likely a throwaway line and not a hill you'd die on. But it masks two interesting and important things: Performance matters a lot and impacts cost transitively, but people and culture ultimately dominate for very good reasons.
- usernamed7 2y agoI've been a ruby dev for 15+ years; i'd really love it if ruby adopted a C# similar approach to typing (albeit, more ruby-like and more flexible). It's the most readable, simplest way I would enjoy as a rubyist. Everything else (including sorbet) feels bolted on, cumbersome and cringe. I appreciate the article and how it goes over the constraints; but genuinely sorbet is just not good enough from a DSL standpoint. Type's can be fun and useful, and i'd love to see them incorporated into ruby in a tasteful way. i don't want it to become a new thing developers are forced to do, but there is a lot of utility from making them more available.
- dismalaf 2y agoC#, ew. Crystal (Ruby but statically typed) has nicer type syntax. Or Pony. Or Nim, Odin, and others.
- usernamed7 2y agoHrm, i think this is a good point, It might be something better served by a ruby-like alternative. I'll have to tinker with crystal sometime. Thanks for this comment!
- ksec 2y agoCrystal still has tooling problems and compiling time problem. Both not easily solvable. May be Kagi will grow to certain size some day they could invest back into Crystal.
- emn13 2y agoWhile I'm most familiar with C#, and haven't used Ruby professionally for almost a decade now, I think we'd be better off looking at typescript, for at least 3 reasons, probably more. 1. Flowsensitivity: It's a sure thing that in a dynamic language people use coding conventions that fit naturally to the runtime-checked nature of those types. That makes flow-sensitive typing really important. 2. Duck typing: dynamic languages and certainly ruby codebases I knew often use ducktyping. That works really well in something like typescript, including really simple features such as type-intersections and unions, but those features aren't present in C#. 3. Proof by survival: typescript is empirically a huge success. They're doing something right when it comes to retrospectively bolting on static types in a dynamic language. Almost certainly there are more things than I can think of off the top of my head. Even though I prefer C# to typescript or ruby _personally_ for most tasks, I don't think it's perfect, nor is it likely a good crib-sheet for historically dynamic languages looking to add a bit of static typing - at least, IMHO. Bit of a tangent, but there was a talk by anders hejlsberg as to why they're porting the TS compiler to Go (and implicitly not C#) - https://www.youtube.com/watch?v=10qowKUW82U https://www.youtube.com/watch?v=10qowKUW82U - I think it's worth recognizing the kind of stuff that goes into these choices that's inevitably not obvious at first glance. It's not about the "best" lanugage in a vacuum, it's a about the best tool for _your_ job and _your_ team.
- throwaway346434 2y ago> There was also a project called TypedRuby, largely a passion project of an engineer working at GitHub. After a few weeks of evaluation, it seemed that there were enough bugs in the project that fixing them would involve a near complete rewrite of the project anyways. There's 6 open bugs and 4 closed ones. This seems like either it's throwing shade or they didn't bother lodging bug reports upstream.
- Groxx 2y agoLast commit: 2018 Readme: >Note: TypedRuby is not currently under active development. I think "brief check, move on" is reasonable, particularly if it doesn't appear to already be at several-year-stable quality.
- jez 2y agoWe were in direct contact with the author, most bugs were reported over email. The project was a hobby project and we did not feel it to be prudent to flood someone’s hobby project with dozens of issues created by a team working at a venture funded startup.
- deleted 2y ago[deleted]
- Trasmatta 2y agoWhen it comes to "Ruby-like and statically typed", Crystal is such an amazing language. I think the design is incredible, but every time I try to use it, I just hit so many issues and things that slow me down. I think it's such a cool language, but every time I try to use it I just end up switching back to Ruby.
- durkie 2y agoSame! I reach for crystal when I need something that is going to perform faster than ruby, but man I get my hand slapped constantly when writing it.
- smuffinator 2y agoI love Crystal. It's what I wish Ruby was. If only the tooling was a bit better
- eterps 2y agoCrystal could have been big if it worked really well in combination with Ruby out of the box (f.i. as an extension language).
- Lio 2y agoShout out to the crystalruby[1] project. I’ve not used crystal much myself but I love how easy it is to switch between the two. 1. https://github.com/wouterken/crystalruby https://github.com/wouterken/crystalruby
- deleted 2y ago[deleted]
- breckinloggins 2y ago> My counter is that when it comes to language design, semantics—what the types mean—are easily 10 times more important than syntax Sometimes I long for the days before type theory took over programming language research.
- mvdtnz 2y agoYou don't need fancy boy theories to see value in types.
- dismalaf 2y agoThe value in types is telling the compiler to make certain assumptions so it can optimize the output. There's not a ton of value to be had in something like Typescript or Python types.
- dragonwriter 2y ago> The value in types is telling the compiler to make certain assumptions so it can optimize the output. Correctness is more important (in general and as a benefit of type checking) than optimization. Doing the wrong thing fast is easy, but not valuable.
- dllthomas 2y agoPerhaps Devil's advocate, but my rebuttal would be that we're not necessarily comparing similar gains to correctness as to optimisation, and that perhaps the correctness gains can be better attained another way where the optimisation gains cannot. (Personally I find a type checker tremendously valuable for communicating with myself - and my team, I suppose - about assumptions being made in other parts of the program, which can probably be accounted "correctness".)
- williamdclt 2y agoYet TS is massively popular. So maybe that’s not the only value?
- uticus 2y agoit's amazing to me that the industry hasn't found a better way to support bits of code interacting with other bits of code properly, than adding an attribute to a bit of code calling it a "type" ...saying this as someone who benefits from it but also rarely uses sub-typing ("poodle" type sub to "dog" type sub to "animal" type) or any sort of the other benefits that are commonly associated with typing. for me the benefit is code checking and code navigation of unfamiliar code bases
- xboxnolifes 2y agostructural typing? duck typing?
- uticus 2y agostructural typing is a good step - but afaik there's no way to distinguish between different types that have the same structure but different context - a "truth.right" will be different from a "direction.right", but it's up to the dev to keep that straight. duck typing is what ruby does without sorbet or rbs, it portrays nothing about how the code bits interact at the boundary. if a "dog" is passed in but a "cat" is expected, things will work just fine until runtime when the cat is asked to bark. (saying as someone who is a big fan of ruby overall)
- zhisme 2y agoWith great power comes great responsibility. It is developer responsibility to ensure that you will not receive cat during designing stage of your classes. And inheritance is bad, this is also sharp knife. But annotating everywhere sig { params(cat: Cat) } does not improve your design, it just makes noisy and clumsy. I would think that if your code need type annotations, it smells like bad design and should be considered for refactoring.
- gitroom 2y agoman i always bounce between loving ruby's speed to build and wishing for just a bit more built-in safety tbh. you ever feel like adding more types actually slows you down or does it make big teams way less stressed?
- bradly 2y agoI've been a Ruby dev since 2006 and started using Sorbet fulltime at work about a year ago. After about six months I started wanting it in my personal projects. Not enough to add the dependency, but if Ruby had it built-in I would probably use it. For myself, it makes it easier to work on code I'm not familiar with either because I didn't write it or I wrote it longer than X days ago. The Sorbet syntax is pretty bad, though. And things go from bad to worse as soon as you get a bit tricky with composition or want to do something like verify the type of an association. I haven't tried inline RBS comments yet, but the DSL does look more pleasing. The team in general had mixed views. Some hated it, some liked it, most developers just don't really care–although now the Sorbet advocates are claiming it helps the AI, so now there is that leaning on the Sorbet lever as well.
- deleted 2y ago[deleted]
- deleted 2y ago[deleted]
- zhisme 2y ago[flagged]
- phoronixrly 2y agoRuby is a language that grants you lots of freedom. Including often the freedom to shoot your own foot. Bolting on types to it is something I expect to be possible in Ruby and completely in tune with one or both my previous statements.
- zhisme 2y agoRuby is a language that grants you lots of freedom. Nobody can argue that. With freedom we can do anything, but anyone wondering why in first place? What about wisdom and pragmatic approach? If you ever need types why not choose typed language for your case, but instead re-implementing something and wasting so much time for work that has no real use. Why not use c-bindings for typechecks, this feature is already available in ruby *rb_obj_is_kind_of(value, rb_cString);* and has no cost. Is it wise? I don't think so, I would be shocked if I saw such code in production. We care so much about environment and climate changes, but think it is ok to waste resources for such activity. It is not research task. People really advocate to use in real-world applications such thing as sorbet. DHH even removed typescript from his frontend libraries, and this types is like kindergarten for some homemade rack/hanami/dryrb application that will server 2 people in production (creator of this stuff and random visitor). Maybe someone could tell me why we need such thing in ruby ecosystem like rbs/sorbet library. I'm really wondering. Not because ruby grants us freedom and we can do that. That's not an answer. We can do enormous count of absurd things. But the question is why?
- phoronixrly 2y agoWoah there, you're making a straw man here but I'll bite. I'll preface it with the fact that I also think types are against the very nature of Ruby as I know and practice it. But, why not choose a proprer statically typed language? Because you've inherited a mastodon of a legacy app that started simple, then matured with thick layers of features. How can you wrangle this? Oh just rewrite it in Rust. Or you could seek an iterative approach and introduce tools to help you. Like bolted-on types. Oh and I'm not sure I follow what all this about the environment is... If one is so conscious about it, maybe choose a different field entirely?
- status_quo69 2y agoOne thing that I've wondered is why sorbet didn't choose to use the stabby lambda syntax to denote function signatures? sig ->(_: MyData) { } def self.example(my_data) ... end Obviously this opens up a potential can of worms of a dynamic static type system, but it looks sufficiently close enough to just ruby. My opinion is that sorbet doesn't lean into the weirdness of ruby enough, so while it has the potential to be an amazingly productive tool, this is the same community that (mostly) embraces multiple ways of doing things for aesthetic purposes. For example you could get the default values of the lambda above to determine the types of the args by calling the lambda with dummy values and capturing via binding. Personally having written ruby/rails/c#/etc and having been on a dev productivity team myself, I say: lean into the weird shit and make a dsl for this since that's what it wants to be anyways. People will always complain, especially with ruby/rails.
- jez 2y agoI don’t know if there’s a Ruby reflection API to produce the values of default keyword arguments in a lambda, but correct me if that’s not the case. This syntax is neat enough to be worth spending some time thinking about if you can correct me.