Autocasting

Autocasting is a convenient way to set typed properties without bringing in extra API modules via the require() function. Developers can pass constructor parameters directly to the desired property and the API will instantiate the object with the provided constructor parameters internally, making the process simpler for the developer.

Let's take a look at some typical code to create a symbol. Notice that three modules are needed to create a SimpleMarkerSymbol:

require([
  "esri/Color",
  "esri/symbols/SimpleLineSymbol",
  "esri/symbols/SimpleMarkerSymbol"
], function (
  Color, SimpleLineSymbol, SimpleMarkerSymbol
) {

  var symbol = new SimpleMarkerSymbol({
    style: "diamond",
    color: new Color([255, 128, 45]),  // You typically create the instance with the "new" keyword
    outline: new SimpleLineSymbol({    // also here
      style: "dash-dot",
      color: new Color([255, 128, 45]) // and here
    })
  });

});

From the documentation, we see that the color and outline properties of SimpleMarkerSymbol can be autocast.

Now let's bring autocasting into the picture. Remember, there's no need to require all the modules. We only need to pass constructor parameters directly to the autocast properties:

require([
  "esri/symbols/SimpleMarkerSymbol"
], function (
  SimpleMarkerSymbol
) {

  var symbol = new SimpleMarkerSymbol({
    style: "diamond",
    color: [255, 128, 45],  // No need to write new Color()
    outline: {              // No need for new SimpleLineSymbol()
      style: "dash-dot",
      color: [255, 128, 45] // Again, no need for new Color()
    }
  });

});

Notice the code is much more simple and functionally does the same thing. The API will take the values you pass in these properties and instantiate the typed objects internally.

Supported properties

The value of any property with the following label in the API documentation may be set via autocasting.

autocast label

Another example

Let's create a Viewpoint by autocasting its typed properties. The camera property of Viewpoint, position property of Camera, and spatialReference property of Point can all be set by autocasting.

require([
  "esri/Viewpoint"
], function(Viewpoint){

  // Viewpoint is the only module that needs to be required
  var viewpt = new Viewpoint({
    // esri/Camera can be autocast here
    camera: {
      heading: 90,
      tilt: 30,
      // esri/geometry/Point is autocast
      position: {
        x: -9177811,
        y: 4247784,
        z: 50000,
        // esri/geometry/SpatialReference is autocast here
        spatialReference: { wkid: 3857 }
      }
    }
  });

});

Autocasting is a convenience to you as a developer and is designed to help you simplify your experience with the API.