Is it possible to offer return options in a method?

Asked

Viewed 388 times

4

As far as I know Overload in the creation of methods, which are execution options, example(very simple), a method where you have the option to pass 2 or 3 parameters calling the same function:

//Soma 2 numeros
public int  Somar(int a, int b)
{
    return a + b;
}    

//Soma 3 numeros
public int  Somar(int a,int b, int c)
{
    return a + b + c;
}

The question is: Can I create a return Overload? Like for example:

public PessoaJuridica RetornarPessoa(string id)
{
    return new PessoaJuridica();
}

public PessoaFisica RetornarPessoa(string id)
{
    return new PessoaFisica();
}

The need arose because I have two method with different names and it would be easier for me to use the same name because they do "the same thing".

  • how you separate natural person from legal person, I mean, in the database, are in separate tables ?

  • Rovann, it was one example only, I am not using Personal and Personal, is that I have two types of user, the common User(who queries photos) and a user contributor(who sends), and I wanted to receive the profile data of both through a method name only and not two.

  • right, so, these two types of users, are on the same table ? there is a field that identifies which type it is ?

  • They are in different tables, because in this case the information of each is different BEEEM. Let’s say it’s another area of the site, they have no relationship to each other.

  • in this case, I would go by Generics even, if I had an inheritance relationship, I could even use polymorphism, but if it has no relation, I would use an interface and generic method

3 answers

4


Not possible. Method signature does not consider the return type. If possible, it would complicate the compiler’s work to know which method you want to use.

I don’t quite understand what the intention is of doing this and there aren’t enough details for me to understand, but I imagine you can solve this using a generic method.

public static T RetornarPessoa<T>(string id) where T: new()
{
    return new T();
}

I I suppose that you have some hierarchy in the classes. So I made this example of how my idea can be implemented:

See working on . NET Fiddle.

public class Program
{
    public static void Main()
    {
        var pessoa = RetornarPessoa<PessoaFisica>("a550");
        Console.WriteLine(pessoa.Id);
    }

    public static T RetornarPessoa<T>(string id) where T: Pessoa, new()
    {

        return new T { Id = id };
    }
}

public abstract class Pessoa
{       
    public string Id { get; set; }
}

public class PessoaJuridica : Pessoa
{
    public string RazaoSocial { get; set; }
}

public class PessoaFisica : Pessoa
{
    public string NomeCompleto { get; set; }
}

4

This is not possible, the signature of the method only consider your name and parameter types.

If I did with the return it would complicate the compiler, create ambiguity and possible bugs.

The most sensible solution in this case is to put a differential in the name of the method indicating what kind of return it will provide. It is even more readable. I have come to think of some others, but nothing that is recommended. I even started an answer with an alternative, but it’s something that complicates the code, creates confusion, opens the door to bugs, better go the easy, safe and obvious way.

public PessoaJuridica RetornarPessoaJuridica(string id) {
    return new PessoaJuridica();
}

public PessoaFisica RetornarPessoaFisica(string id) {
    return new PessoaFisica();
}

I put in the Github for future reference.

  • your post/reply on the link was fantastic !

2

In the post were asked two questions.

I can create a return Overload?

Not, this really cannot. The identity of a method is done by method name x parameters. Even if you duplicate the method by modifying only the return, the compiler will understand that it is the same method - not an overload - and will give error.

It is possible to offer return options in a method?

It is possible yes, just make this clear in signing the method.

A method may or may not return a value, but the cool thing is that this value can be anything.

Using Either

public interface IPessoaRepository
{
    Either<Option<PessoaFisica>, Option<PessoaJuridica>> RetornaPessoa(string id);
}

public class PessoaRepository : IPessoaRepository
{
    public Either<Option<PessoaFisica>, Option<PessoaJuridica>> RetornaPessoa(string id)
    {
        // { seu codigo aqui }
        return pessoa; // Tanto faz se é tipo PessoaFisica ou PessoaJuridica
    }
}

Source: http://www.elemarjr.com/pt/2017/04/exceptions-sao-muito-intrusivas/

Browser other questions tagged

You are not signed in. Login or sign up in order to post.