Hacker News new | past | comments | ask | show | jobs | submit login

> First, traits don't define any fields.

How does one handle cases where fields are useful? For example, imagine you have a functionality to go fetch a value and then cache it so that future calls to get that functionality are not required (resource heavy, etc).

    // in Java because it's easier for me
    public interface hasMetadata {
        Metadata getMetadata() {
            // this doesn't work because interfaces don't have fields
            if (this.cachedMetadata == null) {
                this.cachedMetadata = generateMetadata();
            }
            return this.cachedMetadata;
        }
        // relies on implementing class to provide
        Metadata fetchMetadata(); 

    }





Getters and setters that get specified by the implementing type.

But then you have the getters, setters, and field on every class that implements the functionality. It works, sure, it just feels off to me. This is code that will be the same everywhere, and you're pulling it out of the common class and implementing it everywhere.

Yep. Or ... don't put that in the interface at all. It looks like an implementation concern to me.

But if there's a lot of classes that implement the same thing, then not duplicating code makes sense. And saying "it's an implementation detail" leads to having the same code in a bunch of different classes. It feels very similar to the idea of default implementations to me; when the implementation will be the same everywhere, it makes sense to have it in one place.

So to be clear about your example: You have a whole lot of different - totally distinct - types of things, which all need to have the same logic to cache HTTP requests? Can you give some examples of these different types you're creating? Why do you have lots of distinct types that need exactly the same caching logic?

It sounds like you could solve that problem in a lot of different ways. For example, you could make an HTTP client wrapper which internally cached responses. Or make a LazyResource struct which does the caching - and use that in all those different types you're making. Or make a generic struct which has the caching logic. The type parameter names the special individual behaviour. Or something else - I don't have enough information to know how I'd approach your problem.

Can you describe a more detailed example of the problem you're imagining? As it is, your requirements sound random and kind of arbitrary.


From a very modified version of something I was working on recently, but with the stuff I couldn't do actually done here (and non-functionality code because of that, but is shows the idea)

    public interface MetadataSource {
        Metadata metadata = null;

        default Metadata getMetadata() {
            if (metadata == null) {
                metadata = fetchMetadata();
            }
            return metadata;
        }
        
        // This can be relatively costly
        Metadata fetchMetadata();
    }

    public class Image implements MetadataSource {
        public Metadata fetchMetadata() {
            // goes to externally hosted image to fetch metadata
        }
    }

    public class Video implements MetadataSource {
        public Metadata fetchMetadata() {
            // goes to video hosting service to get metadata
        }
    }

    public class Document implements MetadataSource {
        public Metadata fetchMetadata() {
            // goes to database to fetch metadata
        }
    }
Each of the above have completely different ways to fetch their metadata (ex, Title and Creator), and of them has different characteristics related to the cost of getting that data. So, by default, we want the interface to cache the result so that the

1. The thing that _has_ the metadata only needs to know how to fetch it when it's asked for (implementation of fetchMetadata), and it doesn't need to worry about the cost of doing so (within limits of course)

2. The things that _use_ the metadata only need to know how to ask for it (getMetadata) and can assume it has minimal cost.

3. Neither one of those needs to know anything about it being cached.

I had a case recently where I needed to check "does this have metadata available" separate from "what is the metadata". And fetching it twice would add load.


Here's my take on implementing this in rust. I made a trait for fetching metadata, that can be implemented by Image, Video, Document, etc:

    trait MetadataSource {
        fn fetch_metadata(&self) -> Metadata;
    }
    impl MetadataSource for Image { ... } 
    impl MetadataSource for Video { ... } 
    impl MetadataSource for Document { ... }
And a separate object which stores an image / video / document alongside its cached metadata:

    struct ThingWithMetadata<T> {
        obj: T, // Assuming you need to store this too?
        metadata: Option<Metadata>
    }

    impl<T: MetadataSource> ThingWithMetadata {

        fn get_metadata(&self) -> &Metadata {
            if self.metadata.is_none() {
                self.metadata = Some(self.obj.fetch_metadata());
            }
            self.metadata.as_ref().unwrap()
        }
    }
Its not the most beautiful thing in the world, but it works. And it'd be easy enough to add more methods, behaviour and state to those metadata sources if you want. (Eg if you want Image to actually load / store an image or something.)

