Your assumption about requiring some form of registration system for Python to 
implement extensions is correct as Roslyn (The C# compiler) resolves them at 
compile time (as long as the namespace is imported/the class is in the "global" 
namespace)

When I said static function, it's basically me saying static method, and 
they're the same as a method in python with "@staticmethod" applied to it.

here's a sample "prototype" of a System.Linq extension method (a very useful 
one imo):

"namespace System.Linq
{
    public static class Enumerable
    {
        public static IEnumerable<TResult> Select<TSource, TResult>(this 
IEnumerable<TSource> enumerable, Func<TSource, TResult> selector);
    }
}"

As you can see, the param "enumerable" is prefixed by the "this" keyword, this 
tells Roslyn to treat "enumerable" as if it was the special implicit "this" 
operator in instance methods:

"using System.Linq;

namespace MyNS
{
    public sealed class MyCLS
    {
        public static readonly List<int> MyInts = new List<int>() { 0, 5, 12, 
56, 9 };

        public int RNG = 42;

        public IEnumerable<int> ExpandInts()
        {
            return MyInts.Select(@int => @int * this.RNG);
        }
    }
}"

As you can see in the above to quoted blocks (SOMEONE TELL ME HOW TO DO CODE 
BLOCKS!), I'm using the extension method "Select" defined in "Enumerable" as if 
it was defined on the interface "IEnumerable<T>" when it's actually not. 
(List<T> implements IList<T> which inherits ICollection<T> which inherits 
IEnumerable<T>)

The only requirement is that both the static class AND the method is visible to 
your code (with public being visible to everyone)

Here's the official docs: 
https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/extension-methods
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/UMINJUP4PG5WXY3W2MUYJL2DQ4GTVOWH/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to