Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dart: what is the meaning of class constructor being marked as const

Tags:

dart

So I have seen code as such:

class Whatever {
  final String name;
  const Whatever(this.name);
}

What does change by the fact that the constructor is marked with const? Does it have any effect at all?

I have read this:

Use const for variables that you want to be compile-time constants. If the const variable is at the class level, mark it static const. (Instance variables can’t be const.)

but it does not seem to make sense for the class constructor.

like image 682
Peter StJ Avatar asked Mar 11 '15 09:03

Peter StJ


People also ask

Why is a Dart const a constructor?

Dart supports the keyword const for object instantiation. If an object is declared as being instantiated via the const keyword, it's assigned a value at compile time. A constant is an instance that is initialized with one of the following: A value of a primitive type.

What is const constructor in Flutter?

A const constructor is an optimization! The compiler makes the object immutable, allocating the same portion of memory for all Text('Hi!') objects. But not Text(Math. random()) though, as its value can't be determined at compile time!

What does const do in Dart?

Const Keyword in Dart Using const on an object, makes the object's entire deep state strictly fixed at compile-time and that the object with this state will be considered frozen and completely immutable.

Can a constructor be declared const?

Constructors may be declared as inline , explicit , friend , or constexpr . A constructor can initialize an object that has been declared as const , volatile or const volatile . The object becomes const after the constructor completes.


2 Answers

  • The constructor can't have a constructor body.
  • All members have to be final and must be initialized at declaration or by the constructor arguments or initializer list.
  • You can use an instance of this class where only constants are allowed (annotation, default values for optional arguments, ...)
  • You can create constant fields like static const someName = const Whatever();

If the class doesn't have a const constructor it can't be used to initialize constant fields. I think it makes sense to specify this at the constructor. You can still create instances at runtime with new Whatever() or add a factory constructor.

See also

  • Dartlang const constructor - how is it different to "regular" constructor
  • Dart factory constructor - how is it different to “const” constructor
  • How to make a factory constructor that returns as const value
  • Why does Dart have compile time constants?
  • How to write abstract class constructors so that it will be flexible for extending in sub classes

The "old style" (still valid) enum is a good example how to use const https://stackoverflow.com/a/15854550/217408

like image 62
Günter Zöchbauer Avatar answered Oct 12 '22 09:10

Günter Zöchbauer


If your class produces objects that never change, you can make these objects compile-time constants. To do this, define a const constructor and make sure that all instance variables are final.

class ImmutablePoint {
  const ImmutablePoint(this.x, this.y);

  final int x;
  final int y;

  static const ImmutablePoint origin = ImmutablePoint(0, 0);
}

Code example

Modify the Recipe class so its instances can be constants, and create a constant constructor that does the following:

  • Has three parameters: ingredients, calories, and milligramsOfSodium (in that order).

  • Uses this. syntax to automatically assign the parameter values to the object properties of the same name.

  • Is constant, with the const keyword just before Recipe in the constructor declaration.

    class Recipe {
      final List<String> ingredients;
      final int calories;
      final double milligramsOfSodium;
    
      const Recipe(this.ingredients, this.calories, this.milligramsOfSodium);
    }
    
like image 37
Paresh Mangukiya Avatar answered Oct 12 '22 09:10

Paresh Mangukiya