Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Re: meta-classes why/when?

19 views
Skip to first unread message

Joanna Carter

unread,
Jul 31, 2008, 5:07:36 PM7/31/08
to
José Eduardo a écrit :

> type Foo = class
> public:
> constructor Create();
> end;
> implemenation
> constructor Foo.Create()
> begin
> end;
>
> type FooMeta = class of Foo;
> function CreateFooObject(fooMeta : FooMeta) : Foo
> begin
> Return := fooMeta.Create();
> end;
>
> This was an article talking about meta-classes in c# like delphi7. Please,
> sorry if this is a dumb question, but someone can explain what´s the catch
> here?

There's no catch here, all that is being done is using the standard
Delphi metaclass type in a peculiar and unnecessary factory method.

You can declare a metaclass for any Delphi class and then call any
static methods or constructors.

However, in C#, such metaclasses are not so simple, they have to be
written explicitly for each class hierarchy using reflection.

However, you can use generics to instantiate any class that support an
unparameterised constructor.

e.g.

public static class Factory<T> where T : class, new()
{
public static T Create()
{
return new T();
}
}

...used like this :

public class MyClass
{
public MyClass() : base() { }
}

{
object newObject = Factory<MyClass>.Create();
}

What are you trying to learn here ?

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer

José Eduardo

unread,
Jul 31, 2008, 4:51:19 PM7/31/08
to
type Foo = class
public:
constructor Create();
end;
implemenation
constructor Foo.Create()
begin
end;

type FooMeta = class of Foo;
function CreateFooObject(fooMeta : FooMeta) : Foo
begin
Return := fooMeta.Create();
end;

This was an article talking about meta-classes in c# like delphi7. Please,
sorry if this is a dumb question, but someone can explain what´s the catch
here?

Thanks in advance
José Eduardo


Graeme Geldenhuys

unread,
Aug 1, 2008, 6:26:58 AM8/1/08
to
Joanna Carter wrote:
>>
>> type FooMeta = class of Foo;
>> function CreateFooObject(fooMeta : FooMeta) : Foo
>> begin
>> Return := fooMeta.Create();
>> end;
>>
>> This was an article talking about meta-classes in c# like delphi7.
>> Please, sorry if this is a dumb question, but someone can explain
>> what´s the catch here?
>
> There's no catch here, all that is being done is using the standard
> Delphi metaclass type in a peculiar and unnecessary factory method.

Maybe I'm not up-to-date with the latest Delphi lingo (and I don't use
.NET), but what is a metaclass? Isn't the above type a class-reference
type? Class-Reference types are very handy in factory methods where the
specific type is only known at runtime.


Regards,
- Graeme -


_______________________________________________________
fpGUI - a cross-platform GUI toolkit using Free Pascal
http://opensoft.homeip.net/fpgui/

José Eduardo

unread,
Aug 1, 2008, 8:53:52 AM8/1/08
to
> There's no catch here, all that is being done is using the standard Delphi
> metaclass type in a peculiar and unnecessary factory method.


wow, thanks for the imediate answer...

> What are you trying to learn here ?

Just studing patterns (factorys) and the term popup in a web site. the
'meta-class' is/was new for me.

One more question, may be completly outdated but i´ve read that you are
doing c# now. Mainly i work with pascal / win32 database app´s. Should i
really consider a migration to .net / c#?

Thanks Joana.

José Eduardo


Rob Kennedy

unread,
Aug 1, 2008, 9:50:55 AM8/1/08
to
Graeme Geldenhuys wrote:
> Joanna Carter wrote:
>>>
>>> type FooMeta = class of Foo;
>>> function CreateFooObject(fooMeta : FooMeta) : Foo
>>> begin
>>> Return := fooMeta.Create();
>>> end;
>>>
>>> This was an article talking about meta-classes in c# like delphi7.
>>> Please, sorry if this is a dumb question, but someone can explain
>>> what´s the catch here?
>>
>> There's no catch here, all that is being done is using the standard
>> Delphi metaclass type in a peculiar and unnecessary factory method.
>
> Maybe I'm not up-to-date with the latest Delphi lingo (and I don't use
> .NET), but what is a metaclass? Isn't the above type a class-reference
> type?

Yes. Two names for the same thing.

> Class-Reference types are very handy in factory methods where the
> specific type is only known at runtime.

Right. The example that everyone has called at some point in his or her
Delphi career is Application.CreateForm.

--
Rob

Joanna Carter

unread,
Aug 1, 2008, 10:19:11 AM8/1/08
to
Graeme Geldenhuys a écrit :

> Maybe I'm not up-to-date with the latest Delphi lingo (and I don't use
> .NET), but what is a metaclass?

Delphi class references are, essentially, metaclasses. IOW, they are
classes that describe classes instead of instances.

> Isn't the above type a class-reference
> type? Class-Reference types are very handy in factory methods where the
> specific type is only known at runtime.

Agreed, but that is something that is not as easy to accomplish in .NET
as you are not allowed to have either virtual constructors or static
(class) methods.

Joanna Carter

unread,
Aug 1, 2008, 10:22:05 AM8/1/08
to
José Eduardo a écrit :

> One more question, may be completly outdated but i´ve read that you are
> doing c# now. Mainly i work with pascal / win32 database app´s. Should i
> really consider a migration to .net / c#?

The only reason to move to .NET is if your clients/employers need such
functionality or you want to make the move.

The call has to be yours :-)

Graeme Geldenhuys

unread,
Aug 1, 2008, 10:59:08 AM8/1/08
to
"Jos�����������������������������������" wrote:
> doing c# now. Mainly i work with pascal / win32 database app�s. Should i
> really consider a migration to .net / c#?

Don't believe all the hype you read about .NET and C#. .NET is not
taking off quite as good as Microsoft would make you believe (their
marketing department is great). Native executables are here to stay (for
a very long time still). Plus the Delphi language is great, native apps
are fast, native application deployment is much easier that .NET
deployment etc...

I would only move if my boss demands it - and then only if he can
convince me first. :-)

Joanna Carter

unread,
Aug 1, 2008, 12:28:12 PM8/1/08
to
Graeme Geldenhuys a écrit :

> Don't believe all the hype you read about .NET and C#. .NET is not
> taking off quite as good as Microsoft would make you believe (their
> marketing department is great). Native executables are here to stay (for
> a very long time still). Plus the Delphi language is great, native apps
> are fast, native application deployment is much easier that .NET
> deployment etc...

Graeme, I moved to .NET, not because it was a different platform - after
all, .NET still runs on Win32/64 - but because :

1. I was elected to TeamB to support C#Builder
2. I have always preferred the C syntax
3. the framework libraries are just awesome
4. interfaces are not reference-counted (phew!)
5. at the time, Delphi didn't support language features like generics
6. the anti-.NET hype is overstated

... but not necessarily in that order :-)

As soon as either a client wants Delphi work or Delphi.NET reaches .NET
3.5, "I'll be back" (well maybe) (unless I start developing Objective C
apps for OS X) (or whatever pays the bills)

<vbg>

0 new messages