My favorites | Sign in
Project Home Wiki Issues Source
READ-ONLY: This project has been archived. For more information see this post.
Search
for
  Advanced search   Search tips   Subscriptions
Issue 11: PROPOSAL: improve enum can't be dynamically instantiated with type-safety and least verbosity
1 person starred this issue and may be notified of changes. Back to list
 
Project Member Reported by she...@coolpage.com, Mar 1, 2010
This was originally reported as a proposal for the HaXe 2.04 language:

https://code.google.com/p/haxe/issues/detail?id=91

My comments from that issue have been copied here, so to the extent that
Copute may support HaXe as an output target, then the context of the
proposal relative to HaXe is recorded.
Mar 1, 2010
Project Member #1 she...@coolpage.com
There is no way to dynamically (enum type selected at run-time) instantiate an enum
without using a type-unsafe (Dynamic arguments) and switch.  This is boilerplate
coding which the compiler should be able to do and make it type-safe.  I have shown
how it should work (see single line "var morphed"):

http://copute.com/dev/docs/Copute/ref/class.html#Enumerated_Inheritance

The only way that single line can currently be done in HaXe is:

var morphed = clone<int>( c, [0xFF, Rgb<int>( 0, 0, 0 )] )

function clone( c : Color<T>, args : Array<Dynamic> ) : Color<T>
{
  switch( c )
  {
  case Rgb<T>( r, g, b ):
     return Alpha<T>( args[0], args[1], args[2] )
  case Alpha<T>( a, c ):
     return Alpha<T>( args[0], args[1] )
  default:
  }
}

I came across this issue when designing a compiler (for Copute) where I want each
terminal token to be an enum, and I have hard-coded the map of token regular
expressions (e.g. [0-9] for Terminal.Int) to these enums from the grammar, but the
enum is created at run-time by the lexer to fill it with the value from the regular
expression.

So I instead of forcing me to make an unnecessary giant switch (which has to be
maintained every time I add a new constructor to the enum Color<T>), which is not
type-safe, I prefer to suggest the optimization at the link above.  Any way, I am
just sharing, if you do not like this, then do not implement it.
Labels: -Type-Defect Type-Enhancement
Mar 4, 2010
Project Member #3 she...@coolpage.com
I see Nicolas CannASSe marked this "Won't Fix" for HaXe (he continues to fight all my
contribution to HaXe):

https://code.google.com/p/haxe/issues/detail?id=91#c1

No worries, I will implement it in Copute.  I was only sharing and trying to help.


Powered by Google Project Hosting