<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">I saw a message come across about Memoization, and I thought of using a slightly different approach for similar benefit.<div class=""><br class=""></div><div class="">I commonly want to store the result of a complex calculation in a private variable in a struct to avoid recalculation cost… but now I have to mark that calculation as *mutating* even though it isn’t really mutating in the semantic sense (since it will have the same value). It is an implementation detail leaking out… the caller shouldn’t care if I am memoizing.</div><div class=""><br class=""></div><div class="">Also, my attempt to speed things up may have actually slowed things down, since a bunch of other stuff had to be copied...</div><div class=""><br class=""></div><div class="">It would be nice to mark a calculated property of a struct as “memo" so that the calculation result is stored… but the calculation shouldn’t need to be marked as mutable (unless it actually mutates other things). That memo should be automatically cleared whenever an actual mutating method is called on the struct.</div><div class=""><br class=""></div><div class="">Ideally, the compiler would eventually be smart enough to only clear the memos when properties used in the calculations change, but I think just clearing it whenever the struct is mutated is a good first step.</div><div class=""><br class=""></div><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><div class="">struct MyStruct {</div><div class=""> var a:Int</div><div class=""> var b:Int</div><div class=""><br class=""></div><div class=""> memo var c {</div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>//Complex calculation here</div><div class=""> }</div><div class="">}</div><div class=""><br class=""></div><div class="">let s = MyStruct(a:2, b:3)</div><div class="">print(s.c) // result of c is automatically memoized</div><div class="">print(s.c) // returns memoized result</div><div class="">s.a = 5 //This clears the memo for c as it may affect the calculation</div><div class="">print(s.c) // New result of c is automatically memoized</div></blockquote><div class=""><br class=""></div><div class=""><br class=""></div><div class="">The other alternative is to do things manually. Letting the programmer declare special private variables on a struct that are allowed to mutate without the “mutating” key word. That is potentially more powerful, but also more error prone. I prefer the set-it-and-forget-it approach.</div><div class=""><br class=""></div><div class="">If “memo" is the wrong word for this, I am completely open to calling it something else. I would really like to see this functionality in Swift 3 though (whatever it is called).</div><div class=""><br class=""></div><div class="">Thanks,</div><div class="">Jon</div></body></html>