Fork me on GitHub
Adam Helins16:02:37

Can Closure figure out that type extensions are never used and remove related code? (eg. using extend-type with a protocol in a library but the user ends up not using that).

Adam Helins16:02:25

I would say no based on what I see but who knows


yes in theory. in practice unlikely though. CLJS isn't "typed" enough for this to work reliably

Adam Helins16:02:21

And I guess there is no way to help the process? Some type annotation somewhere?


depends. can't explain the process really. yes, typehints and eliminating dynamic uses of the protocol


basically you'll have to spend a bunch of time going through pseudo-named code looking for reasons why the code stays alive when you think it should be removed


closure is surprisingly good but many of the more advanced features really only work in fully typed code


but thats like tweaking the last 10% when you already get 90% of the benefit

👍 3
Adam Helins16:02:51

So essentially, when extending protocols, dead code is rather to be expected on native types and custom ones that are used at least once somewhere


hard to say. really depends on how you use it but I'd say that is accurate yes.

Adam Helins17:02:04

Makes one slightly paranoid about overusing protocols


well you are using protocols with the intent of using them no? I mean you don't intend to write dead code from the start?

Adam Helins17:02:35

In the context of an app yes indeed, in the context of a lib it's best to think twice and try no to be too clever


Hello there, I am new Clojure(script) world. I needed a little with clojurescript, so I am fetching information from data api which contain html tags <br/> \r\n that are in hashmap as values. This is so that lines breaks after at desired sentence in HTML dom. I expect the line to break everytime this <br/> \r\n , however this is what I am getting

Whenever has this tag <br/>
This is what i get 
Lorem ipsum dolor sit amet, consectetur adipiscing elit. <br/> Quisque nisl eros, pulvinar facilisis justo mollis.
this is when I have \r\n
This is what i get 
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque nisl eros, pulvinar facilisis justo mollis.
Thanks in advance


A lot of HTML rendering engines are reluctant to render “external” strings as HTML due to the danger of hacking, so they render “</br>” as a literal string </br> instead of as a break. I would expect the \r\n to work though. What are you using to generate the HTML for your DOM?


Oh derp, no, HTML treats \r\n like a single space character unless you’re inside a <pre> block or similar.


Ah I see


I have a shadow cljs react application with helix dom


Ok, React definitely won’t let you inject a “</br>” into your DOM. At the javascript level it will let you call dangerouslySetInnerHtml() to do something like that, but I wouldn’t take the chance myself. I haven’t used helix, let me look that up real quick and I’ll make a suggestion.


Ok, quick and dirty, here’s an example with my fake d/div function

(def test-string "Lorem ipsum</br>dolor sit amet\r\nQuisque nisl eros.")
=> #'user/test-string
(defn my-block
  (let [lines (clojure.string/split from-api #"\r\n|\n|</br>")]
    (apply div (for [line lines] (div line)))))
=> #'user/my-block
(print (my-block test-string))
  <div>Lorem ipsum</div>
  <div>dolor sit amet</div>
  <div>Quisque nisl eros.</div>
=> nil


Thanks @U06CM8C3V for taking your time helping me out :)

👍 3

I’ve tried using there function I am getting Use of undeclared Var div


Oh, sorry, whatever you’re using to generate your DOM HTML. I was looking at the docs for helix, and I saw they were using a function d/div, so I made a fake/demo version of it because I didn’t want to build a whole helix project just for a quick demo. 😄

👍 3

Ah I see thanks Man


Basically what I did was use clojure.string/split plus a regular expression to split the incoming string into separate strings wherever there was a \r\n or a </br>, and then render each string separately.


ah, nevermind, saw you were processing html line breaks too.


Couple extra lines, but it makes React feel safer.