Hacker Read top | best | new | newcomments | leaders | about | bookmarklet login

> Some languages, such as Jsonnet or Starlark, are not statically typed

Facebook's Starlark parser has some experimental support for type hints.



sort by: page size:

From https://mobile.twitter.com/brettsky/status/13643808111814819...:

> For those that voted for "static typing, strict type hinting" as a desired feature: what did you mean by this? What are type hints and all of the four major static type checkers lacking?


In case anyone ever stumbles across this thread, a couple of posts about this:

https://keleshev.com/parsing-ambiguity-type-argument-v-less-...

https://soc.me/languages/stop-using-for-generics


> And you can do that in any language, regardless of its stance on typing.

Yes and no, because nothing prevents you from passing the wrong kind of value in an untyped language unless, well, you check for the runtime type yourself in the function and reject that. And you can only do that at runtime, at which point you'll not be able to immediately see where you're passing in invalid data accidentally unless you hit this exact branch in your code. So actually just the type tagging is quite useful already.

Also, it looks like more powerful dependently-typed languages like Idris[0] (and probably also Agda and Coq) can encode this information in the type system.

[0]: https://stackoverflow.com/a/28436452


If you want better types, have you taken a look at Hack?

https://hacklang.org


> Empirical is the only system I know of that can infer types while staying static.

F#'s Type Providers can do that too.

https://docs.microsoft.com/en-us/dotnet/fsharp/tutorials/typ...


> Can you give an example or provide a citation to this? I'd love to read more.

`nom`'s `Parser` trait is implemented for functions. You can mix and match parser functions and parser types, including the built-in types that are returned when you call the post-fix combinators on the `Parser` trait.

https://docs.rs/nom/latest/nom/


CL's type system has a few warts to really do static typing well.

For a language that truly hass optional static types see Shen[1]

[1] http://shenlanguage.org/


Or maybe not.. what about mypy?

http://mypy-lang.org/

If 98% of the code has very basic types (i.e. function takes a Integer and returns a Decimal)... we could encode that in type hints with something like mypy, and get many of the type checking that statically typed languages get, without a crazy type system.

Hey I will take a 98% statically type checked language over 0%... if in so many cases I clearly know the types of stuff, let's put that info in there and detect and code time. So much of the standard library could use this to be cleaner.


> (Well, in case of these two, better to check that person has some grasp of static typing :))

http://www.erlang.org/doc/reference_manual/typespec.html

http://web.mit.edu/drscheme_v42/.share.sun4x_510/plt/doc/ts-...

Shouldn't be that hard. It's not because the language is dynamic that it doesn't support static types ;)


I think you will be interested in this submission from a few months ago: Just-In-Time Static Type Checking for Dynamic Languages, https://news.ycombinator.com/item?id=11528375

Sounds like an opportunity for a type checker that uses the language server.

If you're a fan of the ecosystem, but not of dynamic types, there are statically typed languages on BEAM, eg Gleam (https://gleam.run/)

True, but if you turn those "features" off and swap out implicit typing for explicit typing it becomes a much simpler language.

This is what I ended up doing:

https://github.com/crdoconnor/strictyaml


Static typings + type inference

Parse, don't validate https://lexi-lambda.github.io/blog/2019/11/05/parse-don-t-va...

All libs/products should be pure functions, with input output documented, making libs/products predictable

Use in-app event sourcing to reduce the need for global states states

DoD https://youtu.be/yy8jQgmhbAU In non bare-metal languages, this will be useful for readability

For errors, return instead of throw


It looks like it's only the specific hint at the bottom that requires any custom knowledge about the types themselves.

If you're interested, there were a couple more examples shown elsewhere in this thread:

http://elm-lang.org/blog/compiler-errors-for-humans (an older version it seems)

https://twitter.com/st58/status/732908457217531904

https://twitter.com/GregorySchier/status/732830868562182144

Something I've just found is that the errors can also be output in a nice machine readable format, so the suggested fixes can be read in by your editor plugin.

And finally, this is wonderful, a specific repo for code that causes error messages and a request for people to submit error messages that are confusing: https://github.com/elm-lang/error-message-catalog


> Every language phasing the web is stringly typed

Heh, not even close. Off the top of my head I can think of Ur/Web as an extreme example ( http://www.impredicative.com/ur ), and slightly more mainstream systems like Yesod ( https://www.yesodweb.com ). I've worked professionally with Haskell, although not for Web stuff. These days I mostly work with Scala, which has a similar typing mindset to ML/Haskell, but unfortunately inherits a lot of stringly typed legacy from Java. We use an in-house library that provides zero-cost newtypes to distinguish between different semantically-distinct data types, many of which just-so-happen to be representable as subsets of String (e.g. GET parameter names, GET parameter values, POST bodies, etc.). This makes it a type error to try and e.g. concatenate different sorts of data together.

W.r.t. "escaping", I tend to avoid it entirely since it's inherently unsafe:

- "Escaping" doesn't distinguish between its input and output types; they're both just "String", and we have to make assumptions about the contents of each (i.e. it's unsafe)

- Having the same input and output types makes it possible to "double-escape" by accident. This discourages the use of escaping, just-in-case it happens to be done elsewhere; hence it's very common to end up without any escaping taking place.

- Having the same input and output types makes escaping functionally unnecessary: anything we do to an escaped string could also be done to an unescaped string, so it's up to us to remember that it's needed (i.e. it's unsafe).

The whole idea of "escaping a string" betrays a flawed approach to the problem. Instead of throwing everything into the same representation, then manually trying to figure out whether or not a value comes from a particular subset of that representation or not, it's much easier and safer to avoid lumping them all together in the first place. If our inputs have a certain type (e.g. HTTP.Get.Val) and we can only output certain other types (e.g. JSON, Map[HTTP.Header.Key, HTTP.Header.Val], etc.), then the processing which turns input into output is forced to specify any necessary conversions. Whilst such conversions may involve escape sequences, having them associated to particular types is more akin to serialisation.

Heck, at my first PHP job we largely solved this problem not by 'filtering and escaping', but by modifying the PHP interpreter to distinguish between 'clean' and 'dirty' strings (with literals being clean, and $_GET, etc. being dirty). Operations like concatenation would propagate 'dirtiness', and output functions like 'echo' would crash if given a dirty string. Traditional 'escaping' functions would convert dirty strings to clean ones, and crash when given a clean string. Having this be dynamic was more annoying than ahead-of-time compile errors, but it still did a pretty good job.

There's pretty much no excuse for stringly typed languages/libraries/etc. when such such trivial solutions exist, other than the historical inertia of legacy systems.


https://googlethatforyou.com/?q=successful%20business%20buil...

You get the picture.

Also, types are a spectrum. C++ has types, Objective-C has types. Those alone have enough success stories to fill tomes! But of course you’re talking about strong static type systems like Haskell, Ocaml, Scala or ML, so you should read up on Jane Street. Or Mercury. Blockchains like Cardano.

Personally I think the onus is on you to prove that types are _not_ useful. Many of us enjoy types.


I had a go at writing a type-inferred composable language for database queries a few months ago: https://github.com/KMahoney/squee

next

Legal | privacy