4 ms·
How do you even type local variables?
by heavenlyblue 6y ago
How do you even type local variables?
- RangerScience 6y agoWhy would you need to? Edit: Like, seriously. Either the local var is populated by something coming in externally (which is then typable) or, unless your code is too complex / large, it should be easy to see everywhere it's used, and then why would you need that additional typing info?
- viraptor 6y agoCould be needed if you have a factory returning various subclasses, but you're making a call which you know will create only one type. Like `foo=open_database("mysql://...")`.
- frewsxcv 6y agoIf something is untyped in Sorbet, you can give it a type with `T.let`. So if the return value of function `foo` is untyped, but you have a high degree of confidence that it will return a `String`, you can do `ret = T.let(foo, String)`
- amw-zero 6y agoBecause when you see the benefit of type annotations (I’m not saying that’s objective, just if you do go that route) you want to add type information to as much as possible. Leaving them off because you want to is one thing. Not being able to is an unnecessary limitation.
- kgilpin 6y agoThe point is that the type of a local variable can almost always be inferred based on what it’s assigned to.
- amw-zero 6y agoWhile that’s true, that’s not what I’m talking about. I’m talking about the communicative benefit of type annotations. If you get the benefit from seeing the types, you don’t want them to be inferred. You use them as a reading tool.
- hombre_fatal 6y agoOne big use-case of types is the sanity-check that the value is what you think it is. A classic example of where I might have an inline type annotation in Rust is when I'm doing a non-trivial chain of Future/Result combinators in the middle of a function. It doesn't take much code for your understanding to desync from reality. Annotating "Result<String, IOError>" inline both documents to others what this intermediate value is but also creates better, local errors as the chain is modified. Complex stuff does generally get factored out into functions, but at the same time, it's nice when you're the one who decides when it makes sense to extract code rather than a limitation of the typing syntax. Those things don't always line up.
- rattray 6y agoI mentioned elsewhere that Sorbet (an implementation) allows inline type definitions. Its syntax for local variables is this: def foo username = T.let("heavenlyblue", String) end It's a little clunky but gets the job done, and in practice it's quite rare that you need to type a local variable. However, more important to have in the body of a program is tools for casting and asserting types, like these: T.assert_type(foo, String) T.cast(foo, String) T.must(foo) # assures the compiler foo is not nil T.unsafe(foo) # the equivalent of a TS `any` cast Docs at https://sorbet.org/docs/type-assertions https://sorbet.org/docs/type-assertions I'm not sure how tools that use RBS without inline syntax will handle these situations, but to be honest I expect the community to adopt Sorbet in practice anyway. It's very fast and battle-hardened in production at Stripe and several other large companies. Disclaimer, again: former Stripe employee.