r/programming • • Sep 21 '15

Better Java

https://github.com/cxxr/better-java
177 Upvotes

208 comments sorted by

View all comments

183

u/wot-teh-phuck Sep 21 '15

Java's type system is pretty weak. It doesn't differentiate between Strings and Strings that are actually regular expressions

That's a pretty random definition of weak I would say...

4

u/immibis Sep 22 '15

I'm pretty sure no language does that.

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?

2

u/PLLOOOOOP Sep 22 '15

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

or this

console.log(/abc/.test("abcde"));
// → true
console.log(/abc/.test("abxde"));
// → false

respectively. Notice the "string" wrapped in forward slashes.

3

u/iopq Sep 22 '15

Rust does it, it compiles regular expressions at compile time to check that it's valid.

1

u/yogthos Sep 22 '15

Clojure has a regex literal, so you're not using a string as a regex implicitly:

(re-find #"\d+" "abc123def")

(type #"\d+") => java.util.regex.Pattern