Welcome! Please see the About page for a little more info on how this works.

+1 vote
in Clojure by

Probably trivial but I'm new to Clojure and couldn't figure it out.

Why does this not evaluate and throws the error:
clojure.lang.ExceptionInfo: Could not resolve symbol: defstruct

(ns StructMaps)

(defn Pets
  []
  (defstruct pets :PetType :PetName)
  (def myPet (struct pets "dog" "Fido"))
  (println myPets)
  )

(Pets)

Thanks for helpful answers!

by
I wonder why people are still using defstruct while there's defrecord? I'd be curious what brought you to this choice.
by
I will guess: OP follows either TutorialsPoint Clojure tutorial: https://www.tutorialspoint.com/clojure/clojure_struct.htm or Udemy Clojure: The Complete Beginner's Guide 2023 https://www.udemy.com/course/clojureprogramming/ ... both are extremely bad.
by
Probably also worth mentioning:

CamelCase is not idiomatic for Clojure bindings, so we'd have (ns struct-maps), (defn pets ..), (def my-pet ..), and so on. We do use CamelCase for things that are like "type" names or generate a Java class under the hood.

def (and other def* things) create top-level definitions so we don't use them inside functions. If you're following a tutorial that does this, then it's a bad tutorial and you should stop following it and find something better.

As others have noted, structs have been deprecated for a long time and even tho' records exist as a modern replacement, we generally use plain old hash maps -- it's something you'll have to get used to coming from statically typed OOP languages: Clojure is about plain data most of the time.

1 Answer

0 votes
by

I can't reproduce this, when running with clj -M StructMaps.clj except that there's a typo: myPet should probably be myPets?

...