General language improvements
- Elvis expressions:
If a value is nullable, it is not usable by anything that only accepts non-null values, to solve this, I added the elvis expression, which allows you to give a fallback for null values(compilation in image 2):
//int doesn't have to be nullable because we have a fallback value!
int x = functionThatReturnsNull()?: 5;
- String boxing and nullable primitives
Previously, scratcher only supported having structs, enums and function references as nullable, now it also supports having primitive types as nullable:
str? x = functionThatReturnsNullableString();
//distinguish between literal null and string null
if(x == null) looks::say("null!");
if(x == "null") looks::say("null as string!");
//the compiler achieves this by boxing the string return with a struct:
//struct StringBox(str value)
//the actual type behind the scenes is:
//StringBox? x = ...;
- Safe dot traversal
If a struct instance is nullable, you cannot use regular struct.member because that may result in a NullPointerException, to solve this I added ?. which allows for null safe traversal(compilation in image 1):
//if nullableTriangle == null, return null
//if nullableTriangle != null, return nullableTriangle.x1
int? x1 = nullableTriangle?.x1;
- Deepseek fix
I noticed that FunctionInlining completely ignored execution order, so I asked deepseek to solve it(this happened after I added string boxing).
But I found a better solution to the same problem later on, so I reverted deepseek’s fix and wrote my own, which is 260 lines compared to deepseek’s 360 lines, over 100 lines shorter!
Comments 0
No comments yet. Be the first!
Sign in to join the conversation.