‫Welcome back.
‫In this video we are going to look into nobles.
‫And nobles are a concept that you see in other programming languages.
‫For example, Swift uses them quite extensively and there they are called options.
‫So they are variables that can have a value or they don't have a value.
‫So there are two options, right?
‫So either something has a value or does not have a value, and usually that's null or not.
‫So let's create some.
‫And by the way, it's a data type, so the data type can be empty or not.
‫And usually that's like Nullarbor as you can see here and Nullarbor of type int 32 for example.
‫So that's generally how you write, how you would write it.
‫That would be a Nullarbor in 32.
‫So evaluated can have values from -2 billion roughly up to plus 2 billion.
‫So let's go ahead and create them.
‫So in order to create a novel, you just add a question mark after the data type.
‫So if you want to have a integer Nullarbor as I've just written, you will just use end question mark
‫and I'm just going to call that number one and it's going to be null.
‫Let's create another one that I'm calling NOM two and set that to NULL.
‫And as you can see, we get an error here so it's complaining it says cannot convert null to int because
‫it is not or a non unknowable value type.
‫So you cannot assign null to an integer.
‫That's what you say C here, but you can assign null to a global integer, which we've done in line
‫nine.
‫All right, so let's create another one and it will be num two and it will be 1337 1337.
‫That's how you would go ahead and create an elbow.
‫When you see it's either null or it has a value.
‫The same goes for doubles.
‫So double question mark.
‫Number three is new double and question mark like that.
‫So that's also how you can assign a multiple double.
‫So this one will be empty or here num four is 3.14157.
‫So that's PI.
‫And the same goes for Booleans.
‫So it's a multiple bool and I'm going to say Boo Vol is a new bool
‫question mark like that.
‫So now let's check it out.
‫Let's just display that on the screen and see what we get.
‫So see w our global numbers are and we're going to use zero one.
‫Two and three.
‫And that will be number one.
‫Numb to numb.
‫Three and four.
‫And now, of course, it's complaining because I need to end my string here.
‫All right.
‫And let's also display our bool.
‫So c w the multiple boolean value is and bool vol.
‫Now okeydokey.
‫So let's try that control f five.
‫And our knowable numbers are, as you can see, nothing.
‫So it's not even an empty space.
‫It's just nothing.
‫One, three, three, seven, nothing and 31415, seven.
‫And the null boolean value is nothing.
‫So as you can see, it can be nothing and it's not complaining.
‫So even though there is no value in there, it's still fine.
‫It's not crashing our program or stuff like that.
‫If you would try to do the same thing with an integer which is not knowable, so let's create an integer
‫called number five and let's just do it like that.
‫So we didn't initialize it and it's also empty, right?
‫So there is no value in there if I try to access that one like that.
‫And I'm going to use number five here.
‫Let's check that out.
‫Control the five.
‫And as you can see, we have build errors so we can't even stop that because it says use of unassigned
‫local variable number five.
‫So that's the whole point about that.
‫You create a variable that can be empty or it cannot be empty and that's totally fine with the program.
‫It won't crash.
‫And why would you use that?
‫What would be the idea behind Noble's?
‫Well, imagine you have a program which works with loads of data, so it has user data and it gets it
‫from the database.
‫And for some values or for some columns, there are simply no entries.
‫So let's say we have usernames.
‫We have personal name or first name, last name, then we have birthday and so forth.
‫All of those information can be stored within our database, but for most users we only have the username.
‫We don't have their first name, last name.
‫We don't have the password or email address, nothing like that.
‫We only have the username but we still want our program to work correctly.
‫So we will just use nobles which are fine.
‫If there is no value.
‫They will not make our program crash.
‫So let's make it very, very generic example of that.
‫And I'm going to use a bool here, which will be a multiple bool and I'm going to call it Ismael and
‫I'm going to set it to NULL.
‫And in here I'm going to just check is mail, is it null or actually is it true?
‫So if it's true, let's write something like user is mail then else if it's mail is false, we're going
‫to say something like user is female.
‫And then finally else we just say something like no gender chosen or maybe something like no gender
‫could be found or something like that, and that we are.
‫So now you can go ahead and use that kind of approach.
‫Right.
‫And here I'm going to put it in the next line.
‫And if we run that well, of course, we get no gender chosen.
‫And in my case, my program will shut down straight away after it says that.
‫And I still have.
‫An error somewhere and.
‫Well, how would I figure it out?
‫Well, it's this one here.
‫So I have this for still in my text and it's trying to access it.
‫But there is something missing, so that's why it doesn't work.
‫All right, now we can try it again.
‫Control F five and we are our knowable numbers are.
‫That's totally fine.
‫And then here, no gender chosen.
‫All right.
‫That's all good and stuff.
‫But what if you want to convert a gullible into a non gullible type?
‫Well, let's go ahead and try that.
‫Let's create a gullible double here.
‫Let's call it number five and it's going to be 13.1 and then double or actually let's call it number
‫six, because we have this number five here, which we never used, but I'm just going to leave it there.
‫It's not hurting anyone.
‫So number seven is going to be null.
‫So how do I convert those into a non knowledgeable type and I'm going to create a NUM eight which will
‫be a non knowable double.
‫So it's a general double, right.
‫It can't be null or we cannot assign null to it.
‫And now we just check if num six equals null then num eight will be 0.0.
‫And else we're just going to say num eight should be num num six six.
‫But there we have the problem because as you can see that we entered that I cannot just assign it it
‫says cannot implicitly convert type double question mark to double and explicit conversion exists.
‫So are you missing a cast?
‫And that's what we need to do.
‫So we need to cast it into a double because we need we know if it's not now, then num six internally
‫is a double, so it contains a double value.
‫So I'm just going to use that and assign that to NUM eight so we can just go ahead and display that
‫value of NUM eight is num eight.
‫So let's start out with control f five.
‫And as you can see, value of NUM eight is 13.1.
‫So the casting, the explicit conversion worked.
‫There is, however, a faster way to do that.
‫So as you can see, it was quite some effort.
‫We needed this.
‫If an LS there is the null coalescing operator that we can use for that.
‫So we can just say num eight is equal to num six.
‫Question mark.
‫Question mark 8.53 for example.
‫So if num six is empty, then go ahead and add or enter 8.53 and if not, then do something else.
‫So what I'm going to do is I'm going to add another line and I'm going to write something like value
‫of num eight is and actually let's just copy that because we need that twice and I don't want to type
‫so much so that we are now let's go ahead and try that again.
‫Let's see the difference.
‫And as you can see, value of NUM eight is 13.1 value of num eight is 13.1 and value of NUM eight is
‫8.53.
‫So what happened here?
‫What was ignored here in line 50 because it said well, it was well, I assigned 8.53 to it.
‫But as it seems now, I'm six.
‫Had a value and it was taken.
‫So as six had a value, just took a 13.1 and as number seven didn't have a value, num eight now is
‫8.53 because if we look at it, num seven is null, so we assign a different value to it.
‫So that's called I'm just going to.
‫Copy it over for you.
‫The null collapsing operator.
‫Double question mark.
‫So if you want to convert from a normal to a non movable type, then you can use the null coalescing
‫operator.