Hi Chris, thanks for weighing in ;)

The solution you suggest is indeed a sound one, and one of my attempts at 
this issue was through parametric types and an enum instead of attributes 
like you suggest, e.g., 

@enum TypeKind A=1 B=2

type SomeType{T}
  value::Int
end

typealias TypeA SomeType{A}
typealias TypeB SomeType{B}

What I didn't like about this is solution is that only the types' names are 
parametric, but the types' bodies are exactly alike. In other words, it 
feels wrong to me to say that two types are different even though they are 
only different in the kind of instances they produce. Instances of types 
can be different in state, but if their structure is the same, then I feel 
that the best approach should indicate this by relying on a single type, 
and producing the different instances by overloading the constructor. 

BTW, going forward with the multiple types approach, the solution I found 
as most convenient is using a macro. This goes something like the 
following, 

macro sometype(typename, typevalue)
  quote
    type $(esc(typename))
      name::ASCIIString
      value::Int
    end
    $(esc(typename))(value::Int) = $(esc(typename))($typevalue, value)
  end
end

@sometype TypeA "A"
@sometype TypeB "B"

But I was hoping that maybe there was a solution using the single type 
approach.

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
>
>
>

Reply via email to