On Wednesday, April 8, 2015 at 8:00:42 AM UTC-7, Tim Holy wrote:
>
> It's a matter of taste, really, but in general I agree that the Julian way 
> is 
> to reduce the number of accesses to fields directly. That said, I do 
> sometimes 
> access the fields. 
>
> However, your iterator example is a good opportunity to illustrate a more 
> julian approach: 
>
> mesh = mesh(...) 
> for vertex in vertices(mesh) 
>     blah blah 
> end 
>
> The idea is that vertices(mesh) might return an iterator object, and then 
> you 
> write start, next, and done functions to implement iteration. Presumably 
> you 
> should build the iterators for Mesh on top of iterators for FESection, 
> etc, so 
> the whole thing is composable. You'd then have short implementations of 
> the 
> vertices function taking a Mesh, FESection, or Element. 
>
>
Tim,

Can you comment on the performance implications of directly accessing 
fields vs your approach?  I'm guessing that directly accessing the fields 
would be faster?

Phil 

> On Wednesday, April 08, 2015 06:35:46 AM Kristoffer Carlsson wrote: 
> > I come from a Python background where direct access to fields in for 
> > example classes with the dot notation is very common. 
> > 
> > However, from what I have seen in different conversations, accessing 
> fields 
> > directly is not really Julian. Sort of a "fields are an implementation 
> > detail" mindset, and "what is important are the functions". 
> > 
> > Here is an example of a type hierarchy that is a little bit similar to 
> > types I am working with now: 
> > 
> > type Element 
> >     vertices::Vector{Int} 
> > end 
> > 
> > type Node 
> >     coordinates::Vector{Float64} 
> >     id::Int 
> > end 
> > 
> > type FESection 
> >     elements::Vector{Elements} 
> >     nodes::Vector{Nodes} 
> > end 
> > 
> > type Mesh 
> >    sections::Vector{FESection} 
> > end 
> > 
> > Now, let's say that I want to write a function to loop over all 
> vertices. 
> > One way (which I would do in Python is): 
> > 
> > mesh = Mesh(.....) 
> > for section in mesh.sections 
> >     for element in section.elements 
> >         for vertices in element.vertices 
> >               blah bla 
> >         end 
> >     end 
> > end 
> > 
> > 
> > 
> > However, this accesses the fields directly. Would it be more Julian to 
> > write getters for the fields? Since Julia does not have @property like 
> > Python I realize that by accessing the fields you commit to exactly the 
> > name of the field and it's type while with a getter it would be more 
> > flexible. 
> > 
> > Best regards, 
> > Kristoffer Carlsson 
>
>

Reply via email to