ironscheme

IronScheme: How do you 'or' enumeration entries


How do you 'or' enum entries in IronScheme, ex:

(import                                                                                                                                               
  (rnrs)                                                                                                                                               
  (ironscheme clr))                                                                                                                                    

(clr-using System.Reflection)                                                                                                                         

(define public-class-attributes                                                                                                                       
  (bitwise-ior                                                                                                                                        
    (clr-static-field-get                                                                                                                              
      TypeAttributes Public)                                                                                                                            
    (clr-static-field-get                                                                                                                              
      TypeAttributes Class)))                                                                                                                           

(display public-class-attributes)

This causes an error, I haven't found an alternative in the documentation.


Solution

  • I am not sure what your use case is, but as mentioned in the comment, when using clr-call a list of symbols can be used for an OR'd enum. Example here.

    Internally, the compiler will wrap the list with a call to Helpers.SymbolToEnum<T>().

    Note: The case is ignored.

    To illustrate in a small example:

    C# code:

    [Flags]
    enum Foo
    {
      Bar = 1,
      Baz = 2
    }
    
    class AType 
    {
      static void Kazaam(Foo foo) { ... }
    }
    

    Scheme code:

    ; same as Bar | Baz
    (clr-static-call AType Kazaam '(Bar Baz))
    
    ; single value
    (clr-static-call AType Kazaam 'Baz)
    ; same thing
    (clr-static-call AType Kazaam '(Baz))
    
    ; no value (iow zero)
    (clr-static-call AType Kazaam '())
    

    If these are just simple flag, lists should suffice, else you can redefine the enum as an enum-set in Scheme which allows many set operations. Finally, you just use enum-set->list to get the list to pass as an argument as shown above.