PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, December 7, 2022

[FIXED] What does Uppercased term inside union means in Elm?

 December 07, 2022     elm, syntax     No comments   

Issue

I'm reading Elm documentation related to syntax, and stumbled upon this snippet:

type User
  = Regular String Int
  | Visitor String

what type Regular and Visitor is? another type that never defined before? or a function?

it does like a function call, but the parameter is a type, it doesn't look like function signature because there's no ->


Solution

This is documented in the relevant section of the Elm Guide (note that this is linked to from the section in the syntax guide that you link to in your question), but I admit it isn't quite as clear to a beginner as it should be.

Regular and Visitor are indeed functions, and not types, in your example. (In Haskell and PureScript they would be called "(data) constructors", which essentially can be used as regular functions but with the extra feature of being usable in pattern matching on the type they're defined in. Elm has quite a few differences from these 2 languages but has much the same roots, and indeed you can do pattern matching in this way in Elm too.)

This is proved by the REPL examples on the page I linked to, from which the following is a verbatim copy and paste:

> type User
|   = Regular String Int
|   | Visitor String
| 

> Regular
<function> : String -> Int -> User

> Visitor
<function> : String -> User

And like all functions in Elm (again as in Haskell etc.) these functions are automatically curried, so in your example Regular "something" is a function of type Int -> User.

As you observe, "it doesn't look like a function signature" - and it's not. This isn't how you define a typical function. It's instead a definition of a custom data type, which then gives you these "constructor functions" for free.



Answered By - Robin Zigmond
Answer Checked By - Senaida (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing