Re: [elm-discuss] Re: Sending tagged unions through ports

2017-03-08 Thread Martin Norbäck Olivers
I just made an experiment with having the compiler generate a representation of 
an Elm type that could then be used to automatically generate for instance 
Jason encoders and decoders.

The compiler extension is not done but you can see the experiment at 
https://github.com/norpan/elm-dynamic-types and I encode unions like this

{ConstructorName: [values]}

But since it's just Elm functions it would be easy to change given a decent API 
to the json stuff there.

-- 
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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [elm-discuss] Re: Sending tagged unions through ports

2017-03-03 Thread Eirik Sletteberg
That looks a bit clumsy to use from JS land though... if you want to read 
numberOfDoors you'll have to loop through all the fields to find the 
correct one? The format has to be both correct and easy to work with.

fredag 3. mars 2017 19.42.52 UTC+1 skrev Rupert Smith følgende:
>
> On Friday, March 3, 2017 at 4:35:00 PM UTC, Eirik Sletteberg wrote:
>>
>> type alias Car =
>>   { numberOfDoors: Int
>>   }
>>
>> type alias Plane =
>>   { maxSpeed: Int
>>   }
>>
>> type Transport = Walk
>>   | Ride Car
>>   | Fly Plane
>>
>
> Or could be mapped to something that can be described with a json-schema 
> enum for each 'tag' in the union type:
>
> { _type: "Walk" }
>
> { _type: "Ride"
> , fields : [ {numberOfDoors: 1} ]
> }
>
> { _type: "Fly"
> , fields: [ { maxSpeed: 100 } ]
> }
>
> Where 'fields' is a list of the constructor arguments. Would be nice if I 
> could always find a 1:1 mapping between the Elm type and a json-schema.
>
>
>

-- 
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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [elm-discuss] Re: Sending tagged unions through ports

2017-03-03 Thread 'Rupert Smith' via Elm Discuss
On Friday, March 3, 2017 at 4:35:00 PM UTC, Eirik Sletteberg wrote:
>
> type alias Car =
>   { numberOfDoors: Int
>   }
>
> type alias Plane =
>   { maxSpeed: Int
>   }
>
> type Transport = Walk
>   | Ride Car
>   | Fly Plane
>

Or could be mapped to something that can be described with a json-schema 
enum for each 'tag' in the union type:

{ _type: "Walk" }

{ _type: "Ride"
, fields : [ {numberOfDoors: 1} ]
}

{ _type: "Fly"
, fields: [ { maxSpeed: 100 } ]
}

Where 'fields' is a list of the constructor arguments. Would be nice if I 
could always find a 1:1 mapping between the Elm type and a json-schema.


-- 
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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [elm-discuss] Re: Sending tagged unions through ports

2017-03-03 Thread Peter Damoc
On Fri, Mar 3, 2017 at 6:18 PM, 'Rupert Smith' via Elm Discuss <
elm-discuss@googlegroups.com> wrote:

> On Friday, March 3, 2017 at 3:29:34 PM UTC, Peter Damoc wrote:
>>
>> It makes sense to focus on other issues that are more important BUT,
>> people are having enough troubles with boilerplate introduced by decoders
>> that this could easily be considered a priority.
>>
>
> +1 from me, just for the convenience of being able to do it.
>
> What would the default mapping for a union type to json looks like?
>
> A union type would be specific to Elm so, it would matter less the actual
format as long as it is consistent.

Currently, union types are encoded as js objects that would look like this:
 { "ctor": "Tag", "_0": "first value", "_1":"second value", ... }  if
serialized.
That looks reasonable to me.



-- 
There is NO FATE, we are the creators.
blog: http://damoc.ro/

-- 
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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [elm-discuss] Re: Sending tagged unions through ports

2017-03-03 Thread Brian Hicks
> ['Ride', {
>   numberOfDoors: 4
> }]
> 
> or ['Fly', {
>   maxSpeed: 1000
> }]

Personal experience: I think that tends to be too confusing when the object 
(inevitably) gets separated from the list. It all needs to be in one piece. I 
like to use fields beginning in underscores for this, like so:

Encode.object
   [ ( "_kind", Encode.string "benchmark" )
   , ( "name", Encode.string name )
   , ( "status", encodeStatus status )
   ]

-- 
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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [elm-discuss] Re: Sending tagged unions through ports

