←back to thread

511 points mootrichard | 4 comments | | HN request time: 0s | source
Show context
setpatchaddress ◴[] No.23990944[source]
I'm really puzzled by the decision to use a separate file for this. The stated justification ("it doesn't require changing Ruby code") doesn't make sense, and my personal experience with languages with external type specifications is strongly negative. It's an unbelievable pain to keep multiple interface files in sync over time.

`.h` files are not something to emulate! External interfaces should be generated by tools where needed.

replies(5): >>23991001 #>>23991013 #>>23991258 #>>23991289 #>>23994203 #
rattray ◴[] No.23991258[source]
FWIW, you can use inline syntax with Sorbet[0], one of the two typecheckers that will work with the RBS format (the other being Steep, which does not have inline syntax).

Here's a full example, complete with a typo, based on the example in the blog post: https://bit.ly/3hMEMSp

Here's a truncated excerpt to get the basic idea across:

    # typed: true

    class Merchant
      extend T::Sig

      sig {returns(String)}
      attr_reader :name

      sig {returns(T::Array[Employee])}
      attr_reader :employees

      sig {params(token: String, name: String).void}
      def initialize(token, name)
        @token = token
        @name = name
      end

    end
Disclaimer, I used Sorbet while I was an employee at Stripe. I found it to be a terrific typechecker. It's also just absurdly fast (most of the time).

[0] https://sorbet.org

replies(4): >>23991351 #>>23991577 #>>23992070 #>>23995882 #
cheez ◴[] No.23991577[source]
This syntax is horrible. I'm surprised they didn't just copy Python's typing syntax.
replies(3): >>23991785 #>>23991801 #>>23991864 #
1. Trasmatta ◴[] No.23991801[source]
I believe one of their guiding principles was that they wanted all the syntax to be valid Ruby, because they did not want it to become a separate Ruby interpreter. So they were pretty limited in the syntax available to them.
replies(1): >>23992008 #
2. cheez ◴[] No.23992008[source]
I'm not sure a separate interpreter is necessary but a preprocessor could remove the notations perhaps.
replies(1): >>23992128 #
3. Trasmatta ◴[] No.23992128[source]
I believe they don't want to just strip out the annotations because Sorbet also does run time type checking. So to get all the features they wanted, they had to either write a new interpreter or use valid Ruby.
replies(1): >>23992176 #
4. cheez ◴[] No.23992176{3}[source]
OK, take non-ugly syntax, translate to ugly syntax.