title: Julialang: Enforcing interfaces on Abstract Types
body: <p>I have been trying to understand the type system for <code>Julialang</code> but some design aspects are still confusing me. I was hoping someone could clarify. </p>
  
  <p>So the question here is about Abstract Types and their concrete implementations. From what I <a href="http://www.stochasticlifestyle.com/type-dispatch-design-post-object-oriented-programming-julia/" rel="nofollow noreferrer">understand</a> <code>Julia</code> Abstract types do not enforce any constraints on their concrete implementations. So there is no guarantee that a method that works on the Abstract type will work on a concrete implementation of that type. </p>
  
  <p>I get that Julia does not use classes or follow inheritance. But I just want to avoid generating all sorts of bugs in my code. If there is a different design paradigm, then could someone please answer question 2 below. </p>
  
  <p>So I have 2 questions.</p>
  
  <ol>
  <li><p>Is this still the way that the language works? Just to confirm nothing has changed since the blog post. </p></li>
  <li><p>How do users design their software around this seeming vulnerability?</p></li>
  </ol>
  
  <p>And example of the issue from the linked post:</p>
  
  <pre><code>abstract type AbstractPerson end
  abstract type AbstractStudent &lt;: AbstractPerson end
  abstract type AbstractTeacher &lt;: AbstractPerson end
  
  struct Person &lt;: AbstractPerson
    name::String    
  end
  
  struct Student &lt;: AbstractStudent
    name::String  
    grade::Int
    hobby::String
  end
  
  struct MusicStudent &lt;: AbstractStudent
    grade::Int
  end
  </code></pre>
  
  <p>Now if I create some methods on the abstract type.</p>
  
  <pre><code>get_name(x::AbstractPerson) = x.name
  p1 = Person("elroy")
  get_name(p1)
  
  &gt;"elroy"
  </code></pre>
  
  <p>So even if <code>MusicStudent</code> is a subtype of <code>AbstractPerson</code>, the <code>MusicStudent</code> DOES NOT have a <code>name</code> attribute. That means that to following behavior is observed.</p>
  
  <pre><code>m1 = MusicStudent(10)
  get_name(m1)
  
  
  ERROR: type MusicStudent has no field name
  
  Stacktrace:
   [1] getproperty(::Any, ::Symbol) at ./sysimg.jl:18
   [2] get_name(::MusicStudent) at ./In[2]:1
   [3] top-level scope at In[13]:2
  </code></pre>
  
  <p>So the problem here is that <code>Julia</code> allows me to instantiate the type variable <code>m1</code> with essentially an incomplete constructor. And it only gives me an error when I try to run that function.</p>
  
  <p>So that means if I write a function for the Abstract Type, I can't guarantee every concrete implementation of that type has the same interface. That seems like it will make very fragile code as the developer won't know which types implement which attributes and methods. </p>