 So I know it's been a while, but this time around we're going to be discovering both discriminated records and variant records, and that's because essentially variant records just build up on discriminant records or conversely that discriminant records are a more simplified version of variant records, but they are a essentially the same thing Let's just jump right into it, and I will cover what these would be used for as we go So to start let's just begin with the Get which language I'm using Okay, we need the name first, so let's do example is record and Then and record and then we can put something in here. So let's just do Okay This is just a basic record. I just want to show getting the value out of this first Just as a little refresher. I'm gonna need to initialize one of these first. So I don't know if I'll be up here. I can't have it just once, so we'll do this So then if we run this Okay Pretty straight forward there. Nothing really special going on. Where we add a discriminant is Through this so Let's just call it B. It can just be a care sure Forget no language again. It's not care. It's character and add a but yeah So you'll see immediately if we try to build this again That it's going to complain and that's because for all intents and purposes Anything inside of the discriminant can be considered one of the fields of the record So you can kind of view it like that Conceptually that's sort of How that works but the other thing that making it a discriminant causes is that You have to actually initialize that along with the And I have a syntax highlighting here, but along with the type No values slide for discriminant V. That's on line nine. Yes, it is. Oh I forgot about that so If you're doing a full initialization of a bailout the variable That actually has to be here and now won't complain and as you can see getting the Getting the I field out of that works exactly the same getting the V field would be the identical because like I said, it's Essentially just another one of the fields within the record. It shouldn't be considered any different as far as getting values out of it If we want to do another one of these, so let's just call this EA And then we'll do an EB And this one we're not going to initialize immediately. We're just going to describe the type as you can see This is what I had tried to do before because I forgot that the when you're fully initializing it goes into the Aggregate, but if you are not fully initializing it, you have to supply a value for the discriminant regardless and that's because for all intents and purposes the discriminant makes it so that The same type but a different discriminant are incompatible with each other so if we try to do something like this where we say EB is equal to EA Or EB is a sign the value of EA rather If you are not familiar with discriminants, it would seem like this should work because both are the type example as you can see though It does not work They are essentially different types and in a way this can be viewed kind of similar to Different child types within a class hierarchy being fundamentally compatible with each other similar idea, so we need to actually Do this legitimate or yeah fully fully assign that legitimately and Let's just Because it's already got one No, I know how that works So if I remember correctly if we try to reassign V to anything else it's going to complain Yeah, I Think you can Set it the same character, but there's a sort of better way to do it too. We set V to default Does that work? No, that doesn't work, but we can do EB V And that should pull in the same value Yeah So what what I was playing around with there real quick You want to Essentially copy the value of V from whatever it was before You can hard-code it like I had done before where I put in the V character itself That gets tedious especially trying to if you have a bunch of these and are trying to remember what was what I Really don't recommend that So you can copy that over like that or in this case just because it's such a small one You know, we're really only assigning one value within the field You can just do it through assigning the value The field directly so either one of those options work. This is pretty much it for discriminates Like I'd kind of hinted at these become useful when you need What is essentially the same type to be incompatible with each other For a certain reason Discriminates are a little bit more restricted as far as what can actually be used as a discriminant goes For example, you can't use a string and you can use unbounded strings though And the reasons for that are a little weird, but essentially you can't use any type of array There may be a few other restrictions there that I'm not aware of but I know arrays are the big one Luckily the NAT compiler is good about giving error messages about what's actually valid So if you run into that kind of issue, it's going to be very clear what the actual issue is and What is appropriate for actual use as a discriminant? And next would be the variance, so let's just tweak this into a variant I don't remember if these work Hey, I really don't remember range's work for that Opponent may not follow a variant part. Oh, yeah, so Any of the fields that will exist in every single variant have to be at the very beginning No value supplied for component B. Okay, so as it turns out you can use the ranges in there I like I said, I completely forgot if that was allowed or not but as it turns out it is and as you can see here because the The variant we supplied the capital A is you know within this range It expects that this field to be there and Because in this one again, we did a capital B that's within this range It expects this field to be there So we need to supply value for that and so while we're doing that let's create one That's actually it in this range as well. So we're gonna need a value for being this one Let's just do and so you're going to need Let me put them be there just to show what happens Just It kind of makes variant records make a little more sense if you see the error that's gonna happen Okay, it looks like I got another issue there By for discriminant. Let's be static. Oh Why did you work before but not now? It doesn't want you to possibly change it because it's a variant record now. I get it. I get it Ah So that instance what they need to do then is Okay, so this is what I was talking about for was Wanting to show you this error Component not present in subtype of example So if we see that's on line 21, that's where we try to access the B field of EC which if you remember The Discriminant provided here is a lowercase c which would mean that there's going to be an a field And no B field That's important if we change this to using an a field and Then you know, obviously of the correct type this works that's because variant records are essentially a Impossible list you don't have to have this of common fields which apply to every single variant as well as I mean, if it's a variant record you I guess arguably you could not have any of these but it'd be stupid it would just make it a discriminant record with a case structure in there that doesn't need to be there but Practically there needs to be something in here that provides some level of variance and In our case It's it's just a simple example. It's not practical by any means, but it does show what needs to be shown Based on the discriminant you're going to have either no additional field Or one of these two additional fields These are useful for Situations where you would have kind of like a very simple class hierarchy that the functions Methods if you're working with classes really wouldn't really have that much of Of a difference between these variants and it's essentially just Having some additional data tag along Any of the variants within the methods could be very easy to write I'm talking very simple class hierarchy. These may actually be a better option I do use them sometimes for that reason so Yeah, I don't really think there's anything more to show there. It's just it's a fairly simple concept Next time around we'll be getting into tagged records which forms the basis of object-oriented programming Unlike in other languages object orientation is rather broken up into discrete parts that you can Optionally take use of it's not like an all-in to object-oriented programming But you certainly can go that route for now though Hopefully you found this video helpful if it has considered giving a thumbs up and also consider subscribing Comment with questions all that kind of stuff that you pretty much already know to do but apparently these kinds of things serve as a good reminder Until the next video have a good one