How to define a function that takes no input

755 views
Skip to first unread message

Pabich Pawel

unread,
Feb 23, 2016, 9:30:57 AM2/23/16
to Elm Discuss
Hi,

I started playing with Elm a couple of days ago and I can't stop :).

I'm trying to define a function that always returns the same data and takes no input parameters. How would I define it in Elm ?

Thanks

Pawel

Pabich Pawel

unread,
Feb 23, 2016, 9:36:14 AM2/23/16
to Elm Discuss
It looks like I found it:

init: a -> Model
init = always 0

Bobby Eickhoff

unread,
Feb 23, 2016, 9:36:52 AM2/23/16
to Elm Discuss
I'm curious -- since the function takes no input parameters and always returns the same data, why use a function?  That sounds like a constant.

Pabich Pawel

unread,
Feb 23, 2016, 9:42:22 AM2/23/16
to Elm Discuss
Hi Bobby,

I wanted to use this to implement the init method (same approach that is used in elm architecture). Just in my case I don't any input. I'm not familiar with constants. 
Would you mind showing me how you would it ?

Bobby Eickhoff

unread,
Feb 23, 2016, 9:57:32 AM2/23/16
to Elm Discuss
Depending on the component you are building its init could be a function or a constant.  StartApp doesn't require it to be a function, though a function is often appropriate.

If you're not taking any parameters, you could just define the value:

init = 0



In that case you would use it directly:

StartApp.start
 
{ init = init
 
...
 
}



Alternatively, as you already showed, you could use always to make init a function which always returns a certain value:

init =
  always
0



In that case, you would need to pass it a (dummy) argument:

StartApp.start
 
{ init = init ()
 
...
 
}

Thomas Weiser

unread,
Feb 23, 2016, 9:58:02 AM2/23/16
to elm-d...@googlegroups.com

A function that takes no arguments is a constant.

type alias Model = Int

init : Model
init = 17
--
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.

Max Goldstein

unread,
Feb 23, 2016, 10:13:49 AM2/23/16
to Elm Discuss
I'll agree with most posters that you just want to define a constant.

always is a function defined in Basics: always thing _ -> thing

You can also look at the unit type/value (). It's a type with only one value, so that value doesn't convey any information. It gets used as a placeholder.

But again, for your case, just define a constant.

Joey Eremondi

unread,
Feb 23, 2016, 11:49:27 AM2/23/16
to elm-d...@googlegroups.com

It is important to distinguish a function that takes no input from a function that ignores its input. "always" gives the latter, and the former is just a constant.

--

Pabich Pawel

unread,
Feb 23, 2016, 6:10:26 PM2/23/16
to Elm Discuss
Thanks guys.

This is exactly what  I was looking for.
Reply all
Reply to author
Forward
0 new messages