> I'm a new scientific user and Julia is an exciting prospect. I read through
> the manual on types and methods, but got confused about how I can define a
> new abstract type (= trait according to some of the literature)
Yep, that's all there is to defining one:
> abstract AbsType
Except, often you make them a subtype of something, otherwise they are
implicitly a subtype of Any, the supertype of all types. Abstract types
are quite abstract in the sense that they don't really do anything apart
from sitting in the type tree unless you specify some methods to go with
them.
> and write methods for it such as
>
> function func2(x::AbsType)
> func1(x) + func1(x)
> end
>
> I can trust that whatever object that has been passed being of a concrete
> type
>
> ConcType <: AbsType
> attr::Number
> end
>
> should always come with the completely necessary method func1(x::ConcType)?
Only if you defined one. Otherwise you can trust that there will be a
runtime error if you try and call it.
> When I tried this (run all the codeblocks above) without defining func1, I
> got no failures until I called func2(foo) where foo=ConcType(25813).
There is no static checking in Julia. Thus there are only runtime
errors (assuming the syntax is correct) (I think).
> If I instead defined a "fallback" method
>
> function func1(x::AbsType)
> 0
> end
>
> then calling func2(foo) merrily returns 0 instead of 51626.
Well, why should it return 51626? Like in any other programming
language, you need to tell Julia what to do ;-)
> This is either a nice feature or a silent failure depending on the
> intentions of whoever (not myself) decided not to or forgot to
> implement func2(ConcType).
Well, if returning a default is not an option, then you could define a
func1 (and any other a concrete interface needs) like this
function func1(x::AbsType)
error("No function func1 defined for concrete type $(typeof(x)): define
one!")
end
You can also define a function
function func1(x::AbsType)
x.attr
end
which assumes that all subtypes have a field attr (if not you need to
make another method for func1 for that particular subtype).
> I'm aware that 1) obviously there wasn't anywhere else before then that was
> particularly reasonable for the interpreter to fail during my trial, and 2)
> classes and inheritance are declasse. Then in Julia's type system (as
> implemented now or planned in the near future), what's the best guarantee I
> can give myself as I write a module that defines AbsType and func2 beyond
> writing great docs and hoping that the people who implement their own
> ConcType will read the docs as well as write unit tests for their new type?
There is no formal way of specifying an interface for your abstract
type. Maybe there should be one? Above two suggestions give two
possible ways to go about it, but maybe others who have written more
Julia can comment on best ways to go about this.
You can use `methodswith` to discover what methods are defined for a
particular type.
Coming from an classic OO background (Python) it took me a bit of time
to get adjusted. There are things which can't be done but then other
things are easy which are difficult in OO.