Define type inside expression level macro, then use it in declarations

3 views
Skip to first unread message

Andrew Davey

unread,
Oct 22, 2009, 12:53:17 PM10/22/09
to Nemerle Forum
If I define a new type from an expression level macro, can I then use
that in another class declaration?
For example:

public class HomeController : Controller
{
public Index() : ActionResult {
View(viewmodel { x = 1 });
}
}

The "viewmodel" macro defines a class called "HomeIndexViewModel" and
creates a new instance.
Then in the same project I want to do this:

public class HomeIndexPage : MasterPage[HomeIndexViewModel] {
...
}

I can't get this to compile yet. It complains that HomeIndexViewModel
is not found. Am I exceeding the capability of the compiler? Is there
some way to re-run a step so it finds the newly defined type?

Thanks :)

hardcase

unread,
Oct 22, 2009, 3:17:15 PM10/22/09
to Nemerle Forum
I think, you should create types, which are used within the same
assembly, only by meta-attributes.
So, your code turns to:

public class HomeController : Controller
{
[ViewModel(x)]
public Index() : ActionResult {
View(viewmodel { x = 1 });
}
}

or with special syntax form:

public class HomeController : Controller
{
public Index() : ActionResult
view model ( x )

emp...@gmail.com

unread,
Oct 22, 2009, 5:46:21 PM10/22/09
to Nemerle Forum
Basically what you want is to access to the "TypeBuilder" of parent
class. The below link shows you how to do it with another namespace
So you should be able to do it something similar.


http://groups.google.com/group/nemerle-en/browse_thread/thread/b012c94b67952521/c099b54bc0782053?lnk=gst&q=namespace#c099b54bc0782053

hardcase

unread,
Nov 4, 2009, 11:16:57 AM11/4/09
to Nemerle Forum
Andrew, are you using ASP.NET MVC framework?

About week ago I started to study this excellent web framework, and
I've got same ideas.
So, I've implemented such 'view model' in previously described way.
Usage example (this is not true controller, it just shows availability
of auto class in source code):

using ViewModel;

public class MyClassController {

public FooAction() : MyClassFooActionViewModel
viewmodel(A : int, B : int, C : IEnumerable[string])
{
viewmodel ( A = 10 );
}

}


Complete macro source is available here: http://paste.org.ru/?wd8xl8

Andrew Davey

unread,
Nov 5, 2009, 3:14:25 AM11/5/09
to Nemerle Forum
I am indeed :)

Thanks for sharing the code.
> > Thanks :)- Hide quoted text -
>
> - Show quoted text -
Reply all
Reply to author
Forward
0 new messages