Try this:

type Node
    threads::Vector{Fyle}
    maxthreads::Int
    complete::Vector{Fyle}
    percent::Float64
    name::UTF8String
end


Differences:

   - capitalized Node: the convention is that types are capitalized
   - made threads & complete of type Vector{Fyle} – Array{Fyle} is an
   abstract type with unspecified number of dimensions
   - made name of type UTF8String. String is an abstract type, which has
   been renamed to AbstractString to make that clearer; since all strings
   can be converted to UTF8String, this will transparently just work with
   better performance.

Note that without additional methods you can only invoke Node with all five
arguments given positionally. If you want a constructor that takes
keywords, you'll need to provide it. Similarly, if you want a constructor
that constructs incomplete Node objects, you'll need to provide that too.

On Tue, Mar 31, 2015 at 5:55 PM, Michael Bullman <[email protected]>
wrote:

> Hi Pablo,
>
> Thanks for the help so far. I've tried to rewrite the node type using the
> outer constructor, but I'm getting Stack Overflow errors and I'm 99%
> certain it's my outer constructors fault
>
> type node
>
>         threads::Array{Fyle}
>
>         maxthreads::Int
>
>         complete::Array{Fyle}
>
>         percent::Float64
>
>         name::String
>
> end
>
>
> #Outer Constructor
>
> node(name) = node(maxthreads=400;name="Default")
>
> I tried using your outer constructor as a template along with using the Doc 
> page on constructors.
> The above outer constructor does not work, I get "ERROR: function node does 
> not accept keyword arguments" even though I thought putting the semi-colon 
> before name defined name as a keyword argument.
>
> I'm also wondering if this falls under the Incomplete Initialization 
> <http://julia.readthedocs.org/en/latest/manual/constructors/?highlight=constructors#incomplete-initialization>
>  subsection of the constructor Doc, since I define many more fields than I 
> initialize in the constructor.
>
>
> I'm not looking for anyone to write this out for me, I'm just fairly
> confused by what my outer constructor is saying.
>
> I've tried it with
>
> node(name) = node(maxthreads=400;name=name) and a second empty constructor 
> for default values that looked like
>
> node() = node(maxthreads=400;name="Default")
> node(name) = node(maxthreads=400;name=name)
>
> Thinking that the node() constructor would become the "Default" Constructor 
> and the node(name) constructor would pass name to name, but it's still 
> getting the does not accept keywords error.
>
> I think I'm just fundamentally thinking of this incorrectly.
>
>
>
> On Friday, March 27, 2015 at 2:53:28 PM UTC-4, Pablo Zubieta wrote:
>>
>> Hi Michael
>>
>> As Stefan just pointed out, in Julia one should in general take advantage
>> of the Multiple Dispatch paradigm and write functions that dispatch on the
>> types instead of having functions belonging to the type (as you would have
>> in Python).
>>
>> Let me give you a simple example by rewriting you Fyle type.
>>
>> You have something like this
>>
>> type Fyle
>>     size::Float64
>>     origSize::Float64
>>     lifetime::Int
>>
>>     get_lifetime::Function
>>     get_size::Function
>>
>>     function Fyle(size = 0)
>>         this = new()
>>
>>         this.size=size
>>         this.lifetime=0
>>         this.origSize=size
>>
>>         this.get_lifetime = function()
>>            return(this.lifetime)
>>         end
>>         this.get_size = function()
>>            return(this.size)
>>         end
>>
>>         return this
>>     end
>> end
>>
>> when you should have really written something like this
>>
>> # I am going to define a method size for you type Fyle
>> # size is a generic function in Base, so one should import it on order to
>> be able to extend it for a user Type
>> import Base.size
>>
>> type Fyle
>>     size::Float64
>>     origSize::Float64
>>     lifetime::Int
>> end
>>
>> # Outer constructor (in some cases you might want to define an inner one
>> as you did)
>> # Read the manual section on Constructors
>> Fyle(size=0) = Fyle(size, size, 0)
>>
>> # Define the desired functions to dispatch on the Type
>> lifetime(f::Fyle) = f.lifetime
>> size(f::Fyle) = f.size
>>
>> Hope this helps.
>>
>> Greetings, Pablo
>>
>>
>>

Reply via email to