PROPOSAL: String parameters compile time validation (early beta)
Reinier Zwitserloot
reinier at zwitserloot.com
Thu Mar 26 15:55:40 PDT 2009
I don't think the idea was to make a hierarchical tree of regexp
types. There's just 1 regexp type: Pattern. The one we already have.
The idea is simply to have a literal syntax for regexps, where the
compiler will actually put the binary representation of a compiled
pattern in the .class source. As I mentioned, this has very
significant speed savings, because compiling a regexp is more
expensive than applying one, especially for complex regexps that are
performance sensitive.
I don't understand your argument about casting. Right now, you get
compile-time checking on e.g. the bounds of an integer, but if you
cast, you get that check at runtime (or, actually, the bits are just
cut off, but you get the point).
The regexp literal notation would literally have the type 'Pattern'.
You can try to cast a string to this, but that would obviously fail,
and the compiler will in fact error when you try, because String is
not a subtype of Pattern. If you want to turn a string into a pattern,
you'd use Pattern.compile. So:
Pattern x = /a*a+a*a+/;
Pattern y = Pattern.compile("a*a+a*a+");
simple enough.
--Reinier Zwitserloot
On Mar 26, 2009, at 22:19, Artur Biesiadowski wrote:
> Marek Kozieł wrote:
>> Validation by regexp-s would be only at compile time (efficiency).
>>
>> Target is quite clear, but anyone see any problems?
>>
>
> As somebody else said, it might be a part of the JSR 305. But just to
> give you example complications:
>
> can (a{1-3}) be passed to (a+) ? (a|b) passed to (b|a) ? [0-9][a-z]
> passed to [:digit:][:letter:] ?
>
> It might be a nice project for somebody's thesis to work on
> equivalence/superset/subset relationships between regexp (especially
> fun
> if you take backreferences into account), but I would not put it into
> java type system...
>
> And no, validation only on compile time doesn't make any sense. You
> would need some kind of cast operator on regexp, which would cast
> free-form string into regexp-bound string (and throw exception
> otherwise) - similar to way Class.cast works.
>
> Regards,
> Artur Biesiadowski
>
More information about the coin-dev
mailing list