PDA

View Full Version : Rusts option construct for null is elegant



wila
28-Nov-2020, 06:38 AM
Hi,

The request for having null support in DataFlex has come up every now and then.
I do think it would be good to have, but also see the problems with it.
For example I just had a bug report last week from my new product (vimarun) where a customer bumped into a runtime bug. Basically it boiled down to the side effect of me not taking care of a null problem...

So last night my wife asks me to explain something in rust.
She's studying that now and loves it (for good reason!)

The problem was "what is an option enum?"
See this chapter in the rust doc (read it whole) : Defining an enum
(https://doc.rust-lang.org/book/ch06-01-defining-an-enum.html)
She comes from a loosely typed language (javascript) which is even more loosely typed than DataFlex, but it has support for null.
So while she understands the null concept, the problem with null first had to be explained.
Well OK I had a bug I could show :D

The solution in rust with having option<T> is elegant and can help a lot while avoiding some of the problems with null.
Can we have this in DataFlex too please ;)

Example code on how it could look like once implemented in the compiler & runtime.


// Wont Even Compile, type mismatch!
Integer iX
Option<Integer> iY
Integer iSum

Move (iY + iY) To iSum




// Compiles
Integer iX
Option<Integer> iY
Integer iSum

if (iY.none=false) Begin
Move (iY + iY.Some) To iSum
End
Else ;
Showln 'Y has no value!'


Thanks Oy
--
Wil

Frank Cheng
9-Dec-2020, 11:56 AM
Doesn't that require actual type checking? in DF?
Also generic? Nullable in C#?
https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/nullable-value-types

I would bet that multi-threading will come before type checking.

Frank Cheng

wila
10-Dec-2020, 07:04 AM
Hello Frank,

Thanks for your reply.

I am under the impression that DAW can implement it this way without a big change to the way type checking works.
But -of course- I might be completely wrong about that.

Thanks for the C# link, reading that now.

Don't think that multi-threading will ever come to DataFlex tbh...
--
Wil