Posted by: leppie | August 16, 2009

New CLR shorthand syntax

I have added a new macro called ‘with-clr-type’, that tags an identifier with a CLR type, and this allows to write shorter syntax.

Given the following class:

  public class TestClass
  {
    public string Source;
    public string Message { get; set; }

    public TestClass()
    {

    }

    public TestClass(string msg)
    {
      Message = msg;
    }

    public void Print(string hello)
    {
      Console.WriteLine("{0} {1}", hello, Message);
    }

    public char this[int i]
    {
      get { return Message[i]; }
      set { Message = i.ToString() + value.ToString(); }
    }
  }

One can do the following:

> (import (ironscheme clr))
> (clr-using IronScheme)
> (let ((obj (clr-new TestClass "foo")))
.   (with-clr-type ((obj TestClass)) ; can tag multiple ids
.     (printf "~a\n" (eq? obj 50)) ; normal reference, do nothing
.     (printf "~a\n" (obj : Message)) ; property get
.     (obj : Message = "world") ; property set
.     (printf "~a\n" (obj -> Source)) ; field get
.     (obj -> Source = "bar") ; field set
.     (printf "~a\n" (obj -> Source)) ; field get
.     (obj Print "hello") ; method call
.     (printf "~a\n" (obj (1))) ; indexer get
.     (obj (99) = #\a) ; indexer set
.     (obj : Message) ; property get
.     ))
#f
foo
()
bar
hello world
o
"99a"
>

Which is a lot shorter than the normal way. :)

About these ads

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: