Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is @ inside razor syntax called operator?

I just bought a book on ASP.NET MVC With Razor View Engine. There is a subsection called Usage of @ Operator and this subsection title makes me ... well, uncomfortable.

Is @ inside the razor view engine called operator?

UPDATE

I guess my question is not so clear. I want to know if @ is an operator inside the razor view engine. For example, < > = != >= => these are called as operator inside C# language. Is it same for @ inside Razor view engine?

like image 494
tugberk Avatar asked Sep 12 '26 08:09

tugberk


2 Answers

I think the reason for you discomfort is that the @ token (when talking about it from a parsing perspective, though the word character should also do) is overloaded to indicate a number of different situations. Let's examine what those are:

  • Write a value to the output:

    @this.Value
    
  • Indicate a code block transition:

    @{
        var foo = 1;
        foo += 1;
    }
    
  • Indicate a code statement transition:

    <div>
    @if(foo) {
         // code
    }
    </div>
    
  • Indicate an escape to markup till the end of the line

    @if(foo) {
        foo = false;
        @: value printed to output
    }
    
  • Indicate directive statements:

    @inherits MyCustomBaseType
    
  • Indicate special code blocks:

    @section Foo {
        <div />
    }
    
    @helper Bar(int param) {
        param += 1;
    }
    
  • Delimit comment blocks:

    @*
       This is a comment
    *@
    
  • Escape the @ character:

    Email me at myemail@@example.com
    

In my opinion only the first usage can be considered as an operator. The operand (i.e. everything else that follows the @ up to but excluding the first markup-significant whitespace character) is passed as the parameter to the Write() method. All other usages don't have any clearly identifiable operands or require additional tokens (the * in the comment block, etc) to be indentified.

like image 137
marcind Avatar answered Sep 14 '26 10:09

marcind


According to the official pages on Razor, you can find one example here, it does not seem that this is called an operator.

From the linked page:

You add code to a page using the @ character

(my emphasis)

I also found numerous other pages on that same site, all referring it to just the "@ character", so in that sense it isn't considered an operator.

<opinion>

However, if you read on Wikipedia on the topic of operators, then:

Syntactically operators usually contrast to functions. In most languages, functions may be seen as a special form of prefix operator with fixed precedence level and associativity, often with compulsory parentheses e.g. Func(a) (or (Func a) in LISP). Most languages support programmer-defined functions, but cannot really claim to support programmer-defined operators, unless they have more than prefix notation and more than a single precedence level. Semantically operators can be seen as special form of function with different calling notation and a limited number of parameters (usually 1 or 2).

(again, my emphasis)

Then I would argue that @ is in fact an operator. It is a symbol with a specific meaning, and you could argue that you're "escaping out of the surrounding context to do something else", sort of like a function call.

In other words, while the word operator does not appear in the website articles I've seen so far, I would consider it to be an operator nonetheless.

</opinion>

like image 20
Lasse V. Karlsen Avatar answered Sep 14 '26 08:09

Lasse V. Karlsen