code-reviews

2020-04-07T13:45:50.003700Z

Is there a more idiomatic way to do this?

> (defn lang-tag [s tag]
   (with-meta (reify java.lang.Object (toString [this] s)) {:lang tag}))
lang-tag
> (def gaol (lang-tag "gaol" :en-uk))
gaol
> (str gaol)
"gaol"
> (meta gaol)
{:lang :en-uk}
> (def jail (lang-tag "jail" :en-us))
jail
> ;; etc

2020-04-07T15:28:40.004Z

@eric.d.scott one alternative

(defn lang-tag
  [s tag]
  ^{:long tag} (reify Object (toString [this] s)))

2020-04-07T15:29:01.004500Z

Object (and most of java.lang) is already in scope, so doesn't need the full package

2020-04-07T15:29:29.005100Z

typically I only use with-meta inside macros in order to emit a meta transform, the reader macro is much simpler otherwise

2020-04-07T16:07:51.005300Z

Thanks!

2020-04-07T16:09:37.006300Z

But the general idea of sticking metadata to a string by reifying Object with (toString...) is generally considered legit?

2020-04-07T16:39:42.006700Z

yes, that's pretty much how it would need to be done

2020-04-07T16:40:00.007300Z

if you need other operations, you would need to reify CharSequence or something instead...

2020-04-07T16:40:18.007700Z

but it's probably better to own a string as a field than to try to be one

1👍