PROPOSAL: String parameters compile time validation (early beta)

Artur Biesiadowski abies at
Fri Mar 27 00:00:12 PDT 2009

Reinier Zwitserloot wrote:
> 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+");

I think that we are talking about different things. As far as I 
understood OP, he wants to have kind of typedef String which accepts 
only certain values. It is not about compile-time Patterns, which are 
just a side requirement. Main topic is to have something like


void setPhoneNumber(@Regexp("JustDigits")  String phoneNumber)

Now, you can call it from the code like


and compile would do a static check if 12345 is fitting "[0-9]+" regexp, 
but you probably would also like to do

String number = someField.getText();

which is not safe. We would need to have something like

String number = someField.getText();
@Regexp("JustDigits") String reallyNumber = 

Actually, when I think about static code checks like in the first case, 
I don't think it could be pulled out if this crosses class boundary - 
you could recompile receiver class, changing the regexp contents and the 
safety would fail (unless regexp would be included into method signature 
by contents).

Artur Biesiadowski

More information about the coin-dev mailing list