compliment icon indicating copy to clipboard operation
compliment copied to clipboard

"Unmunge" deftype field names

Open tsdh opened this issue 8 years ago • 3 comments

When you have a Clojure type like

(deftype Foo [x-y] ...)

then (.x completes to (.x_y instead of (.x-y. That's because for a deftype a class is generated, and the field names of the class are the "munged" names of the deftype's fields, i.e., hypens are replaced with underscores.

In Clojure on the JVM, (.x_y foo) actually works but it relies on this very implementation detail and is not portable to the CLR or ClojureScript. Thus, it's better to use (.x-y foo) as this is the documented way to access fields of a deftype.

So it would be nice if CIDER could directly suggest the correct version when completing. The fact that can be exploited is that all classes (and only those) generated for a deftype implement clojure.lang.IType (see https://github.com/clojure/clojure/blob/master/src/clj/clojure/core_deftype.clj#L396). Therefore, I suggest to "unmunge" field names of classes implementing IType.

tsdh avatar Sep 02 '15 12:09 tsdh

So everything that comes from deftype implements IType. Can we be reasonably sure that noone implements IType on their own?

alexander-yakushev avatar Sep 02 '15 13:09 alexander-yakushev

Yes, we may assume that. IType is just a marker interface for exactly this purpose.

tsdh avatar Sep 02 '15 13:09 tsdh

Yeah, that'd be nice.

bbatsov avatar Mar 08 '16 12:03 bbatsov

Didn't even take 8 years 🎉

alexander-yakushev avatar Aug 08 '23 13:08 alexander-yakushev

Nice!

bbatsov avatar Aug 08 '23 16:08 bbatsov