changeset 1056:fe83ea1db953 feature/nested_stencils

Add argument restrictions to stencil constructors to get clearer error messages (grafted from f74189c954d65eb5e2ce8de4e9210f74390b8cb7)
author Jonatan Werpers <jonatan@werpers.com>
date Thu, 20 Jan 2022 15:17:37 +0100
parents df498ce0cf52
children 0728e84af0dc
files src/SbpOperators/stencil.jl
diffstat 1 files changed, 9 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/src/SbpOperators/stencil.jl	Thu Jan 20 14:03:16 2022 +0100
+++ b/src/SbpOperators/stencil.jl	Thu Jan 20 15:17:37 2022 +0100
@@ -29,7 +29,7 @@
 
 Base.convert(::Type{Stencil{T}}, stencil) where T = Stencil{T}(stencil)
 
-function CenteredStencil(weights::Vararg)
+function CenteredStencil(weights::Vararg{T}) where T
     if iseven(length(weights))
         throw(ArgumentError("a centered stencil must have an odd number of weights."))
     end
@@ -81,20 +81,22 @@
 end
 
 
-struct NestedStencil{T,N}
-    s::Stencil{Stencil{T,N},N}
+struct NestedStencil{T,N,M}
+    s::Stencil{Stencil{T,N},M}
 end
 
+# The exessive use of type parameters for the following constructors are to catch errors earlier with clearer error messages
+
 # Stencil input
-NestedStencil(s::Vararg{Stencil}; center) = NestedStencil(Stencil(s... ; center))
-CenteredNestedStencil(s::Vararg{Stencil}) = NestedStencil(CenteredStencil(s...))
+NestedStencil(s::Vararg{Stencil{T,N}}; center) where {T,N} = NestedStencil(Stencil(s... ; center))
+CenteredNestedStencil(s::Vararg{Stencil{T,N}}) where {T,N} = NestedStencil(CenteredStencil(s...))
 
 # Tuple input
-function NestedStencil(weights::Vararg{Tuple}; center)
+function NestedStencil(weights::Vararg{NTuple{N,T}}; center) where {T,N}
     inner_stencils = map(w -> Stencil(w...; center), weights)
     return NestedStencil(Stencil(inner_stencils... ; center))
 end
-function CenteredNestedStencil(weights::Vararg{Tuple})
+function CenteredNestedStencil(weights::Vararg{NTuple{N,T}}) where {T,N}
     inner_stencils = map(w->CenteredStencil(w...), weights)
     return CenteredNestedStencil(inner_stencils...)
 end