Local function overloading don't work: "local variable already defined in this scope"
Asked Answered
T

1

5

There are 2 classes - a base class, and inherited class. I want the function print to be able print objects of both of these classes. Though the name of the function is similar in both cases, the signature of the function is different. Why then the function does not work here?

this is the code:

using System;

namespace ConsoleApp8
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Asset asset = new Asset();     // פולימורפיזם
            asset.name = "A";
            asset.price = 1;

            Stock stock = new Stock();
            stock.name = "B";
            stock.price = 2;
            stock.shared = 5;

            print(stock);
            print(asset);

            static void print(Asset asset)
            {
                Console.WriteLine(asset.price + " " + asset.name);

            }
            static void print(Stock stock)
            {
                Console.WriteLine(stock.price + " " + stock.name + " " + stock.shared);
            }
        }

        class Asset
        {
            public string name;
            public int price;
        }
        class Stock : Asset
        {
            public int shared;
        }
    }
}


Thank you!

Taam answered 17/6, 2022 at 16:45 Comment(6)
Local functions don't support overloading. There's probably a duplicate question somewhere...Caines
In your own words: does the definition of static void print(Asset asset) appear inside static void Main(string[] args), or outside? Why? Should it be inside, or outside? Why?Dympha
Is there any reason you chose not to override ToString() on Asset and Stock?Alsatia
Not an exact duplicate, but mentions the the fact that local functions don't support overloading: #71279742Caines
Feel free to ask any question. If you feel that my reply is helpful, then you can upvote or mark my reply as an answer to simplify the future search of other users. How does accepting an answer work?Harwill
The compiler team is willing to add this. Upvote the feature request here.Skyway
H
6

Local functions cannot be overloaded:

A method cannot contain two local functions with the same name but different parameters. That is, overloaded local functions are not supported.

So what you can do is to create two local methods with different names:

void PrintAsset(Asset asset) {}
void PrintStock(Stock stock) {}

And some more restrictions for local functions:

  • local functions can't be virtual or abstract
  • (related) local functions can't be marked override or sealed
  • local functions can't have access modifiers
Harwill answered 21/6, 2022 at 11:57 Comment(1)
Local functions being virtual or abstract does not make sense because this concept requires an object to resolve actual method to call. Override does not make sense for local methods because you can only override virtual methods. Sealed does not make any sense for local methods because you can only seal virtual methods. Access modifers do not make sense for local methods for obvious reasons. HOWEVER name overloading makes 100% sense for local methods so why introduce this artificial limitation?Terpsichorean

© 2022 - 2024 — McMap. All rights reserved.