-
Notifications
You must be signed in to change notification settings - Fork 65
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for Constraint on the floating point count #5081
Add support for Constraint on the floating point count #5081
Comments
The requested feature is similar to,
The proposed constraints
Usage@constraint:Int {maxDigits: 4}
type Integer int;
@constraint:Float {maxIntegerDigits: 3, maxFractionDigits: 2}
type Float float;
@constraint:Number {maxFractionDigits: 3}
type Number int|decimal|float; Additional validations
@shafreenAnfar Can you review? |
This constraint is also allowed on the built-in sub types of import ballerina/constraint;
import ballerina/log;
@constraint:Int {maxDigits: 4} // No compiler error
type Integer int:Signed8;
public function main() {
// int:Signed8 a = 1000; // Compiler error from Lang
Integer|error b = constraint:validate(1000); // No compiler error
if b is error {
// Type conversion failed due to typedesc and value mismatch.
log:printError("error occurred", b);
}
} |
Does it validate the given value type during the runtime (ex: check whether it is signed32 or signed64) or its constraint validations (ex: check it exceeds or has constraint rules max digits )? |
Yes, when we call the For example,
There are no runtime rules on the @constraint:Int {maxDigits: 4} // No compiler error
type Integer int:Signed8;
public function main() {
Integer|error b = constraint:validate(100);
// No runtime error for maxDigits constraint value
} |
Thank you @TharmiganK for the clarification. I'm wondering about the behaviour regarding the minDigits scenario[1] @constraint:Int {minDigits: 4}
type Integer int:Signed8;
public function main() {
Integer|error b = constraint:validate(100);
} may this behave like the minimum in an integer value with |
This proposal only covers the But I could understand the concern when there is a // The constraint value is not correct since `int:Signed8` cannot have values greater than 128
// Still we do not report a compiler error. This could be a compiler plugin validation
@constraint:Int {minValue: 128}
type Integer int:Signed8;
public function main() {
Integer|error b = constraint:validate(102);
// This will give an error saying validation failed for `minValue`
// But actually we should give an error saying the value for `minValue`
// is not compatible with the type
} We can create another issue to address this as an improvement |
Description:
Add support for Constraint on the floating point count in Constraint module. This may help to limit the number of a digits in a floating point number.
The text was updated successfully, but these errors were encountered: