4 ms·
This looks really nice... i wish it was just part of the language to be able to write ruby with type information e.g. instead of having a separate more verbose
by taf2 8y ago
This looks really nice... i wish it was just part of the language to be able to write ruby with type information e.g. instead of having a separate more verbose sig method that's called before each method signature...
```
class Foo
extend T::Helpers
sig(
# Both positional and named parameters are referred to by name.
# You must declare all parameters (and the return value below).
foobar: Integer,
widget: String
)
.returns(Symbol)
def foo(foobar, widget: nil)
(foobar.to_s + widget).to_sym
end
end
```
I'd love it if we could write this as :
```
class Foo
extend T::Helpers
def foo(foobar:Integer, widget:String:nil):Symbol
(foobar.to_s + widget).to_sym
end
end
```
Not sure the best way to handle default values... e.g. widget:String:nil is kind of awkward... but still i'd be better this way then the additional method call 'sig'
- unfunco 8y agoThe second version would be difficult, maybe impossible currently due to keyword arguments, since: def foo(foobar:Integer) end is already valid, and I think the lexer might struggle with the ambiguity; but I'm not 100% certain. Edit: I seemingly missed the last line of your comment where you state something similar.
- tsycho 8y agoIf foo:SomeType is problematic, let’s just use foo::SomeType instead. I agree with the parent that inline types are much nicer and more intuitive than he sig based approach.
- spitfire 8y agoFor some reason that reminds me of Pascals interface/implementation split... For those not in the know Pascal units had to declare the function (procedure) prototypes before implementing them.
- ptarjan 8y ago(one of the authors here) We'd love that too! We're chatting with the Ruby folks to see if anything like that is feasible. We experimented with lots of other syntaxes (comments, yarddoc, .rbi files, monkey-patching stdlib classes to be callable) and the `sig` syntax seems to be the easiest for folks to use inside Stripe. After writing sigs for the past few months, I don't dislike it as much as I thought I would. You get the syntax highlighting and autocomplete of your editor, while `sig` and `T` are short enough to not feel like too much boilerplate. We're very open to other suggestions if you have any for other syntax suggestions. Just email us at sorbet@stripe.com.
- transfire 8y agoTomdoc?
- faitswulff 8y agoI don't know, as a long-time Rubyist, I actually prefer Sorbet's fluent interface with `sig(...).return(...)` to these (albeit more typical) type annotations. Reminds me of RSpec.