Dispatch on return types for a higher order function

The motivational problem I have is I’m trying to make a data pipeline to calculate features from observations.

I’m gonna jump into an example, let’s say I’m working with real estate data. Observations look like this:

struct Data
  street::String 
  squarefoot::Int64
  floors::Int64
  ...
end

Then I can define feature methods that are all of the following “type”:

feature_function :: Data -> SomeType

such as

street(d::Data) = d.street
logsqft(d::Data) = log(d.squarefoot)

I can construct models and pair them with appropriate feature methods. Then I just need to build some generic code to go over the data with the supplied feature methods and return for the model to use. I can do this but I was wondering if it’s possible to make the code type stable. For example, as I understand the following method is not type stable.

extract(fs::Vector{Function}, d::Data) = Tuple(f(d) for f in fs)

Since the type of extract([street, logsqft], d) is not the same as extract([logsqft, street], d). I was trying to figure out if I could do this with type selectors and traits but I couldn’t think of a way.

Is there a Julian way to go about this? I think I can live with this type instability but I wanted to check if there’s a solution here that I missed.

A different question might be: why do you want it to be type stable at all?

One would presume that f(d) is more CPU intensive than for f in fs.

And running your model even more so than that.

It seems like premature optimization to me. Though, of course, that might be because of your simplified example. And in different situations it would be desirable.

I run large analytics and I never worry about type stability of the setup because the analysis dominates.

That’s very fair and I agree that this is premature optimization. I’m partially using this project as a learning process for how to think in a Julian fashion. So I was interested in spending a little time thinking about this.

Thanks for your input. It’s a helpful reminder that not everything needs to be type stable to get the benefits of Julia.