Why static analysis is the way to go
monkyyy
crazymonkyyy at gmail.com
Sat May 30 06:14:25 UTC 2026
On Saturday, 30 May 2026 at 03:45:46 UTC, Richard (Rikki) Andrew
Cattermole wrote:
> On 30/05/2026 3:18 PM, monkyyy wrote:
>> On Saturday, 30 May 2026 at 03:02:37 UTC, Richard (Rikki)
>> Andrew Cattermole wrote:
>>>
>>> There are somethings that we can't do in our type system,
>>
>> Got an example of a useful thing?
>
> Anything effect based.
>
> I.e. value type exceptions.
a) exceptions airnt useful
b) function coloring is bad
c) with a ct counter, which I have 2 methods of making, you can
make a nice open-sumtype, throw an opensumtype into an exception
and you can grab that data in the catch block; you can just do
this:
```d
import std;
import core.stdc.stdlib;
struct tuple(T...){
T me; alias me this;
}
struct opensumtype{
enum counter=cast(immutable(void)*)[0].ptr;
static auto getcount()=>(*(cast(int*)counter));
static auto count()=>(*(cast(int*)counter))++;
enum mytypeid(T...)=count();
void* where;
int what=mytypeid!null;
this(T...)(T args){
where=malloc(tuple!T.sizeof);
alias S=tuple!T;
*cast(S*)where=args;
what=mytypeid!T;
}
ref tuple!T get(T...)(){
assert(has!T);
return *cast(typeof(return)*)where;
}
bool has(T...)()=>what==mytypeid!T;
}
unittest{
opensumtype foo;
foo=opensumtype(3);
assert(foo.has!int);
assert(foo.get!int[0]==3);
foo=opensumtype(3.14,"hello",1000);
assert(foo.get!(double,string,int)==tuple!(double,string,int)(3.14,"hello",1000));
}
class valueexception : Exception{
enum counter=cast(immutable(void)*)[0].ptr;
static auto getcount()=>(*(cast(int*)counter));
static auto countup()=>(*(cast(int*)counter))++;
static auto countdown()=>(*(cast(int*)counter))--;
opensumtype values;
static void areexceptionshandled(string file = __FILE__,
size_t line = __LINE__){
assert(getcount()==0,"some exceptions are not handled");
}
void handled(string file = __FILE__, size_t line =
__LINE__)(){
enum __=countdown();
}
this(string file = __FILE__, size_t line =
__LINE__,T...)(string msg,T args){
enum __=countup();
values=opensumtype(args);
super(msg,file,line,null);
}
}
void fileio1(string s){
if(s=="rm -rf *"){
throw new valueexception("attempted deleted harddrive",">:(");
}}
unittest{
try{
fileio1("rm -rf *");
} catch (valueexception e){
e.values.get!string[0].writeln;
e.handled();
}
}
struct importantcustomeerdata{
string name="john";
int balence=500;
bool onfbiwatchlist=true;
}
enum transactionstatus{pending,done}
void fileio2(string s,importantcustomeerdata data){
if(s=="rm -rf *"){
throw new valueexception("attempted deleted
harddrive",data,transactionstatus.pending);
}}
unittest{
try{
fileio2("rm -rf *",importantcustomeerdata("mary"));
} catch (valueexception e){
assert(e.values.has!(importantcustomeerdata,transactionstatus));
e.values.get!(importantcustomeerdata,transactionstatus).writeln;
//e.handled();
}}
unittest{
valueexception.areexceptionshandled();
}
```
d) (it just be better to throw any "important data" into a file
tho; now you have two problems)
More information about the Digitalmars-d
mailing list