harrisonlabollita / MagShapeAni.jl

Calculate the magnetic shape anisotropy for a given lattice and physical constants
0 stars 0 forks source link

Why use abstract type for composite types #1

Open lanceXwq opened 3 years ago

lanceXwq commented 3 years ago

Hello Harry,

Just spent some time browsing your code, awesome work! Here are a couple of questions of mine.

  1. When struct is called, it defines a composite type, correct? So I wonder why you also define an abstract type for each one of them.
  2. If abstract types are not necessary, instead of something like
    struct Physical <: PhysicalConstants
    a::Float64
    moment::Float64
    end

    how about

    struct Physical{T<:AbstractFloat}
    a::T
    moment::T
    end

    which may have better performance.

harrisonlabollita commented 3 years ago

That’s a good point. I was following along another example so that’s why I wrote it that why. I feel like by doing what you suggested you would be eliminating one level of abstraction that Julia has to do at compile time, which could speed things up.

I will give it a try. Funny that you sent this today, because I’ve been watching a few talks on Julia today. JuliaCon is soon, so hopefully there will be some interesting things to check out then

lanceXwq commented 3 years ago

I guess an abstract type makes more sense if it contains multiple composite types, just like your AbstractUnitCell.

I'm also working on some code in Julia so I'm now "nitpicky" about types and performance.

This is also my practice of using Github since my lab now has an account. Probably I'll also submit a couple of pull requests, please don't take them too seriously, and you can close this issue any time you like. :)