4 ms·
> Rust is one of the few programming languages that correctly doesn’t treat file paths as strings. Imagine if languages allowed subtypes of strings which are n
by DannyB2 6y ago
> Rust is one of the few programming languages that correctly doesn’t treat file paths as strings.
Imagine if languages allowed subtypes of strings which are not directly assignment compatible.
HtmlString
SqlString
String
A String could be converted to HtmlString not by assignment, but through a function call, which escapes characters that the browser would recognize as markup.
Similarly a String would be converted to a SqlString via a function.
It would be difficult to accidentally mix up strings because they would be assignment incompatible without the functions that translate them.
There could be mixed "languages" within a string. Like a JSP or PHP that might contain scripting snippets, and also JavaScript and CSS snippets, each with different syntax rules and escaping conventions.
- gnarbarian 6y agoYou would probably like Java 1.4
- masklinn 6y agoThis pattern (newtyping) is a huge weakness of Java in general, and even more so older Java, and people who like newtyping are not going to like java. Because creating newtypes in Java is 1. verbose, defining a trivial wrapper takes half a dozen lines before you've even done anything 2. slow, because you're paying for the overhead of an extra allocation and pointer indirection every time, unless you jump through unreadable hoops making for even more verbose newtypes[0] It is a much more convenient (and thus frequent) pattern in languages like Haskell. Or Rust. [0] https://gist.github.com/jbgi/d6b677d084fafc641fe01f7ffd00591c https://gist.github.com/jbgi/d6b677d084fafc641fe01f7ffd00591...
- DannyB2 6y agoI use Java 14 now. Java 11 in production.
- jdc 6y agoCf. newtype in Python and Haskell.
- bruckie 6y agoSome security-sensitive libraries do this, e.g. https://www.javadoc.io/doc/com.google.common.html.types/types/1.0.2/com/google/common/html/types/SafeHtml.html https://www.javadoc.io/doc/com.google.common.html.types/type...
- torstenvl 6y agoFailing that, you could also adopt a naming convention with prefixes to indicate what sort of thing it is you're storing there: hsCode = hsFromUs(usInputBuffer); ssStoredCode = ssFromHs(hsCode); https://www.joelonsoftware.com/2005/05/11/making-wrong-code-look-wrong/ https://www.joelonsoftware.com/2005/05/11/making-wrong-code-...
- DannyB2 6y agoYes. But having the compiler enforce it is your first line of defense. If it doesn't compile, you know there is an actual problem. In modern IDEs, you see these compile errors as quickly as you type them.
- mhh__ 6y agoAllowed you to? You could do that in C++ quite happily, it's just not useful enough. To bother implementing, at least.
- akiselev 6y agoThey're not worth the effort in C++ because it doesn't have strictly enforced affine/dependent types. The GP is invisioning a language that does.
- ori_b 6y agoWhy do you need them to enforce that only escaped strings are passed to functions? html::append(html::string text); with an constructor html::string(std::string) that handled escaping seems like it'd work just fine.
- eska 6y agoIt's absolutely useful enough, it's just that it's awful in C++ due to language limitations as opposed to other languages such as Haskell, where it is standard.
- gpderetta 6y agoHow would be awful in c++? It seems trivial to do, basic_string is already templated and distinct instantiations are not mutually compatible by default. In fact wstring, u8string, u16string, u32string exist today in the language simply as distinct instatiantions of basic_string. You can crate your own by picking a new char type. Algorithms can be and are, generic and work on any string type.
- mika9090 6y agoTry Pascal (free pascal or Delphi)
- DannyB2 6y agoI used Pascal for the 80's and part of the 90's. Currently use Java. I almost tried Delphi, but my shop moved on to something else between Pascal and Java.
- robocat 6y agoAFAIK they just provide type name aliases, which do not enforce or warn of you if you mix the “types”.
- benibela 6y agoThey have changed it Now the string types have an encoding and the string themselves, too. When you assign a string to a string variable with a type of a different encoding, the string is automatically converted. But it is causing a huge mess. Especially with existing code. When you have a library using utf-8 and one library using the default codepage, that is not valid anymore. Although you can manually override the encoding for each string, so any string might have any encoding regardless of its type.
- benibela 6y agoHere is an example of the mess: I have a benchmark of various maps in freepascal. The benchmark creates strings of random bytes to use as keys. A classic key-value store is the sorted TStringList. Now the benchmark of the TStringList fails. Apparently, because it now assumes the keys are valid utf-8 when using the utf-8 codepage as default codepage. The default codepage can be changed. When I start the benchmark with LANG=C .. it works with the random byte keys. On Windows, the default codepage is usually latin1, so it would work there, too.
- jkoudys 6y agoNot quite at that level, but rust does have OsStrings (managed the same way as the OS, often but not always utf8), and CStrings (basically just byte buffers - just like c likes). There are special rules around inclusion of nulls and null terminators. It'll give the benefits of the behaviour you mentioned - not allowing an invalid string type for a function call. The sqlx crate for rust also has a macro called query!, which (at compile time) validates the SQL and created a value of type "record". Similar idea there, since you'll get early exceptions thrown by the compiler if you write sql with errors in it.
- yencabulator 6y agoGo is like that. Not the "mixed within" part, though html/template's AST understands the context where you're using a value and escapes it differently. For example, https://golang.org/pkg/html/template/#HTML https://golang.org/pkg/html/template/#HTML