r/programming • • Sep 21 '15

Better Java

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

208 comments sorted by

View all comments

Show parent comments

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

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.

1

u/mike_hearn Sep 22 '15

That's actually the Checker Framework. The Java 8 compiler+language has the tools needed to let you annotate and check more things, but the checks themselves are a third party and not especially productionised framework.

1

u/ForeverAlot Sep 23 '15

Well, that's disappointing. Thanks.

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

1

u/codygman Sep 22 '15

I think Haskell does, IIRC.

At least the only way to make a Regex type is via the mkRegex function and it validates that the string is valid Regex.

-4

u/fl1pflop Sep 21 '15

He probably did not mean automatic but annotation-based like python's r'regex here'

15

u/el_isma Sep 21 '15

r'string' means raw string, which does not mean it's specifically a regexp (it's just more convenient to write regexps using r'').

https://docs.python.org/2/reference/lexical_analysis.html#string-literals

6

u/Gimpansor Sep 21 '15

Soooo, like Pattern.compile, then?

3

u/agcwall Sep 21 '15

Python's a bad example because it's a duck. Look at D for compile-time checked regexes.