Colon After Method Declaration

Colon after method declaration?

Yes it's new syntax introduced in PHP 7 to declare the method returns an array.

http://php.net/manual/en/functions.returning-values.php#functions.returning-values.type-declaration

C++, What does the colon after a constructor mean?

As others have said, it's an initialisation list. You can use it for two things:

  1. Calling base class constructors
  2. Initialising member variables before the body of the constructor executes.

For case #1, I assume you understand inheritance (if that's not the case, let me know in the comments). So you are simply calling the constructor of your base class.

For case #2, the question may be asked: "Why not just initialise it in the body of the constructor?" The importance of the initialisation lists is particularly evident for const members. For instance, take a look at this situation, where I want to initialise m_val based on the constructor parameter:

class Demo
{
Demo(int& val)
{
m_val = val;
}
private:
const int& m_val;
};

By the C++ specification, this is illegal. We cannot change the value of a const variable in the constructor, because it is marked as const. So you can use the initialisation list:

class Demo
{
Demo(int& val) : m_val(val)
{
}
private:
const int& m_val;
};

That is the only time that you can change a const member variable. And as Michael noted in the comments section, it is also the only way to initialise a reference that is a class member.

Outside of using it to initialise const member variables, it seems to have been generally accepted as "the way" of initialising variables, so it's clear to other programmers reading your code.

What does colon after PHP function do?

That is the return type declaration, a feature introduced with PHP 7.

The type that you see after the colon, is the type that will be returned with the function.

There are 2 options:

By default the returned value will be converted to the type that needs to be returned.

In your case it will always be converted to a string.

If strict typing is enabled (by declare(strict_types=1);), the output needs to be a string, or it will throw a TypeError.

Either way: as a developer you are always sure that the return type is a string in this case.

It is not obligatory to define the return type, so you can leave it if you don't want it. The return type in that case can be anything.

What is the meaning of colon (:) operator after member function name in php

public function getTitle():Data {
return $this->title;
}

"Return type declaration" added since PHP 7.0 (This method should return an object having type "Data").

Like "Argument type declaration", "Return type declaration" is optional.

to check the new features introduced in PHP 7.0

check this link
http://php.net/manual/en/migration70.new-features.php

Colon after Constructor in dart

The part after : is called "initializer list". It is a ,-separated list of expressions that can access constructor parameters and can assign to instance fields, even final instance fields. This is handy to initialize final fields with calculated values.

The initializer list is also used to call other constructors like : ..., super('foo').

Since Dart version 2.0 the initializer list also supports assert(...) which is handy to check parameter values.

The initializer list can't read from this because the super constructors need to be completed before access to this is valid, but it can assign to this.xxx.

Pointing out as mentioned in the comments by user693336:

This also means the initializer list is executed before the constructor body. Also the initializer lists of all superclasses are executed before any of the constructor bodies are executed.

Example (copied from https://github.com/dart-lang/language/issues/1394):

class C {
final int x;
final int y;
C(this.x) : y = x + 1;
}

Colon after colon syntax while calling a function in Javascript

These are type annotations. They are not standard javascript. They are added when using tools that layer static typing onto javascript. The two most popular flavors are Typescript and Flow.

When you write code that uses this syntax you will transpile your source code into code that is syntactically valid for execution by running one of the above mentioned tools on your code. When you do, it will tell you if your usage of the types is correct, raise warnings that are helpful in development, and then strip all this out so it can actually be run.

Colon in the method

For constructors (function names with the same name as the class name), the : indicates that a constructor of the base class will be called and will execute first, with any passed parameters, before the code of the child constructor.

So for the function public Cylinder(double radius, double height) : base(radius) the constructor for Circle is executed before the code in the Cylinder constructor, which in turn calls the constructor for Shape setting this.x and this.y, and then executes its own code, which it has none, and then finally the code in Cylinder constructor is executed, setting y.



Related Topics



Leave a reply



Submit