2017-03-03 Thread Eirik Sletteberg
type alias Car =
  { numberOfDoors: Int
  }

type alias Plane =
  { maxSpeed: Int
  }

type Transport = Walk
  | Ride Car
  | Fly Plane

Could be encoded as [name, encoded] or just [name] if it's only an 
identifier without a value.
So they would be encoded as

['Walk']

or

['Ride', {
  numberOfDoors: 4
}]

or ['Fly', {
  maxSpeed: 1000
}]

fredag 3. mars 2017 17.18.02 UTC+1 skrev Rupert Smith følgende:
>
> On Friday, March 3, 2017 at 3:29:34 PM UTC, Peter Damoc wrote:
>>
>> It makes sense to focus on other issues that are more important BUT, 
>> people are having enough troubles with boilerplate introduced by decoders 
>> that this could easily be considered a priority. 
>>
>
> +1 from me, just for the convenience of being able to do it.
>
> What would the default mapping for a union type to json looks like? 
>

-- 
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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [elm-discuss] Re: Sending tagged unions through ports

2017-03-03 Thread 'Rupert Smith' via Elm Discuss
On Friday, March 3, 2017 at 3:29:34 PM UTC, Peter Damoc wrote:
>
> It makes sense to focus on other issues that are more important BUT, 
> people are having enough troubles with boilerplate introduced by decoders 
> that this could easily be considered a priority. 
>

+1 from me, just for the convenience of being able to do it.

What would the default mapping for a union type to json looks like? 

-- 
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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [elm-discuss] Re: Sending tagged unions through ports

2017-03-03 Thread Brian Hicks
I could have phrased this more carefully. I meant that we can't blithely pass 
random strings out to JavaScript without some formality of defining that API. 
My point about type systems is only salient so far as to say "JS doesn't have 
union types so there's no direct mapping."

> It makes sense to focus on other issues that are more important BUT, people 
> are having enough troubles with boilerplate introduced by decoders that this 
> could easily be considered a priority.

I agree. Increased ergonomics for interop are important. I don't really care if 
that's static code generation, automatic en/decoders, or something else 
entirely. But the fact that questions like this keep coming up tells me that 
the user experience around ports and value passing could improve. Same goes for 
JSON, actually! I suspect solving one will heavily inform the other.

-- 
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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [elm-discuss] Re: Sending tagged unions through ports

2017-03-03 Thread Peter Damoc
On Fri, Mar 3, 2017 at 4:38 PM, Brian Hicks  wrote:

> It makes sense (at least to me) that this isn't supported. Elm has a much
> different type system than JavaScript.
>

I respectfully disagree.
It makes sense to focus on other issues that are more important BUT, people
are having enough troubles with boilerplate introduced by decoders that
this could easily be considered a priority.
The fact that Elm and JS have vastly different type systems is irrelevant.
JS wanting to talk with Elm through ports could conceivably use the proper
encoding or some kind of help functions that would create the proper types.

It makes very little sense to force programmers to write code about
something that the compiler already knows.
Sure, keep the encoding and decoding libraries for situations when custom
processing is required BUT, if the marshaling process can be automated, it
should be automated.


-- 
There is NO FATE, we are the creators.
blog: http://damoc.ro/

-- 
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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[elm-discuss] Re: Sending tagged unions through ports

2017-03-03 Thread Brian Hicks
It makes sense (at least to me) that this isn't supported. Elm has a much 
different type system than JavaScript. I could see converting them in a 
standard way, but that's a ways off yet.

For now, try encoding to a Json.Encode.Value - you can send that through a 
port, and it has a standard representation on the JavaScript side. Plus, 
when you change your model you get to decide whether or not to break 
compatibility with your encoder. It codifies the contract between the two 
sides in the specific case of your code.

On Friday, March 3, 2017 at 8:22:12 AM UTC-6, Eirik Sletteberg wrote:
>
> An example use case for this would be when gradually porting an existing 
> Redux-based app to Elm.
> One could rewrite state handling from Redux into Elm updaters/messages, 
> and wrap the Elm app's main updater, so that after every message is passed 
> through the updater, it sends the whole state tree through a port, which 
> would then be picked up by the legacy application's view (it could consist 
> of React components, for example).
> Or one could imagine mapping the state into multiple "ViewModel" objects, 
> and dispatch them to different ports (one port per ViewModel type?) but 
> whenever you try to send a tagged union type, there's no support for that 
> yet.
>

-- 
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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.