You're looking for #1974 <https://github.com/JuliaLang/julia/issues/1974>.
Once that's done, you will be able to do this, but not until then.


On Tue, Mar 25, 2014 at 4:48 PM, Keith Mason <desc...@gmail.com> wrote:

> I have a number of variables based on composite types. I want the fields
> of these variables synced to disk, so that every time I modify a field, the
> data is written to disk. Is there any way to do this with assignment
> notation?
>
> For example,
>
> type Foo
>   x::Int32
>   y::Int16
>   z::Int16
>
>   function Foo(x,y,z)
>     s = stream("foo",true, true, true, false, false)
>     write(s, x)
>     write(s, y)
>     write(s, z)
>   end
> end
>
> f=Foo(1,2,3)
> ...
> f.z = 4
>
>
> The constructor will write the initial values to disk.  What kind of code
> can I add to this program so that the assignment of f.z seeks to the disk
> location where z is stored and writes the new value.  I don't mind
> implementing a function like
>
> function update(value, name::Symbol, x)
>
>   T = type(value)
>
>   index = indexin([:x], convert(Array{Symbol,1},names(T)))
>
>   seek(s, fieldoffsets(T)[index])
>   write(s, x)
>   setfield(value, name, x)
>
> end
>
>
> but I don't want to have to call update(f, :z, 4) everywhere in the code
> that wants to change f.  I want to write f.z=4. Otherwise, the user of my
> type may forget and cause inconsistency between data in memory and data on
> disk.
>
> I know that some languages have triggers, where a function can be called
> when a variable is modified, and I know that I can write functions to
> replace operators in Julia.  But I cannot find any such trigger
> functionality in Julia, nor can I seem to override the assignment operator
> (=).
>
> function =(x, v)
>
> ...
>
> end
>
>
>
>
> doesn't work; it tells me "syntax: unexpected =".
>
> The docs state that there is a setfield! function that is called for a.b =
> c, but the language disagrees.  Apparently it's actually named setfield (no
> exclamation point).  And it's not a function I can replace.
>
> setfield(x, name::Symbol, value) = println("$name")
>
>
> reports "invalid method definition: not a generic function".  So, setfield
> cannot be overridden.
>
> Do I have any options?
>
> Thanks,
> Keith
>

Reply via email to