Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I fully agree that the types offered by the programming language (wrapping the underlying types) are not enough. However, a static type language allows you to define your own types. So (as your sibling comment by brundolf suggests) you can create your own types around the existing types such as HtmlEscapedString and UntrustedUserEnteredString. The syntax is particularly convenient in Rust, but you can do the same in e.g. Java, or any other statically typed language, albeit more verbosely:

    class HtmlSnippet {
        public String html;
        ....
    }

    void setWidgetContents(HtmlSnippet s) { .. }

    void foo() {
        // Compile error
        setWidgetContents(request.getParam("foo"));
    }


This is one of those "Turing tar-pit" scenarios. As you point out, it's technically possible, but in most languages, it would drastically increase the verbosity of the code.

None of the built-in control structures will work with your custom types. In Java, for example, "int" is a primitive, and "Integer" is final. All the built-in functionality that works with numeric types won't work with your types. All of the standard and third-party libraries expect the built-in types, too. Java doesn't support operator overloading, so you wouldn't be able to + your numbers, either. Instead of helping you with every arithmetic operation, it would be a massive pain point at every arithmetic operation.

Strings and integers are fundamental types. Programming languages just aren't designed to let people substitute their own.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: