C# Language Specification - Extension Methods

G

Guest

I think one of the major advancements on C# will be the growing support for
Extension Methods and related concepts...

The current syntax involves a static class with static members using a
warping of the "this" concept applied to function parameters. Surely we'll
see more twisting and warping of language semantics as this technology
evolves...

Rather than warp language symantics... It would be more apparently proper to
allow something like:

(** Proposed syntax:
public class global::System.String {
public static string Encrypt(object key) { ... }
}
**)

Something generally synonymous with partial types... saying, hey look, you
can even partialize an existing type. People will ooh and ahh over it... I
can hear you doing it now as you prepare your rebuttle. Initially only static
members would be allowed, but you could then provide support for more things
without ever warping language concepts.

Currently, we have to teach... "this" means "this", unless it's on a
parameter in an extension class, in which case it means "that".
 
N

Nicholas Paldino [.NET/C# MVP]

Marshal,

Unfortunately, the argument doesn't hold. "global" is a language
concept in C#, and you are proposing changing that. Granted, it's not as
ubiquitious as "this", but it is still the same thing.

The same thing applies to other keywords as well. Return doesn't always
mean return. What happens when it is used in an attribute declaration, or
with yield?

It's like anything else, what it means depends on the context it is used
in.

The reason they chose "this" (at least as I see it) was twofold. The
first was to give some sort of indication that the extension method would be
applied to instances. "this" helps convey the idea of an instance pretty
well. The second was to help not introduce new keywords into the language
so as to reduce conflicts with pre-existing code. Your example in this case
helps prevent that as well, so it's applicable here. However, I don't see
"global" as a good way of saying "this should be applied to an instance".
 
G

Guest

Marshal said:
I think one of the major advancements on C# will be the growing support for
Extension Methods and related concepts...

The current syntax involves a static class with static members using a
warping of the "this" concept applied to function parameters. Surely we'll
see more twisting and warping of language semantics as this technology
evolves...

Rather than warp language symantics... It would be more apparently proper to
allow something like:

(** Proposed syntax:
public class global::System.String {
public static string Encrypt(object key) { ... }
}
**)

Something generally synonymous with partial types... saying, hey look, you
can even partialize an existing type. People will ooh and ahh over it... I
can hear you doing it now as you prepare your rebuttle. Initially only static
members would be allowed, but you could then provide support for more things
without ever warping language concepts.

Currently, we have to teach... "this" means "this", unless it's on a
parameter in an extension class, in which case it means "that".

And how would this be any less "warping" than the already proposed
syntax? Adding a new "global" keyword and a "::" operator?

Wasn't it you that earlier was against introducing "where" as a new
keyword? How's this different ?

If we're going to talk about new syntax for this, as well as introducing
new keywords, then I like the following better (which I saw somewhere
recently):

public class extend System.String { ....
 
G

Guest

public class extend System.String { ....

I like it... anything that reuses the concept of "partial types", or reuses
any existing concepts. As far as I know, "global", and "::" are official
elements already. The importance of them are to facilitate something like an
absolute scope versus a relative scope based on existing using statements.
 
G

Guest

I have always thought that static members should be accessible from instances
as well. I can keep them straight via strong naming and would have no
problems.

I recognize the validity of your comments. I do think my warp of "global" is
less severe than their warp of "this" however.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top