Isstructtype()

Is it possible to do something like

function my_func(  x::I  )  where isstructtype(I)

    #function body
    
end

WhereTraits.jl supports that.

1 Like

note that if you’re coming from Matlab (which I’m willing to bet you are), this won’t do what you want. instead you want to leave this untyped.

Thanks Jar.
Thinking of suggesting this for use in a couple of packages.

For example NamedTupleTools.jl has a specific function ntfromstruct() for converting structs.
With the below line added. The user could instead use the generic NamedTuple function.

@traits NamedTuple( x ) where isstructtype(typeof(x)) = ntfromstruct( x )

Similarly, the Dictionary() function in Dictionaries.jl converts many different types of input (but not currently structs). The below line would allow it to do so.

@traits Dictionary( x ) where isstructtype(typeof(x)) = Dictionary( ntfromstruct( x ))

@Oscar_Smith
I’m not a matlab user. Not sure if this affects your advice.

Out of curiosity, what kinds of objects would be permitted to dispatch through this? We don’t have limits on dispatch through functions.

Bit of a warning, a lot of unexpected types are isstructtype.

1 Like