Discussion:
[elm-discuss] Is there an Elm window.print()?
Casper Bollen
2017-07-07 07:39:40 UTC
Permalink
Is there an Elm way to implement window.print() without using a javascript
port?
--
You received this message because you are subscribed to the Google Groups "Elm Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elm-discuss+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Frank Bonetti
2017-07-07 15:29:55 UTC
Permalink
The only other way is to create a "native" module, which is an undocumented
and unstable API. Here's an example:

https://github.com/elm-lang/persistent-cache/blob/master/src/Native/LocalStorage.js

Ports are currently your best option.
Post by Casper Bollen
Is there an Elm way to implement window.print() without using a javascript
port?
--
You received this message because you are subscribed to the Google Groups "Elm Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elm-discuss+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Alex Barry
2017-07-08 15:00:56 UTC
Permalink
Is there a good reason *not* to use a port for this? The difficultly level
is very low, and t's a single js function call.

Also, don't even consider a native module at all. The general consensus is
that native code isn't meant for the general Elm population, because it has
the ability to absolutely break all guarantees that Elm initially promises.

One main issue here is that Elm, being a functional language, needs a value
returned after a function call. Sure, you could have something like `print
: () -> Bool`, but that's not quite correct because you are adding extra
meaningless data (ie passing a tuple and returning a bool, both unrelated
to print). As a result, it's much better suited to be a Task (port
function), since tasks don't return values, and there is no logical thing
to return.

From personal experience, don't even consider native modules unless:

1. Elm does not have the ability to support the feature you want.
2. Sending a message to run a task makes performing the function awkward.

99% of the time for printing the screen, it's because the user is clicking
a button on the page, and that's a super simple update from Elm.
Post by Casper Bollen
Is there an Elm way to implement window.print() without using a javascript
port?
--
You received this message because you are subscribed to the Google Groups "Elm Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elm-discuss+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Marek Fajkus
2017-07-08 17:26:05 UTC
Permalink
I agree with Alex in all his points - ports are a better way to achieve
this. Btw () (0-tuple) is unit type
(https://en.wikipedia.org/wiki/Unit_type). *I'm saying that only so there
is any additional value in my comment other than +1 for Alex*
--
You received this message because you are subscribed to the Google Groups "Elm Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elm-discuss+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Alex Barry
2017-07-08 18:32:52 UTC
Permalink
Also to expand on why I used an empty tuple - Elm, from my understanding,
will cache the call if it just returns a Bool, because it ends up looking
like a constant. I don't know if, as a result, Elm will actually execute
the function a second time, or if it will just return the boolean value
from the first run of the call. The empty tuple forces elm to see this as a
function rather than a constant, but it's a hack, and not a good practice.
Post by Marek Fajkus
I agree with Alex in all his points - ports are a better way to achieve
this. Btw () (0-tuple) is unit type (
https://en.wikipedia.org/wiki/Unit_type). *I'm saying that only so there
is any additional value in my comment other than +1 for Alex*
--
You received this message because you are subscribed to the Google Groups "Elm Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elm-discuss+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Continue reading on narkive:
Loading...