Concept: Faultable types

I propose the concept of Faultable type - modification of types by analogy with nullable. The bottom line is that this type can take on a special meaning if something went wrong during the execution of the program: the required file was not found, division by 0 occurred, etc. This type is designed to deal with errors as close to where they occur, as opposed to exceptions, which are more suited to handling errors centrally. In order not to switch the layout 100,500 times, I will call it "erroneous type", no matter how funny it sounds.







Announcement



Any variable or function argument can be declared erroneous.







faultable int number;
faultable string fileName;
faultable Customer user;
void markCustomer (faultable string name){
 ...
}
      
      





The return value of the function becomes erroneous in fact, if in any of the branches there is a return of an error or an erroneous value.

Automatic type is considered unerring unless otherwise stated.







Assigning an error



The main feature for which this type is introduced is the ability to assign a special meaning to it - the meaning of an error using a special keyword. The error value can be accompanied by a description or error class.







number = fault;
filename = fault("  ");
return(fault(EOleException));
      
      





Assignments to normal type



Unlike nullable types, an erroneous type can directly pass a value into variables of an unerring type and vice versa:







faultable int selNumber = 255;
...
int outNumber = selNumber;
      
      





But when trying to assign an error to an unerring value, the compiler must exit the function, returning an error:







faultable int selNumber = fault;
...
int outNumber = selNumber;//   
      
      





An error should be thrown if the error returned by the function is not assigned to anything







int parseMyDate(string strDate)
{
    ...
    return(fault);
}
...
parseMyDate("This is not a date");//
      
      





, , , , , :







faultable Customer payer;
...
payer=fault("Customer not found");
...
faultable string name = customer.name;//    
      
      





, , try...catch. .









, .







name=customer.name nofault;//   ,     name   
      
      





,







name=customer.name nofault "unknown";
      
      





, :







var file=openFile(path) nofault(EFileNotExists) createFile(path);
      
      











if (!variable.isFault()){
    outVar = variable;
}
else
{
    ...
}
      
      









, amount = struct.getField("amount").toInteger() nofault 0;



,







try
{
    amount = struct.getField("amount").toInteger();
}
catch
{
    amount = 0;
}
      
      







, . , , . .









, . catch/throw, ยซ ยป.









, .











, , . , - . , . , , , โ€” -, โ€” , . , , .









, . โ€” . , , .








All Articles