I can see two solutions. Either you define an outer constructor for 
MyOtherType,

MyOtherType(i::Int) = MyOtherType(MyType(i))

or you can consider MyType equivalent to an Int, so that you define convert

convert(::Type{MyType}, i::Int) = MyType(i)

Ivar

tirsdag 13. januar 2015 07.24.25 UTC+1 skrev Nathaniel Virgo følgende:
>
> I have a custom type with a constructor function, and I have another type 
> that has several members of this type. The problem is that the inner type's 
> constructor doesn't get called when I would expect it to. Here is a minimal 
> example to show what I mean:
>
> type MyType
>  member::Int64
>  anothermember::Float64
> end
>
> function MyType(value::Int64)
>  return MyType(value, 0.0)
> end
>
> foo = MyType(42)  # ok
>
> type MyOtherType
>  member::MyType
> end
>
> bar = MyOtherType(42)  # this gives an error
>
>
> The error this gives is as follows:
>
> ERROR: `convert` has no method matching convert(::Type{MyType}, ::Int64)
>  in convert at base.jl:13
>  in include at /Applications/Julia-0.3.3.app/Contents/Resources/julia/lib/
> julia/sys.dylib
>  in include_from_node1 at loading.jl:128
>  in process_options at /Applications/Julia-0.3.3.app/Contents/Resources/
> julia/lib/julia/sys.dylib
>  in _start at /Applications/Julia-0.3.3.app/Contents/Resources/julia/lib/
> julia/sys.dylib (repeats 2 times)
> while loading /Users/ndv21/Dropbox/Code/Irreversible binary string 
> chemistry/test.jl, in expression starting on line 17
>
> What can I do to make this work. E.g. how can I ensure that `convert` will 
> call my type's constructor appropriately? (The `convert` documentation 
> doesn't say.) Or is there a better way to do this?
>
>
> Nathaniel
>
>

Reply via email to