Minutes after I posted the last reply, the solution presented itself to me,
so I'll write it here for posterity ;)
The missing link was indicating that the type has different names, thus
rendering the different functions as constructors. Following the running
example in this thread, this idea turns out as follows,
type SomeType
name::ASCIIString
value::Int
end
typealias TypeA SomeType
TypeA(value::Int) = SomeType("A", value)
typealias TypeB SomeType
TypeB(value::Int) = SomeType("B", value)
Now, when testing if TypeA <: SomeAbstract returns true.
If anyone has thoughts about the different approaches, I'd love to hear.
Uri
On Friday, February 5, 2016 at 11:16:24 PM UTC+2, Uri Patish wrote:
>
> Hi,
>
> I was wondering what is the best way going about the following issue.
> Suppose I have a type with few outer constructors which I name differently,
> and I want Julia to recognize these functions as constructors of that type.
> For example
>
> type SomeType <: SomeAbstract
> name::ASCIIString
> value::Int
> end
>
> TypeA(value::Int) = SomeType("A", value)
> TypeB(value::Int) = SomeType("B", value)
>
> In this case, I have two pseudo-types, TypeA and TypeB, which have the
> same structure and are only different by their state. The problem with this
> approach is that now TypeA and TypeB are functions and not datatypes, thus,
> I cannot use operations defined on datetypes like checking for subtyping
> (TypeA
> <: SomeAbstract), or overloading type construction using call.
>
> Clearly, I could have defined two types which have exactly the same
> structure but this seems wrong to me as it involes code replication.
> Another possible solution is to define a macro which defines the type and
> its outer constructor, and I would call the macro to define TypeA and
> TypeB. Nonetheless, I was wondering if there was a way to tell Julia that
> the functions TypeA and TypeB are in fact constructors of SomeType. I'd be
> glag to hear about other solutions as well.
>
> Uri
>
>
>