In this case, it might be even simpler if you made Image / Video / Document into an enum. Then fetch_metadata could be a regular function with a match expression (switch statement).

If you want to be tricky, you could even make struct ThingWithMetadata also implement MetadataSource. If you do that, you can mix and match cached and uncached metadata sources without the consumer needing to know the difference.

https://play.rust-lang.org/?version=stable&mode=debug&editio...


Isn't this essentially the generic typestate pattern in Rust? In my view there is a pretty obvious connection between that particular pattern and how other languages implement OO inheritance, though in all fairness I don't think that connection is generally acknowledged.

(For one thing, it's quite obvious to see that the pattern itself is rather anti-modular, and the ways generic typestate is used are also quite divergent from the usual style of inheritance-heavy OO design.)


When you call myImageInstance.fetchMetadata, what does it do? I don't know rust, so it's not clear to me how the value gets cached.

In this example, ThingWithMetadata does the caching. image.fetch_metadata fetches the image and returns it. It’s up to the caller (in ThingWithMetadata) to cache the returned value.

But part of the goal is to not need the caller to cache it. Nor have the class that knows how to fetch it need to know how to cache it either. The responsibility of knowing how to cache the value is (desired to be) in the MetadataSource interface.

The rule is that you can't cache a value in an interface, because interfaces don't store data. You need to cache a value in a struct somewhere. This implementation wraps items (like images) in another struct which stores the image, and also caches the metadata. Thats the point of ThingWithMetadata. Maybe it should instead be called WithCachedMetadata. Eg, WithCachedMetadata<Image>.

You can pass WithCachedMetadata around, and consumers don't need to understand any of the implementation details. They just ask for the metadata and it'll fetch it lazily. But it is definitely more awkward than inheritance, because the image struct is wrapped.

As I said, there's other ways to approach it - but I suspect in this case, using inheritance as a stand-in for a class extension / mixin is probably going to always be your most favorite option. A better approach might be for each item to simply know the URL to their metadata. And then get your net code to handle caching on behalf of the whole program.

It sounds like you really want to use mixins for this - and you're proposing inheritance as a way to do it. The part of me which knows ruby, obj-c and swift agrees with you. I like this weird hacky use of inheritance to actually do class mixins / extensions.

The javascript / typescript programmer in me would do it using closures instead:

    function lazyResource(url) {
      let cached = null
      return async () => {
        if (cached == null) cached = await fetch(url)
        return cached
      }
    }

    // ...
    const image = {
      metadata: lazyResource(url)
    }
Of all the answers, I think this is actually my favorite solution. Its probably the most clear, simple and expressive way to solve the problem.

> The rule is that you can't cache a value in an interface, because interfaces don't store data.

Right, but the start of where I jumped into this thread was about the fact that there are places where fields would make things better (specifically in relation to traits, but interfaces, too). And then proceeding to discuss a specific use case for that.

> A better approach might be for each item to simply know the URL to their metadata.

Not everything is a coming from a url and, even when it is, it's not always a GET/REST fetch.

> but I suspect in this case, using inheritance as a stand-in for a class extension / mixin is probably going to always be your most favorite option

Honestly, I'd like to see Java implement something like a mixin that allows adding functionality to a class, so the class can say "I am a type of HasAuthor" and everything else just happens automatically.


One way you could fix this with composition is:

    class CachedMetadataSource implements MetadataSource {
      CachedMetadataSource(MetadataSource uncachedSource) {}
      Metadata getMetadata() {
        if (metadata == null) {
          metadata = uncachedSource.getMetadata();
        }
        return metadata;
      }
    }

I don't see how that solves the problem. It seems like Video will need to keep it's own copy of CachedMetadaSource, which points back to itself, and go through that access it's metadata in the getMetadata implementation it makes available to it's users. At that point, it might as well just cache the value itself without the extra hoops. The difficult part isn't caching the value, it's preventing every class that implements MetadataSource from having to do so.

It would be the other way around. You wouldn't pass around the underlying suppliers directly, you'd wrap them. But if you must have state _and_ behavior, then `abstract class` is your friend in Java (while in Scala traits can have fields and constructors, so there is no problem).



Consider applying for YC's Summer 2025 batch! Applications are open till May 13

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: