Mojo🔥 Programming language | Traits(Complete Guide)- Hands-On Coding with Visual Studio Code

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi everyone welcome to Mojo today we're going to be diving into the world of traits in Mojo a powerful concept that helps you define and enforce behaviors for different types in this video I've also created coding example to help you illustrate the concept of traits easily first water traits a trait defines a set of methods that a type must Implement to be considered compatible imagine traits as contracts between different parts of your code this is similar to interfaces in jarvo or protocols in Swift next thing is why we use traits let's look at an example now we open vs code create a file Y traits. py in this example we have a duck and a cow a duck can Quack and a cow can move first we Define function make it quack we pass object as obj we use TR catch block as obj do quack control at reute error in the accept block we write a print function saying that this is not quack this is the exception at runtime as quack function is not implemented now we make a class duck to implement quack function a duck can quack this is the cow class as cow Can Moo now we call make it quack function and pass duck object now we run file hence a duck and quack now we pass cow object in this function hence quack is not implemented exception is raised but the program did not show this before running the a program in dynamically typed languages like python you can have functions that work with different types of objects as long as they have the same method names but this can lead to errors at run time if an object doesn't actually have the expected method but this is on rent time compiler did not show any warning when we were making Cal class how trait solved this problem trait solve this problem by ensuring static type checking in statically typed languages like Mojo the compiler verifies that types conform to the required traits before you even run your program this helps catch errors early on and makes your code more robust let's see how traits work with a fun example open new buntu terminal this is the folder I had made now we open this folder in V code in Windows Linux subsystem now we create a f named traits. moojo this is how we can declare trait named quable this trait has one signature of function but not its implementation each method signature must be followed by three dots to indicate that the method is unimplemented next we create some structs that conform to the quackle trait to indicate that a struct conforms to a trait include the trait name in parenthesis after the struck name you can also include multiple traits separated by commas if you're familiar with python this looks just like Python's inheritance syntax implement the quack method for duck printing quack implement the quack method for duck printing moo Define a generic function named make it quack T colon quable specifies that the generic type T must conform to the quable trait maybe a duck colon T is the function argument named maybe a duck of type t maybe a duck. quack calls the quack method on the argument leveraging the trait implementation now we Define main function for calling make it quack function for both duck and cow run Mojo file now you can see that everything is good as quack function is implemented in both structs if we change quack and cow you can see that there is error you can see the error that struck cow does not Implement all requirements if we undo this the error has gone traits enforce static type checking reducing RTI errors and making your code more reliable this helps catch errors early on and makes your code more robust thank you so much for watching please like share and subscribe
Info
Channel: Askfree
Views: 234
Rating: undefined out of 5
Keywords: mojo programming language, Mojo, Python Superset, Ai language, Artificial intelligence developers, Artificial Intelligence, Fast Ai language, Mojo Programming Language, Complete Guide, Hand-On coding in Mojo, MOJO, Data Structures in Mojo, Structures in Mojo, Python, Structs in Mojo, Traits in mojo, Traits, traits, Mojo Traits, TRAITS
Id: hFS2Rd4dNzI
Channel Id: undefined
Length: 7min 17sec (437 seconds)
Published: Sun Apr 28 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.