If you really want a solution and want it now, here is a solution using the 
unix-based m4 text/macro processor -- don't know if this has been mentioned 
in the other threads on this topic.   It should be possible to write a file 
like so (maybe named *YourFile.elm.m4*):


## The following is the only line that lists the state names!
define(`stateList', ``Alabama', `Alaska', `Arizona', `Arkansas'')dnl

type AState = m4AltSep(stateList)

allAStates = m4ListStates(stateList)

allAStateNames = m4ListStrings(stateList)

When you run it through m4 with the right macro definitions, you will get 
this:


## The following is the only line that lists the state names!

type AState = Alabama | Alaska | Arizona | Arkansas

allAStates = [ Alabama, Alaska, Arizona, Arkansas ]

allAStateNames = [ "Alabama", "Alaska", "Arizona", "Arkansas" ]


And the required definitions are (maybe this can be in *elmdefs.m4*):

define(`m4foreach',`ifelse(eval($#>2),1,
`pushdef(`last_$1',eval($#==3))dnl
`'pushdef(`$1',`$3')$2`'popdef(`$1')dnl
`'popdef(`last_$1')dnl
`'ifelse(eval($#>3),1,`$0(`$1',`$2',shift(shift(shift($@))))')')')dnl
define(`m4AltSep', `m4foreach(`xstate', `xstate`'ifelse(last_xstate,0,` | 
')',$@)')dnl
define(`m4ListStates', `[ m4foreach(`xstate', 
`xstate`'ifelse(last_xstate,0,`, ')',$@) ]')dnl
define(`m4ListStrings', `[ m4foreach(`xstate', 
`"xstate"`'ifelse(last_xstate,0,`, ')',$@) ]')dnl

The macros are adapted from M. Breen's page <http://mbreen.com/m4.html#toc5> 
especially 
the section on loops.
The command that makes it work:

cat elmdefs.m4 YourFile.elm.m4 | m4 > YourFile.elm

The price you pay is having to somehow integrate this command into your 
build flow -- it is easy enough using unix *make*.  And if your build tool 
would need to understand that when the .elm.m4 file changes, the 
.corresponding elm file should be rebuilt.  And of course if you are using 
elm-format that will work at the .elm level and it will not understand the 
.elm.m4 level.  Finally because it is a source-level modification, it does 
not resolve any of the semantic issues that have been raised in this 
thread.  It just deals with the very narrow enum case.  Given this extra 
work to set this up, some may opt to just live with the risk of getting 
your lists/enumerations out of sync, ... this can only be practical when 
your codebase has a lot of Enums like this which need to be kept in sync.

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

Reply via email to