Hi Robert.

That's kind of right.

Julia doesn't define functions using syntax like you've used, but here's some 
Julia code that would work:

abstract MyAbstractType

immutable MyType <: MyAbstractType
end

constant_attributes(x::MyAbstractType) = 3, 2, 5

x = MyType()

constant_attributes(x)

Whether you should make the argument to constant_attributes a type or a value 
depends on whether you're going to write code with predictable types (which you 
should do). As long as the type of x is predictable, the definition of 
constant_attributes() that I've used will be inlined when appropriate.

Also worth noting that no one uses caps when naming functions in Julia. To most 
of us, a capitalized letter is a very strong indicator that you're talking 
about a type, not a function.

 -- John

Thanks for your quick reply. Yes, I have though of this, however, it sounded a 
bit like a clumsy workaround. That would mean I would have:
function (a, b, c) = constantAttributes{T<:MyAbstractType}(::Type{T})
   a = 3
   b = 2
   c = 5
end

instance = MyType(someAttributes)
(att1, att2, att3) = constantAttribute(typeof(instance))

Right? BTW, a little OT, I know, but is there an overhead if I pass the 
instance itself, instead of only its type, i.e. 
constantAttributes{T<:MyAbstractType}(instance::T)?

  -- John

On Nov 25, 2014, at 1:26 PM, Robert Gates <[email protected]> wrote:

> Hi John,
> 
> Thanks for your quick reply. Yes, I have though of this, however, it sounded 
> a bit like a clumsy workaround. That would mean I would have:
> function (a, b, c) = constantAttributes{T<:MyAbstractType}(::Type{T})
>    a = 3
>    b = 2
>    c = 5
> end
> 
> instance = MyType(someAttributes)
> (att1, att2, att3) = constantAttribute(typeof(instance))
> 
> Right? BTW, a little OT, I know, but is there an overhead if I pass the 
> instance itself, instead of only its type, i.e. 
> constantAttributes{T<:MyAbstractType}(instance::T)?
> 

Reply via email to