Click here to Skip to main content
15,920,438 members
Home / Discussions / C#
   

C#

 
AnswerRe: Generic method: Cast an object to a return type T Pin
Rob Philpott8-Apr-09 23:42
Rob Philpott8-Apr-09 23:42 
GeneralRe: Generic method: Cast an object to a return type T Pin
Spunky Coder9-Apr-09 0:01
Spunky Coder9-Apr-09 0:01 
GeneralRe: Generic method: Cast an object to a return type T Pin
Rob Philpott9-Apr-09 0:12
Rob Philpott9-Apr-09 0:12 
GeneralRe: Generic method: Cast an object to a return type T Pin
Spunky Coder9-Apr-09 0:43
Spunky Coder9-Apr-09 0:43 
GeneralRe: Generic method: Cast an object to a return type T Pin
Rob Philpott9-Apr-09 0:46
Rob Philpott9-Apr-09 0:46 
GeneralRe: Generic method: Cast an object to a return type T Pin
Spunky Coder9-Apr-09 1:08
Spunky Coder9-Apr-09 1:08 
GeneralRe: Generic method: Cast an object to a return type T Pin
_groo_9-Apr-09 1:47
_groo_9-Apr-09 1:47 
QuestionAssignability of output parameters in C# Pin
dojohansen8-Apr-09 22:55
dojohansen8-Apr-09 22:55 
Hi all,

I'm wondering why C# imposes restrictions on assignments using output parameters that are not otherwise present. Presumably there is some logical reason for it, but I can't think of it.

Assume this code:

public interface SomeInterface { }

class A : SomeInterface
{
    public SomeInterface Field;

    void foo() 
    { 
        Parse(out this.Field); 
    }

    static public void Parse(out A something)
    {
        something = new A();
    }
}


This code will not build - instead, the compiler will inform you that The best overloaded method match for A.Parse(out A)' has some invalid arguments. Since A implements SomeInterface, Field is of course assignable to A, but nevertheless it does not build. If I instead write the code so that Parse returns A and the assignment is done in foo(), it builds without as much as a warning, as it should.

void foo()
{
    Field = Parse();
}

static public A Parse()
{
    return new A();
}


This causes some minor annoyances for me as I'm using the compiler-compiler Coco/R to generate a parser for a language of my creation. This tool lets me specify the parser by means of what's called an attributed grammar, and the generated code uses the attributes in the grammar and maps them to parameters. What I wanted to do was to use an output parameter for most productions in the grammar, like this:

Term<out term="" t=""> = (. t = new Term(); Factor f; .)
Factor<out f=""> (. t.Mul(f); .)
{
'*' Factor<out f=""> (. t.Mul(f); .)
| '/' Factor<out f=""> (. t.Div(f); .)
}
.

This straightforwardly leads to a generated method for parsing a Term that looks like this:

void Term(out Term t) 
{
  t = new Term(); Factor f; 
  Factor(out f);
  t.Mul(f); 
  while (la.kind == 15 || la.kind == 16) 
  {
    if (la.kind == 15) 
    {
      Get();
      Factor(out f);
      t.Mul(f);
    } 
    else 
    {
      Get();
      Factor(out f);
      t.Div(f);
    }
  }
}


My little problem arises because I've modeled Factor as an interface type. I want to be able to later on write a new class that performs some calculation or other, be free to derive this class from any class I wish, and so I've defined a simple interface IScalar { decimal GetValue(); } so that any class implementing it can be used as a factor.

I've modeled arithmetic expressions as a list of terms with + or - between them, a Term as shown above as a list of factors separated by * or /, and a Factor as a number, built-in function of the language, variable, or '(' Expr ')' - this model is sufficient to get correct operator precedence and handle nested expressions correctly.

It's not much of an issue as I can declare the output parameter to be of the interface type (these methods are private and only used internally in the parser after all), but I do wonder why C# imposes this restriction on output parameters. Or even if it really does - I haven't checked the language specification and it could be that it's the compiler rather than the spec that restricts me, though it seems a stretch.
AnswerRe: Assignability of output parameters in C# Pin
Rob Philpott8-Apr-09 23:36
Rob Philpott8-Apr-09 23:36 
GeneralRe: Assignability of output parameters in C# Pin
dojohansen9-Apr-09 0:21
dojohansen9-Apr-09 0:21 
AnswerRe: Assignability of output parameters in C# Pin
S. Senthil Kumar9-Apr-09 4:13
S. Senthil Kumar9-Apr-09 4:13 
GeneralRe: Assignability of output parameters in C# Pin
dojohansen1-Jul-09 0:52
dojohansen1-Jul-09 0:52 
GeneralRe: Assignability of output parameters in C# Pin
dojohansen1-Jul-09 3:52
dojohansen1-Jul-09 3:52 
GeneralRe: Assignability of output parameters in C# Pin
S. Senthil Kumar1-Jul-09 5:55
S. Senthil Kumar1-Jul-09 5:55 
GeneralRe: Assignability of output parameters in C# Pin
dojohansen1-Jul-09 6:05
dojohansen1-Jul-09 6:05 
GeneralRe: Assignability of output parameters in C# Pin
S. Senthil Kumar1-Jul-09 18:04
S. Senthil Kumar1-Jul-09 18:04 
GeneralRe: Assignability of output parameters in C# Pin
dojohansen1-Jul-09 22:25
dojohansen1-Jul-09 22:25 
GeneralRe: Assignability of output parameters in C# Pin
S. Senthil Kumar1-Jul-09 23:02
S. Senthil Kumar1-Jul-09 23:02 
GeneralDiscussion : Can we say Overloading as polymorphism Pin
Cracked-Down8-Apr-09 22:19
Cracked-Down8-Apr-09 22:19 
GeneralRe: Discussion : Can we say Overloading as polymorphism Pin
Nagy Vilmos8-Apr-09 22:49
professionalNagy Vilmos8-Apr-09 22:49 
GeneralRe: Discussion : Can we say Overloading as polymorphism Pin
dojohansen8-Apr-09 23:31
dojohansen8-Apr-09 23:31 
GeneralRe: Discussion : Can we say Overloading as polymorphism Pin
Nagy Vilmos9-Apr-09 1:46
professionalNagy Vilmos9-Apr-09 1:46 
GeneralRe: Discussion : Can we say Overloading as polymorphism Pin
dojohansen9-Apr-09 2:46
dojohansen9-Apr-09 2:46 
GeneralRe: Discussion : Can we say Overloading as polymorphism Pin
Rob Philpott8-Apr-09 22:55
Rob Philpott8-Apr-09 22:55 
GeneralRe: Discussion : Can we say Overloading as polymorphism Pin
Cracked-Down8-Apr-09 23:20
Cracked-Down8-Apr-09 23:20 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.