# How to define Macro for a new Type?

**URL:** <https://forum.devtalk.com/t/how-to-define-macro-for-a-new-type/47300>\
**Category:** Backend Questions/Help\
**Tags:** elixir, types\
**Created:** [28 March 2022 09:09 UTC](https://forum.devtalk.com/t/how-to-define-macro-for-a-new-type/47300 "2022-03-28T09:09:17Z")\
**Posts on this page:** 1\
**Showing post:** 11

<div class="post-metadata">

**Author:** ![dimitarvp](https://forum.devtalk.com/user_avatar/forum.devtalk.com/dimitarvp/32/5599_2.png) [@dimitarvp](https://forum.devtalk.com/u/dimitarvp)\
**Post date:** [6 April 2022 14:37 UTC](https://forum.devtalk.com/t/how-to-define-macro-for-a-new-type/47300/11 "2022-04-06T14:37:25Z")

</div>

> [@Fl4m3Ph03n1x](#):
>
> ```elixir
> def name?({:name, val}) when is_binary(val), do: true
> 
> ```

Sorry to comment on the previous idea – but that’s way too global. In a moderately big project you can have 5 different kinds of names. So to implement this properly you’d likely have to scope this somehow e.g. with module names (which are also atoms as you know):

```elixir
defmodule MyApp.Types.Address.Name do
  def name?({ __MODULE__ , val}) when is_binary(val), do: true

```

In many languages, internally types get all sorts of fancy prefixes. That’s important. You don’t want only one `Name` to be ever allowed in your project.

> [@Fl4m3Ph03n1x](#):
>
> ```elixir
> def is_type?({type, _data}, new_type) when type == new_type, do: true
> def is_type?(_data, _new_type), do: false
> 
> ```

Elixir offers an even neater way of doing this:

```elixir
def is_type?({type, _data}, type), do: true # note the repeated `type` variable
def is_type?(_data, _new_type), do: false

```

Examples:

```elixir
is_type?({:x, "something"}, :x} # returns true
is_type?({:x, "something"}, :y} # returns false

```

---

_[View the full topic](https://forum.devtalk.com/t/how-to-define-macro-for-a-new-type/47300)._
