title: How do I use groovy's AS keyword
body: <p>This may be a duplicate but "as" is an INCREDABLY hard keyword to google, even S.O. ignores "as" as part of query.</p>
  
  <p>So I'm wondering how to implement a class that supports "as" reflexively.  For an example class:</p>
  
  <pre>
  class X {
      private val
      public X(def v) {
          val=v
      }
      public asType(Class c) {
          if (c == Integer.class)
              return val as Integer
          if(c == String.class)
              return val as String
      }
  }
  </pre>
  
  <p>This allows something like:</p>
  
  <pre><code>new X(3) as String
  </code></pre>
  
  <p>to work, but doesn't help with:</p>
  
  <pre><code>3 as X
  </code></pre>
  
  <p>I  probably have to attach/modify the "asType" on String and Integer somehow, but I feel any changes like this should be confined to the "X" class...  Can the X class either implement a method like:</p>
  
  <pre><code>X fromObject(object)
  </code></pre>
  
  <p>or somehow modify the String/Integer class from within X.  This seems tough since it won't execute any code in X until X is actually used... what if my first usage of X is "3 as X", will X get a chance to override Integer's asType before Groovy tries to call is?</p>