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

  • Home
  • SEARCH
  • 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 891935
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T13:58:12+00:00 2026-05-15T13:58:12+00:00

I’m trying out Clojure 1.2, specifically mutable fields which are supported in deftype according

  • 0

I’m trying out Clojure 1.2, specifically mutable fields which are supported in deftype according to the clojure.org documentation.

But I can’t get the set to work. What is the syntax for updating a field? Or isn’t mutability implemented yet?

(definterface IPoint
  (getX [])
  (setX [v]))

(deftype Point [x]
  IPoint
  (getX [this] x)
  (setX [this v] (set! (.x this) v)))

user=> (def p (Point. 10))
user=> (.getX p)
10
user=> (.setX p 20)
ClassCastException: user.Point cannot be cast to compile__stub.user.Point

Using a 1.2 snapshot from a few days ago.

  • 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-15T13:58:13+00:00Added an answer on May 15, 2026 at 1:58 pm

    deftype‘s default is still to have the fields be immutable; to override this, you need to annotate the names of the fields which are to be mutable with appropriate metadata. Also, the syntax for set! of instance fields is different. An example implementation to make the above work:

    (deftype Point [^{:volatile-mutable true} x]
      IPoint
      (getX [_] x)
      (setX [this v] (set! x v)))
    

    There’s also :unsynchronized-mutable. The difference is as the names would suggest to an experienced Java developer. 😉 Note that providing either annotation has the additional effect of making the field private, so that direct field access is no longer possible:

    (.getX (Point. 10)) ; still works
    (.x (Point. 10))    ; with annotations -- IllegalArgumentException, works without
    

    Also, 1.2 will likely support the syntax ^:volatile-mutable x as shorthand for ^{:volatile-mutable true} x (this is already available on some of the new numerics branches).

    Both options are mentioned in (doc deftype); the relevant part follows — mind the admonition!

    Fields can be qualified
    with the metadata :volatile-mutable true or :unsynchronized-mutable
    true, at which point (set! afield aval) will be supported in method
    bodies. Note well that mutable fields are extremely difficult to use
    correctly, and are present only to facilitate the building of higher
    level constructs, such as Clojure’s reference types, in Clojure
    itself. They are for experts only – if the semantics and
    implications of :volatile-mutable or :unsynchronized-mutable are not
    immediately apparent to you, you should not be using them.

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

Sidebar

Ask A Question

Stats

  • Questions 449k
  • Answers 449k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Using a modern ver of mysql... Create table Tz_offsets: col… May 15, 2026 at 8:07 pm
  • Editorial Team
    Editorial Team added an answer I don't think you need to do anything else. You… May 15, 2026 at 8:07 pm
  • Editorial Team
    Editorial Team added an answer getJSON is asynchronous. Do this instead: jQuery.getJSON('/data/stats.json', function(chart_data) { var… May 15, 2026 at 8:07 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.