www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Proposal: Enum types as valid With-statement parameters

reply Chris Nicholson-Sauls <ibisbasenji gmail.com> writes:
I recently found myself writing the following code:

# struct Var {
#
#   // ... other stuff ...
#
#   bool truth () {
#     switch (type) {
#       case VarType.Clear  :
#       case VarType.Error  : return false              ;
#       case VarType.Object : return o !is null         ;
#       case VarType.Int    : return i != 0_L           ;
#       case VarType.Float  : return f != 0.0_L         ;
#       case VarType.String : return s.length != 0      ;
#       case VarType.Symbol : return validSymbol()      ;
#       case VarType.Bool   : return b                  ;
#       case VarType.List   : return l.length != 0      ;
#       case VarType.Map    : return m.keys.length != 0 ;
#       case VarType.Frob   : return true               ;
#     }
#   }
#
#   // ... other stuff ...
#
# }

It would have been nice to be able to write this as:

# struct Var {
#
#   // ... other stuff ...
#
#   bool truth () {
#     with (VarType) switch (type) {
#       case Clear  :
#       case Error  : return false              ;
#       case Object : return o !is null         ;
#       case Int    : return i != 0_L           ;
#       case Float  : return f != 0.0_L         ;
#       case String : return s.length != 0      ;
#       case Symbol : return validSymbol()      ;
#       case Bool   : return b                  ;
#       case List   : return l.length != 0      ;
#       case Map    : return m.keys.length != 0 ;
#       case Frob   : return true               ;
#     }
#   }
#
#   // ... other stuff ...
#
# }

Walter: how difficult would it be to allow named Enum types as valid
With-statement 
parameters?  When named Enums are used in tandem with constructs such as
Switch, this 
could save quite a lot of typing, and is still perfectly clear when reading
code.

-- Chris Nicholson-Sauls
Jul 20 2006
next sibling parent xs0 <xs0 xs0.com> writes:
 Walter: how difficult would it be to allow named Enum types as valid 
 With-statement parameters?  When named Enums are used in tandem with 
 constructs such as Switch, this could save quite a lot of typing, and is 
 still perfectly clear when reading code.
 
 -- Chris Nicholson-Sauls

Actually, there's no need for that with(). If the switch value of of a named enum type, the corresponding constants could automatically become available for cases. It could be even better, if no other value was allowed (except default), as it's almost by definition a mistake? xs0
Jul 21 2006
prev sibling parent Deewiant <deewiant.doesnotlike.spam gmail.com> writes:
Chris Nicholson-Sauls wrote:
 Walter: how difficult would it be to allow named Enum types as valid
 With-statement parameters?  When named Enums are used in tandem with
 constructs such as Switch, this could save quite a lot of typing, and is
 still perfectly clear when reading code.
 

I asked for this myself a while back: http://www.digitalmars.com/d/archives/digitalmars/D/37925.html It would indeed be a handy feature.
Jul 22 2006