Confused about ports and initial values

549 views
Skip to first unread message

Alex Spurling

unread,
Jul 31, 2015, 1:55:01 PM7/31/15
to Elm Discuss
Hi,

I have the following program:


When I first loaded InitialValue.elm I saw this error:

Port Error:

No argument was given for the port named 'timestamp' with type:

Int

You need to provide an initial value!

Find out more about ports here <http://elm-lang.org/learn/Ports.elm>

Open the developer console for more details.

So I changed

port timestamp : Int

to

port timestamp : Int
port timestamp = 5

However, when I load InitialValue.elm via the elm reactor, I simply see "HelloNaN" (when I expect to see "Hello7").

If I now load index.html which provides its own default value, I see this error:

Port Error:

You provided an argument named 'timestamp' but there is no corresponding port!

Maybe add a port 'timestamp' to your Elm module?
Maybe remove the 'timestamp' argument from your initialization code in JS?

Open the developer console for more details.

If I remove the line port timestamp = 5 and then load the app via index.html, the application correctly displays the current time. However, when I load the .elm file directly, it fails with the original error above.

My questions:

1) Can we please add how to initialise the value of a port to the documentation?
2) Can we change the error message saying 'there is no corresponding port' to one that can actually detect the initialisation of a port?
3) Why does my application compile when giving the port a default value such as '5' when in fact the port returns an invalid Integer? (as I've read from searching, Evan once said that NaN and infinity are not numbers in Elm).

Thanks,

Alex

Kaspar Emanuel

unread,
Jul 31, 2015, 2:11:44 PM7/31/15
to elm-d...@googlegroups.com

Hi Alex,

you are initialising the port in index.html here with new Date().getTime().

When you declare

port timestamp : Int
port timestamp = 5

You are declaring an outgoing port with value 5. That this is interpreted as NaN is indeed an oversight by the compiler. Your program shouldn’t compile in my opinion. I think your feedback on the error messages is valuable.

Your gist works fine with line 6 in the elm file removed.

Cheers,

Kaspar


--
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.

Corey Trampe

unread,
Jul 31, 2015, 2:14:54 PM7/31/15
to elm-d...@googlegroups.com
Give your port a type of Signal Int.
Remove the line that assigns the number 5. (You're initializing it correctly in index.html.)

Map over the signal. It's not like a variable from a higher scope, it's a like another reality that you have to send your logic into.

    main = Signal.map show timestamp


--

Max Goldstein

unread,
Jul 31, 2015, 2:26:07 PM7/31/15
to Elm Discuss, corey...@gmail.com
Oh, interesting! I'm not sure we ever thought about catching the case where a port is initialized as incoming and outgoing. Yup, one more bug to detect statically...
Reply all
Reply to author
Forward
0 new messages