What is the meta method in Clojure?

Overview

Metadata is data about data. This means metadata is a description of data, but not the content of the data.

What is the meta method?

The meta method checks to see if any metadata is associated with an object. It helps to get the metadata of an object. We'll need to first check if it has any.

Syntax

(meta obj)
Syntax for meta data

Parameter

The meta the method receives one parameter only:

  1. Obj: This is the object we check to see if it has any associated metadata.

Return value

This method returns the associated metadata of the object. if the object has no associated metadata, it returns nil.

Example

Let's see the example below:

(ns clojure.examples.example
(:gen-class))
(defn metta []
(def d-map (with-meta [1 2 3] {:prop "values"}))
(println(meta d-map)))
(metta)

Explanation

From the code above:

  • Line 3: We define a metta function.
  • Line 4: We define an object and attach a meta data {:prop "values"} using the with-meta method.
  • Line 5: We print the returned metadata using println. We get metadata of the object using the meta method.
  • Line 6: We call the metta function.

Free Resources