From the course: Learning Julia

Unlock the full course today

Join today to access over 22,400 courses taught by industry experts or purchase this course individually.

Custom types

Custom types - Julia Tutorial

From the course: Learning Julia

Start my 1-month free trial

Custom types

- Although Julia doesn't support creating classes like other object-oriented languages, you can create custom types. And we'll examine how to do that in this example. So let's go ahead and open up the types start file. The struct keyword is used to define a new type. So to create a new custom type called MyType I can write some code like this. So use struct and then call it MyType and then I can give it some fields. So I'll give it a field1 and I'll define that to be an integer. And then field2 and I'll make that one a string. And you get the idea. And then use the end keyword to close off the struct. So to instantiate this new type, I can use it like a constructor and pass values for the fields. So I can make a variable named x and I'll just instantiate my new type and I'll give it some values like 10 and just a sample string. And then I can print my variable to the stand it out and I can access fields using dot notations so I can write x.field1. Alright so, let's go ahead and run…

Contents