Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7458637
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T13:28:26+00:00 2026-05-29T13:28:26+00:00

Coming from a Java background, I’m quite fond of static type safety and wonder

  • 0

Coming from a Java background, I’m quite fond of static type safety and wonder how clojure programmers deal with the problem of data format definitions (perhaps not just types but general invariants, because types are just a special case of that.)

This is similar to an existing question “Type Safety in Clojure”, but that one focuses more on the aspect of how to check types at compile time, while I’m more interested in how the problem is pragmatically addressed.

As a practical example I’m considering an editor application which handles a particular document format. Each document consists of elements that come in several different varieties (graphics elements, font elements etc.) There would be editors for the different element types, and also of course functions to transform a document from/to a byte stream in its native on-disk format.

The basic problem I am interested in is that the editors and the read/write functions have to agree on a common data format. In Java, I would model the document’s data as an object graph, e.g. with one class representing a document and one class for each element variety. This way, I get a compile-time guarantee about what the structure of my data looks like, and that the field “width” of a graphics element is an integer and not a float. It does not guarantee that width is positive – but using a getter/setter interface would allow the corresponding class to add invariant guarantees like that.

Being able to rely on this makes the code dealing with this data simpler, and format violations can be caught at compile-time or early at runtime (where some code attempts to modify data that would violate invariants).

How can you achieve a similar “data format reliability” in Clojure? As far as I know, there is no way to perform compile-time checking and hiding domain data behind a function interface seems to be discouraged as non-idiomatic (or maybe I misunderstand?), so what do Clojure developers do to feel safe about the format of data handed into their functions? How do you get your code to error out as quickly as possible, and not after the user edited for 20 more minutes and tries to save to disk, when the save function notices that there is a graphics element in the list of fonts due to an editor bug?

Please note that I’m interested in Clojure and learning, but didn’t write any actual software with it yet, so it’s possible that I’m just confused and the answer is very simple – if so, sorry for wasting your time :).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T13:28:26+00:00Added an answer on May 29, 2026 at 1:28 pm

    I don’t see anything wrong or unidiomatic about using a validating API to construct and manipulate your data as in the following.

    (defn text-box [text height width]
      {:pre [(string? text) (integer? height) (integer? width)]}
      {:type 'text-box :text text :height height :width width})
    
    (defn colorize [thing color]
      {:pre [(valid-color? color)]}
      (assoc thing :color color))
    
    ... (colorize (text-box "Hi!" 20 30) :green) ...
    

    In addition, references (vars, refs, atoms, agents) can have an associated validator function that can be used to ensure a valid state at all times.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm quite new to PHP and coming from a Java background. So here it
I'm quite new to C# (coming from a Java background) and I'm working on
Coming from a Java background it is new for me to deal with the
Coming from a Java background, I'm still don't quite understand the semantics of Objective-C
Background: I'm coming from Java background so not too familiar with Javascript. We are
Coming from a Java background, this is the way I'm thinking: The server provides
Coming from a Java background, this is the way I'm thinking: The server provides
I am relatively new to Objective-C and I am coming from a Java background,
I'm very new to Python (I'm coming from a JAVA background) and I'm wondering
im coming from unix / server / c++ /java background with no GUI experience

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.