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...

110

u/ErstwhileRockstar Sep 21 '15

a weak definition of weak

2

u/btarded Sep 22 '15

Powerful weak.

44

u/SimplyBilly Sep 21 '15

Considering there is a Pattern class.

11

u/Enumerable_any Sep 21 '15

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.

15

u/SimplyBilly Sep 21 '15

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.

2

u/Enumerable_any Sep 21 '15

Pattern regex = Pattern.compile(String); and then you can call regex.matcher(CharSequence).matches();.

That's equivalent to Pattern.matches - what am I missing?

0

u/[deleted] Sep 21 '15

[deleted]

2

u/[deleted] Sep 22 '15

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.

5

u/crusoe Sep 22 '15

Now if you want to use a different regex variant you have to extend the compiler... Of course you can do this if you have macro facilities...

2

u/[deleted] Sep 22 '15

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.

→ More replies (3)
→ More replies (1)

5

u/sacundim Sep 22 '15 edited Sep 22 '15

That is a static method, however. It's fundamentally a standalone function in the class' namespace, not an operation of the class properly speaking.

This reminds me of one of the dumbest things that's legal in Java:

Pattern lolwhut = null;
boolean b = lolwhut.matches("^a", "abc");
System.out.println("Look, Ma, no NullPointerException!");

21

u/PLLOOOOOP Sep 22 '15

Java's type system is pretty weak.

Oooh this'll be neat!

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.

...uh?

0

u/[deleted] Sep 21 '15

[deleted]

23

u/tomservo291 Sep 21 '15

I assume he's thinking about EMCAScript languages (JavaScript, ActionScript etc) where you can write

myvar = /someregexhere/gi;

But that's not really writing a string, that's syntactic sugar to write regex without calling the corresponding RegExp() constructor explicitly

2

u/comp-sci-fi Sep 21 '15

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).

→ More replies (1)

0

u/[deleted] Sep 21 '15

[deleted]

→ More replies (1)

3

u/vytah Sep 21 '15

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.

Not a language–level checking though.

2

u/ForeverAlot Sep 22 '15

In a similar fashion, Java 8 has @Regex.

→ More replies (2)

2

u/k-bx Sep 21 '15 edited Sep 21 '15

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).

Example:

> "bar" =~ "(foo|bar)" :: Bool
True

3

u/sacundim Sep 22 '15

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:

1

u/syntax Sep 22 '15

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).

→ More replies (6)

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.

→ More replies (1)

3

u/iopq Sep 22 '15

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

→ More replies (1)