PROPOSAL: String parameters compile time validation (early beta) (original) (raw)
Artur Biesiadowski abies at adres.pl
Fri Mar 27 00:00:12 PDT 2009
- Previous message: PROPOSAL: String parameters compile time validation (early beta)
- Next message: PROPOSAL: Language Escape Operator
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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 = /aa+aa+/; Pattern y = Pattern.compile("aa+aa+");
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
@Regexp("JustDigits","[0-9]+");
void setPhoneNumber(@Regexp("JustDigits") String phoneNumber)
Now, you can call it from the code like
obj.setPhoneNumber("12345")
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(); obj.setPhoneNumber(number);
which is not safe. We would need to have something like
String number = someField.getText(); @Regexp("JustDigits") String reallyNumber = @Regexp("JustDigits").check(number); obj.setPhoneNumber(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).
Regards, Artur Biesiadowski
- Previous message: PROPOSAL: String parameters compile time validation (early beta)
- Next message: PROPOSAL: Language Escape Operator
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]