I have a list of structs of an arbitrary size.
Let's call it l
.
l = [%X{a:1}, %X{a:3}, %X{a:9}, %X{a:11}]
The size of l
keeps changing. What I'd like to know is how do I pattern match against l
to ensure it always is made up of structs of %X{}
. I want the pattern matching to fail if the list contains something else. For example:
l = [%X{a:1}, %X{a:3}, %Y{a:9}, %Z{a:11}]
Things I've tried
i = %X{}
j = %Y{}
[%X{}|_] = [i,i,i]
But that matches only the first element.
[%X{}|_] = [i,j,j]
Should fail for my use case, but it doesn't. Maybe if there's an operator or something like this, that will match a list of specific type, that's exactly what I'm looking for:
[%X{}+] = [i,i,i] # Doesn't exist, just an example
Some background
I'm on phoenix and I have a model post
with has_many
relationship with images
. A given user could upload multiple images and I'd like to pattern match to make sure I'm working with the right struct (%Plug.Upload{}
) in this case.
Any help is much appreciated. Thanks :)