>
> Or are String and Int on   "type MyType = String | Int" just tags?


Yes, exactly!

On Fri, Jun 3, 2016 at 2:53 PM, Lambder <[email protected]> wrote:

> Or are String and Int on   "type MyType = String | Int" just tags?
>
>
> On Friday, 3 June 2016 22:50:34 UTC+1, Lambder wrote:
>>
>> I still like to ask for some clarification.
>>
>> type MyType = String | Int
>>
>> is legal type definition in elm. How to define function which accept one
>> parameter of type MyType and if it is String return it with  'x' appended
>> as a result. If it is a number return its increment?
>> If what I am asking makes no sense, what are the potential usages of non
>> tagged union types?
>>
>> --
>> Thanks
>>
>> On Friday, 3 June 2016 22:25:59 UTC+1, Joey Eremondi wrote:
>>>
>>> The problem here is that you're confusing Types (which you can never
>>> pattern match on) with Value Constructors, which are the only things you
>>> can pattern match on.
>>>
>>> When you declare a "type", you need to give a distinct tag name to each
>>> variant.
>>>
>>> Each "type" declaration has the form
>>>   type Name typeArg1 ... typeArgn = Variant1 | ... | Variant_n
>>> where each variant has the form
>>>   TagName arg1 ... arg_n
>>>
>>> So when you write
>>>    type PageDef a m = ModelAndView a m | Html a
>>> You're saying:
>>>   PageDef is a type constructor that takes two type arguments, a and m.
>>> It has to variants: the first has a tag named ModelAndView, which takes two
>>> arguments, an "a" and an "m". The second is a type constructor named Html,
>>> which takes a single argument of type "a".
>>>
>>> What you probably want is something like this:
>>>   type PageDef a m = TagName1 (MyModelAndView a m) | TagName2 (Html a)
>>>
>>> I don't know what your program does, so I don't know what appropriate
>>> tag names would be.
>>>
>>> Then, to pattern match, you do:
>>> viewChild : PageDef a m -> Html a
>>> viewChild pageDef =
>>>   case pageDef of
>>>     TagName1 (m,v) ->
>>>       v m
>>>     TagName2 h -> h
>>>
>>> Elm only supports Tagged unions: when something can be an A or a B, you
>>> need a tag to indicate which it is.
>>>
>>> Also remember that Types and Value Constructors (tags) exist in totally
>>> separate namespaces! So in your original definition, you had defined two
>>> things, both named MyModelAndView. One was a type alias for (m, View a
>>> m) and the other was a type constructor for the type PageDef.
>>>
>>> The names of types will only occur in type signatures, never in the
>>> bodies of functions. Value constructors can be used in functions, or on the
>>> left-hand side of a pattern match, and will never occur in type signatures.
>>>
>>> It's generally bad practice to share names between these two namespaces,
>>> unless you have a type with a single consructor. For example:
>>>   type NumPixels = NumPixels Int
>>> Defines a new type that is just a shallow wrapper around an Int, that
>>> creates a new type storing an Int. But again, there's no magic here, the
>>> type name NumPixels is completely separate from the tag name NumPixels.
>>>
>>> On Fri, Jun 3, 2016 at 2:12 PM, Daniel Kwiecinski <[email protected]
>>> > wrote:
>>>
>>>> Hi Elmers,
>>>>
>>>>
>>>>    How do I pattern match on aliased types? E.g lets assume I have the
>>>> following type definitions:
>>>>
>>>> type alias View a m = m -> Html a               -- view is a function
>>>> which turns a model (m) into html (Html a)
>>>> type alias ModelAndView a m = (m, View a m)     -- a pair of a model
>>>> and a view
>>>> type PageDef a m = ModelAndView a m | Html a    -- algebraic type for
>>>> unrealised html (model+view) and realised html
>>>>
>>>>
>>>> how do I pattern match on PageDef ?
>>>>
>>>>
>>>> viewChild : PageDef a m -> Html a
>>>> viewChild pageDef =
>>>>   case pageDef of
>>>>     ModelAndView a m as m_v ->
>>>>       case m_v of
>>>>          (m, v) -> v m       -- !!!  GOT AN ERROR HERE !!!
>>>>     Html a as h -> h
>>>>
>>>>  Is it even possible to pattern match directly?
>>>>
>>>> Regards,
>>>> Daniel
>>>>
>>>> --
>>>> 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 [email protected].
>>>> For more options, visit https://groups.google.com/d/optout.
>>>>
>>>
>>> --
> 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 [email protected].
> For more options, visit https://groups.google.com/d/optout.
>

-- 
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 [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to