Instead of having the name be ASCIIString, you could have that be another 
type like SomeTypeName, (which then has the string "A" or "B" as an 
attribute).  Then you could do a type alias to map TypeA to 
SomeType{SomeTypeA} and TypeB to SomeType{SomeTypeB} assuming SomeTypeA and 
SomeTypeB are types that extend an abstract type SomeTypeName.

For example, I have something like this in one of my projects:

abstract ModelType
abstract Model

# model types
type AffineModelType <: ModelType end
type TermStructureConsistentModelType <: ModelType end

# type aliases
typealias AffineModel Model{AffineModelType}
typealias TermStructureConsistentModel 
Model{TermStructureConsistentModelType}

Someone else feel free to chime in if this isn't really the best way, as 
I'm still learning too!

Chris

On Friday, February 5, 2016 at 4:16:24 PM UTC-5, 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