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).
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
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
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.
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?
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
This is a good way to do it: https://github.com/andrewmcveigh/cljs-time/blob/master/src/cljs_time/extend.cljs#L1-L2
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 advanceA 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 What are you using to generate the HTML for your DOM?\r\n
to work though.
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.
Oh derp, no, HTML treats \r\n
like a single space character unless you’re inside a <pre>
block or similar.
Ah I see
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
[from-api]
(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>
<div>Lorem ipsum</div>
<div>dolor sit amet</div>
<div>Quisque nisl eros.</div>
</div>
=> nil
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.
Couple extra lines, but it makes React feel safer.
Thanks @manutter51 for taking your time helping me out :)
I’ve tried using there function I am getting Use of undeclared Var div
did you try https://developer.mozilla.org/en-US/docs/Web/CSS/white-space ?
ah, nevermind, saw you were processing html line breaks too.
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. 😄