sâmbătă, 5 martie 2016, 22:59:33 UTC+1, Milan Bouchet-Valat a scris:
>
> Le samedi 05 mars 2016 à 13:42 -0800, Adrian Salceanu a écrit : 
> > Gentleman, I stumbled onto this one in my code, and despite trying 
> > all the possible combinations I could think of, no dice.  
> > 
> > I have this simple type hierarchy:  
> > 
> > abstract Model 
> > type Package <: Model 
> > type Repo <: Model 
> > 
> > The Model type is an ORM and it defines a series of methods that 
> > operate on Model subtypes.  
> > 
> > ex:  
> > function save{T<:Model}(m::T) 
> Note that this can be written more simply as: 
> function save(m::Model) 
>

I noticed that this works too, but I find that the longer syntax better 
transmits the notion that the method expects a subtype, rather than the 
type itself. Of course, this is lees important in Julia, where the super 
type can only be abstract. 
 

>
> (the longer syntax is only useful when T is a parameter of another 
> argument type) 
>
> > So far so good.  
> > 
> > Now, according to ORM architecture and design patterns, an instance 
> > of a subtype represents a table row, while the class itself 
> > represents the table. And of course there are plenty of such 
> > methods.  
> > For instance, the find_one_by function, which takes the type itself 
> > (the table).  
> > 
> > function find_one_by(m, column_name::SQLColumn, value::SQLInput) # 
> > this works, with m::Any 
> > 
> > The question is, how can I define this method so that it accepts all 
> > the types of the subtypes of Model?  
> > 
> > I tried multiple combinations, such as:  
> > function find_one_by{T<:Type{Model}}(m::T, column_name::SQLColumn, 
> > value::SQLInput) 
> > 
> > none was good. :( Any ideas?  
> Use this: 
> find_one_by{T<:Model}(m::Type{T}, ... 
>
>
It works! Wow, awesome! Many thanks!
 

>
>
> Regards 
>

Reply via email to