Fork me on GitHub
#clojurescript
<
2021-02-15
>
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

thheller16:02:40

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?

thheller16:02:32

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

thheller16:02:21

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

thheller16:02:42

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

thheller16:02:07

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

thheller16:02:12

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

thheller17:02:38

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

fsd20:02:45

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

manutter5120:02:48

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?

manutter5120:02:24

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

fsd20:02:59

Ah I see

fsd20:02:54

I have a shadow cljs react application with helix dom

manutter5120:02:03

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.

manutter5120:02:58

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

fsd21:02:36

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

👍 3
fsd21:02:38

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

manutter5122:02:52

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
fsd14:02:17

Ah I see thanks Man

manutter5120:02:24

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.

dazld21:02:40

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

manutter5120:02:43

Couple extra lines, but it makes React feel safer.