構造体

Julia中級

Anthony Markham

Quantitative Developer

構造体 - 導入

  • 構造体は複合型の一種です。
  • 任意の数のフィールドを持てます。
  • 複合型から、その型のオブジェクト(実体)を作れます。
Julia中級

構造体 - 構文

  • struct キーワードで構造体を定義します。
  • 各フィールドは struct 宣言内にインデントして書きます。
struct Person
    age
    height
    location
end
steve = Person(18, 180, "London")
println(steve.height)
180
Julia中級

構造体 - オブジェクトの型

  • steve の型は Person です。
struct Person
    age
    height
    location
end
steve = Person(18, 180, "London")
println(typeof(steve))
Person
Julia中級

構造体 - イミュータブルの例

  • 構造体はデフォルトでイミュータブルです。
# Steve の誕生日!
steve = Person(18, 180, "London")
steve.age = 19
setfield!: immutable struct of type Person cannot be changed
Julia中級

構造体 - ミュータブル構造体

  • mutable キーワードでミュータブルな構造体を作れます。
mutable struct Person
    age
    height
    location
end
steve = Person(18, 180, "London")
steve.age = 19
println(steve)
Person(19, 180, "London")
Julia中級

構造体 - 型付き構造体

  • 各フィールドにデータ型を指定できます。
mutable struct Person
    age::Int64
    height::Int64
    location::String
end
steve = Person(18.5, 180, "London")
InexactError: Int64(18.5)
Julia中級

練習しましょう!

Julia中級

Preparing Video For Download...