From 32acc26c4d7464fae61f1b7614a5debe7464e7e5 Mon Sep 17 00:00:00 2001 From: Tortar <68152031+Tortar@users.noreply.github.com> Date: Fri, 13 Oct 2023 14:03:37 +0200 Subject: [PATCH] Allow kwdef macro outside of definition of struct (#18) * Allow kwdef macro outside of definition of struct * Update test_ProtoStruct.jl * better matching * Update ProtoStruct.jl * Update ProtoStruct.jl --------- Co-authored-by: Simon Christ --- src/ProtoStruct.jl | 4 ++++ test/test_ProtoStruct.jl | 10 ++++++++++ 2 files changed, 14 insertions(+) diff --git a/src/ProtoStruct.jl b/src/ProtoStruct.jl index 287f9e1..1cfc81a 100644 --- a/src/ProtoStruct.jl +++ b/src/ProtoStruct.jl @@ -1,4 +1,8 @@ macro proto( expr ) + if expr.head == :macrocall && expr.args[1] == Symbol("@kwdef") + expr = expr.args[3] + end + if expr.head != Symbol("struct") throw(ArgumentError("Expected expression to be a type definition.")) end diff --git a/test/test_ProtoStruct.jl b/test/test_ProtoStruct.jl index 909fea4..0909c69 100644 --- a/test/test_ProtoStruct.jl +++ b/test/test_ProtoStruct.jl @@ -57,6 +57,16 @@ end @test tw.E == "yepp" end +@proto @kwdef struct TestMacroOutside + A::Int = 1 +end + +@testset "@kwdef macro outside" begin + tw = TestMacroOutside() + @test tw isa TestMacroOutside + @test tw.A == 1 +end + @proto mutable struct TestMutation F::Int G::Float64