Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- MyObject: type = {
- // some more fields
- public url: std::string;
- public operator=:(out this, url_: std::string) = {
- this.url = url_;
- }
- public operator=:(out this, in that) = {
- this.url = that.url;
- }
- }
- CachingMyObjectAccessor: type = {
- public operator=:(out this, obj_: *MyObject) = {
- this.obj = obj_;
- this.parsed_url = std::nullopt;
- }
- public get_domain: (inout this) -> std::string_view = {
- this.parse_url();
- return std::get<1>(parsed_url*);
- }
- public get_path: (inout this) -> std::string_view = {
- this.parse_url();
- return std::get<2>(parsed_url*);
- }
- private parse_url: (inout this) = {
- if (!parsed_url.has_value()) {
- // some expensive code
- s := std::string_view(obj*.url);
- i := s.find("//");
- j := s.find("/", i+2);
- this.parsed_url = (s.substr(0, i), s.substr(i+2, j-i-2), s.substr(j));
- }
- }
- obj: *MyObject;
- parsed_url: std::optional<std::tuple<std::string_view, std::string_view, std::string_view>>;
- }
- main: () -> int = {
- o: MyObject = ("http://a.b/c/d/e");
- a: CachingMyObjectAccessor = (o&);
- // oo := o; // uncommenting this moves out of o breaking the accessor
- std::print("domain: {} path: {}", a.get_domain(), a.get_path());
- _ = a;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement