I would use a regular function like 

inc_count(item::Item, n)

but you can get the interface you asked for with an extra type definition:

import Base.+, Base.convert

type Counter
    n
end
convert(::Type{Counter}, n::Int) = Counter(n)
function +(c::Counter, m)
    println("Item value +$(m)")
    c.n + m
end

type Item                   
    id::Int32                    
    count::Counter             
end                                    
 

a = Item(1, 1)                  
a.count += 10
a.count += 20
a.count

You could store the name of the item in the Counter, too. It's just not 
very idiomatic, but it depends what you want to do...

On Wednesday, May 4, 2016 at 5:11:32 AM UTC-4, Kristoffer Carlsson wrote:
>
> I believe you would need field access overloading for this to work 
> cleanly: https://github.com/JuliaLang/julia/issues/1974
>
> On Wednesday, May 4, 2016 at 11:03:18 AM UTC+2, Yonghee Kim wrote:
>>
>> I'd like write a simple log when type field value has changed
>>
>> something like this
>> type Item                   
>>     id::Int32                    
>>     count::UInt32             
>>                                          
>>     # function for writing a log
>> end                                    
>>  
>>
>> then when I do this...
>> *   a = Item(1, 1)                  *
>> *   a.count += 10            *
>>
>> write a message like this at console 
>> *  Item "a.count" value +10                 *
>>
>>
>> Is it possible? or should I make macro to do this. 
>>
>

Reply via email to