Tensor Programming avatar

A Beginners Guide to Dart - Generics, Exceptions, Factory Constructors, and Enumerated Types - Part Seven

tensor

Published: 15 Mar 2019 › Updated: 15 Mar 2019A Beginners Guide to Dart - Generics, Exceptions, Factory Constructors, and Enumerated Types  - Part Seven

A Beginners Guide to Dart - Generics, Exceptions, Factory Constructors, and Enumerated Types - Part Seven

dart-bird.jpg

Repository

https://github.com/dart-lang/sdk

What Will I Learn?

  • You will learn to use Generic Types
  • You will learn about Exceptions and how to make custom Exceptions
  • You will learn about factory constructors
  • You will learn how to use Factory constructors to build singleton objects
  • You will learn about Enumerated Types

Requirements

System Requirements:
OS Support for Dart:
  • Windows
  • macOS
  • Linux

Required Knowledge

  • The Dart SDK
  • A Dart supported text editor (Dart Pad can be used)
  • A little time to sit and watch a video and some patience to learn the language

Resources for Dart:

Sources:

Dart Logo (Google): https://www.dartlang.org/

Difficulty

  • Beginner

Description

In this Dart Tutorial Video, we look at Generic Types, Exceptions, Factory Constructors, Enumerated Types and Singleton Objects. While many of these ideas are more complex than any of the concepts that we've looked at before, they can be very powerful. Generic Types and Enumerated Types help build out the type system and Exceptions help us deal with different types of errors. Factory constructors are important because they allow us to build Single instance classes or Singleton Objects.

Generic and Enumerated Types

The Idea of Generic Types is an idea that has been around for awhile and it is usually found it functional statically typed languages. Generic Types allow the developer to build out Polymorphic objects, functions and variables which are targeted to specific patterns. This enables us to create functions and classes that can be reused with various different types and it cuts down on the boilerplate that needs to be built for an application. Enumerated Types on the other hand give us the ability to deal with binary types. We can again created targeted situations with Enumerated types that narrow down how our application state can be derived.

enums-generics.png

In the above image, we have a Status enum type and a function that uses two generic types T and V. The Status enum type can be only one of three different states, Status.running, Status.stopped or Status.paused. Each of these type has its own index representation as well with the first type being index 0 by default. The generic function add takes in two types T and V with both types extending the num type. This means that these types can both be independent types that must also extend the num type. This narrows down how the function must be used in our code and will also decrease how the virtual machine unpacks this code.

Factory Constructors and Singleton Objects

Factory Constructors are an idea that came from the Factory pattern. The main idea behind this pattern was to create a method that doesn't have to specify the exact object that will be created by the constructor. Factories can be used to create sub classes and singleton objects as a result and you can use them to defer the instantiation of a class. They were added to the Dart specification because the pattern was very common in other Object Oriented Languages prior to the creation of the language.

singleton.png

The class above features and example of a simple singleton object class. This class Item contains a static field item which holds a reference to the singleton object after it is instantiated. The Factory constructor for this class takes in the initial state and determines whether or not that item field contains a object or not inside of it. If it does contain an object, then the factory constructor just returns that object with the new added state appended to it. If it doesn't contain an object then it creates a new Item object using the Item._internal() named constructor. In this way, we can call to this Item constructor as many times as we want, but only have one single Item object in the entire program.

The Source Code for this video may be found here: https://github.com/tensor-programming/dart_for_beginners/tree/tensor-programming-patch-6

Video Tutorial

Curriculum

Projects

Leave A Beginners Guide to Dart - Generics, Exceptions, Factory Constructors, and Enumerated Types - Part Seven to:

Written by

Tensor programming is aimed at providing programming education through both youtube and blogs.

Read more #utopian-io posts


Best Posts From Tensor Programming

We have not curated any of tensor's posts yet. But you can encourage our curation team to review posts by visiting them regularly and by referring other readers. Because we give priority to frequently read content.

More Posts From Tensor Programming