67 Replies
This will throws some exception that is similar to
NullReferenceException
The reason why is that in line 16, the
find
method is returning a Nil
(equivalent to null
), passing it to the Subscription
class and then in the create!
method, we are acessing the name attribute on it (person.name
) and that is what throwing the exception
The line that introcued the Nil
(i.e. line 16) is not in the stack trace !what's your point ?
it's not c#
yeah it's not C#, coming from a Ruby background
I am wondering whether C# fully eliminates these kind of problems
I've tried to play with it a little bit
the thing is that if the compiler detects something can be null, the type is Nullable, and thus we cannot call whatever method on it
is there other cases where we fall into the same problem as ruby ?
the compiler detect if that can be nullable but you still can use it
it's not a compiler error
C# still has a
NullReferenceException
exception which is thrown if you try to access anything on null
. There are technically no mechanisms which prevent you from doing this, although in more recently language versions you can enable a set of warnings about null.Thinker
REPL Result: Failure
Exception: NullReferenceException
Compile: 430.573ms | Execution: 54.339ms | React with ā to remove this embed.
So the answer is that C# doesn't eliminate null problems but you can be pretty safe
yeah but at least I am getting a warning from the compiler telling me that I am possibly derefrencing a
null
refrence right ?
Is the compiler always warning about such cases ?Yeah, and you can even turn that into an error if you want to
Usually, yes, if you annotate your code correctly. There are cases however when the compiler is either overly restrictive or lacks sufficient information, so it can be wrong sometimes.
And it only works for libraries that were written with nullable annotations
So if using an older library, that might not be the case
And you can turn it off completely
that remind me of my project in webform where the compiler put "that can't be null" everwhere on nullcheck cause there is 0 nullable
yeah
I was using ANTLR4 a while back and that has no nullable annotations yet everything can and usually is null
you mean this
<Nullable>enable</Nullable>
?Thats one way of doing it, yes
but even with that, you still need to annotate your types
ie, returning
T?
instead of T
from a method that can return null
accepting T?
instead of T
if your method can accept null values
etc etc etchum meaning that if in a method if I use return type
T
whereas in reality it can be T?
that would not issue a compiler error, right ??
Yes, it certainly would
If you mark it as
T
, but you return a T?
or null
, that will be warningsyup but that warning will be located in that method. Everywhere else where the method is used, I will not get warning about dealing with a null return right ?
uhm, no
So there is a risk of having a
NullRefException
with no warnings what so everthats not how it works
or well, sure, if you ignore that warning
because if that method comes from a file/assembly that is flagged as #nullable enable or
<Nullable>enable</Nullable>
, its assumed that it will never return null
because thats what the type system says about it
this only happens if you lie to the compiler and ignore the warning thou
and its not really reasonable to expect the compiler to handle that, is it?haha what a coincidence, I was writing a code about persons too
for example, this method. in the first picture, we get a warning because it promises to return a
Person
, but we explicitly return null. that triggers the warninghere it is
in the second image we supress the warning by saying "trust me, this value is never null"
aka the null-forgiving operator
why you allow age to be null ?
yeah that code screams bad design
you should not take in a nullable parameter if your code cant handle the null
taking in
T? value
then throwing when value is null is badit's jsut a toy example here, to show that the warning is only located in the
Create
static methodright, because that method is breaking the contract
it promises to return a person
but in reality it can return null
and since I am lying to the compiler and telling that it returns
Person
wehars in reality it' s Person?
, it doesn' t help me laterthats the same behaviour you get if you disable nullable reference types
correct
this is why in "real" code, its very common to elevate these warnings to errors
wheras if I am honest
exactly
the compiler will help me prevent the nullRefExceptions with warnings
you've discovered why this exists
:p
as a fun experiment, try implementing this in a language that truly doesnt have null
is None allowed ?
Option<T> is absolutely fine
but trying to return
None
from a method that returns T
...
that aint gonna work chief
C# has had null for 20 years, nullable reference types are just a way to help us work with it better.
its not a true "removal" of null
if C# was re-designed from scratch today, we probably wouldn't have nullwhat language do you suggest ?
Rust
Okay, so to summary all the things, The nullable reference type does not remove the
NullRefrenceException
, it's just a way to help the compiler so that it can prevent you where we may have such issues.
The limits is that :
1. This assumes that you correctly help the compiler
2. The Nullable option is enabled
How the 2. is related to old libs. If I use an old lib that does not have the Nullables, i will not get the warnigs right ?correct
because nullable was introuced in C#8
or hm
I think thats how it works
its either that or it assumes all types are T?
since thats closer to the truth
That would be ridiculously annoying though, it assumes nothing is nullable
easy enough to test
make two libs, one with nullable on one with off
What I am struggeling to understand, is that I have a bad mental model for what a library exactly is š¦
an assembly
Have you ever installed a Nuget package?
Even though I am using an external lib, It's not my config of my main project (nullable enabled or not) that gets taken into account
I did, but I didn't look under the hood how it works
when you create a project you can choose library instead of console application
that means it's already compiled ?
so when I compile my project, it doesn' t get recompiled right ?
wat
wat
When you compile your project, it gets turned into a DLL file. When you recompile, that DLL is regenerated.
ok, but what about the lib, if a lib is an assembley it means that it's already compiled when I import it as a nuget
yep
Installing a Nuget package is basically downloading a glorified DLL file containing code which the .NET runtime understands.
no warnings
but will throw NRE
so when using libraries where its disabled, you assume no nulls
very dangerous
I hope no one will enable that to publish a library xD
no but there might be legacy libraries
okay folks, thanks for your time, I understand how it works now
š
use
/close