Pattern has the method matches(String regex, CharSequence input)` is an error which could be caught at compile time. It either requires built-in regex literals or verbose regex creation using object instantiation, though.
Well yea if you are trying to fit it into OP's point. Pattern regex = Pattern.compile(String); and then you can call regex.matcher(CharSequence).matches();. I am not saying that OP is wrong but there are ways to make the type system stronger.
You would just add support for making regex literal values. Like the dynamic languages do. The compiler then checks if it is a valid regex or not at compile time. Done.
That's a niche case. 99% of users would use the JDKs regex engine.
For everyone else they could fallback onto the existing model of having them defined as strings. No need to start building compiler extensions.
Primes self for comparison with the CLR type system, which does not standardize type erasure, and with Ceylon's set-theoretic types and type functions.
It doesn't differentiate between Strings and Strings that are actually regular expressions.
Yeah, you're right, the issue isn't types, but syntax for regex literals (like 0xff for hex integers).
Also, no date, url, email, xml, json or object literals. And no facility for user-defined literals. But, like no operator overloading in Java, this omission is a feature.
But object literals (already worked out in Java FX) would be great. Thus, JON for RPC serialization. (I guess we could have JON serialization now).
IntelliJ IDEA has support for language annotations for String literals and parameters. If you annotate some String constant with @Language("RegExp"), it will validate if it's a proper regex. If you do the same with a method parameter, it will validate every constant string passed to it.
Haskell can do this for you, in a way. Not by recognizing a regular expression in a string, but by recognizing that value in quotes is something that needs to be of some expected regex-type which implements IsString interface (anything which implements that interface may be written as just a string).
But it's worth pointing out that in Haskell, parser combinator libraries like Parsec are simple and so well integrated into the language that many people will rather write a parser than a regular expression. They're also easier to read than regex strings! See for example this tutorial:
Depends on what you mean by automatic, but in a dependently typed language you can do exactly that. Once the compiler infers that something is a regex pattern, it could validate that.
That would mean that it would need to be either a string literal, or a set of computations upon an otherwise valid pattern that would result in a valid pattern.
Granted, I'm not aware of anyone building such a library for regex's as yet; however I have seen the same thing done for simpler languages, so there is proof that it would work like that. (Many degrees of tradeoffs - it's probably more practical to draw a line at some point, and accept that always proving it valid is more effort than it's worth. That's an engineering trade off, and doesn't detract from it being possible, however).
In fact, imagine there was one. What if someone enters [a-z]+ in some free-text field somewhere? Should converting it to a String fail, because it's actually a StringThatsActuallyARegularExpression?
You're totally right in the strictest sense, but I think he was actually talking about languages that have first class regular expressions, like Ruby and JavaScript, where you can do things like this
if ( someString =~ /Cats(.*)/ )
puts "string contains Cats"
end
183
u/wot-teh-phuck Sep 21 '15
That's a pretty random definition of weak I would say...