by taf2 · 8y ago · 0 comments
``` 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'
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.
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.
For those not in the know Pascal units had to declare the function (procedure) prototypes before implementing them.