# How to raise a warning when using the default constructor directly?

**URL:** <https://discourse.julialang.org/t/how-to-raise-a-warning-when-using-the-default-constructor-directly/64011>\
**Category:** New to Julia\
**Created:** [July 3, 2021, 10:25pm UTC](https://discourse.julialang.org/t/how-to-raise-a-warning-when-using-the-default-constructor-directly/64011 "2021-07-03T22:25:43Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![andrey2185](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andrey2185/32/9889_2.png) [@andrey2185](https://discourse.julialang.org/u/andrey2185)\
**Post date:** [July 3, 2021, 10:25pm UTC](https://discourse.julialang.org/t/how-to-raise-a-warning-when-using-the-default-constructor-directly/64011/1 "2021-07-03T22:25:43Z")

</div>

Hellow! I have a code :

```julia
module Test

export custom_constructor_1, custom_constructor_2

struct Foo
	a::Int
	b::Int
	c::Int
	function Foo(a::Int, b::Int)
		# @warn "You should use a custom constructor!" # this must work only in the default constructor
		return new(a, b, a + b)
	end
end

function custom_constructor_1(;
		a::Int = 5,
		b::Int = 8,
	)
	@info "Good, you use custom constructor № 1"
	println("a=$a, b=$b")
	return Foo(a, b)
end

function custom_constructor_2(;
		a::Int = 3,
		b::Int = 4,
	)
	@info "Good, you use custom constructor № 2"
	println("a=$a, b=$b")
	return Foo(a, b)
end

end # module Test

```

How to raise a warning when using the default constructor directly? like this:

**julia\>** `using .Test`

**julia\>** foo = Test.Foo(1,2)  
┌ Warning: You should use a custom constructor!  
└ @ Main.Test REPL[2]:10  
Main.Test.Foo(1, 2, 3)

**julia\>** foo = custom\_constructor\_1() # this is OK  
[ Info: Good, you use custom constructor № 1  
a=5, b=8  
Main.Test.Foo(5, 8, 13)

**julia\>** foo = Test.custom\_constructor\_2(a=3, b=4) # and this is OK  
[ Info: Good, you use custom constructor № 2  
a=3, b=4  
Main.Test.Foo(3, 4, 7)

---

<div class="post-metadata">

**Author:** ![tamasgal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamasgal/32/27946_2.png) [@tamasgal](https://discourse.julialang.org/u/tamasgal)\
**Post date:** [July 3, 2021, 10:34pm UTC](https://discourse.julialang.org/t/how-to-raise-a-warning-when-using-the-default-constructor-directly/64011/2 "2021-07-03T22:34:20Z")

</div>

The first idea which comes to my mind is to add a keyword argument to your inner constructor which mutes the warning (punintented).

```julia
struct Foo
	a::Int
	b::Int
	c::Int
	function Foo(a::Int, b::Int; warnifused=true)
		warnifused && @warn "You should use a custom constructor!"
		return new(a, b, a + b)
	end
end

```

and then

```julia
function custom_constructor_1(;
		a::Int = 5,
		b::Int = 8,
	)
	@info "Good, you use custom constructor № 1"
	println("a=$a, b=$b")
	return Foo(a, b; warnifused=false)
end

```
