Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
Delphi Cookbook
Delphi Cookbook

Delphi Cookbook: 50 hands-on recipes to master the power of Delphi for cross-platform and mobile development on Windows, Mac OS X, Android, and iOS

Arrow left icon
Profile Icon Daniele Teti
Arrow right icon
₱579.99 ₱2000.99
Full star icon Full star icon Full star icon Full star icon Half star icon 4.5 (19 Ratings)
eBook Sep 2014 328 pages 1st Edition
eBook
₱579.99 ₱2000.99
Paperback
₱2500.99
Subscription
Free Trial
Arrow left icon
Profile Icon Daniele Teti
Arrow right icon
₱579.99 ₱2000.99
Full star icon Full star icon Full star icon Full star icon Half star icon 4.5 (19 Ratings)
eBook Sep 2014 328 pages 1st Edition
eBook
₱579.99 ₱2000.99
Paperback
₱2500.99
Subscription
Free Trial
eBook
₱579.99 ₱2000.99
Paperback
₱2500.99
Subscription
Free Trial

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Table of content icon View table of contents Preview book icon Preview Book

Delphi Cookbook

Chapter 2. Become a Delphi Language Ninja

In this chapter, we will cover the following recipes:

  • Fun with anonymous methods – using higher-order functions
  • Writing enumerable types
  • RTTI to the rescue – configuring your class at runtime
  • Duck typing using RTTI
  • Creating helpers for your classes
  • Checking strings with regular expressions

Introduction

This chapter explains some of the not-so-obvious features of the language and the RTL that every Delphi programmer should know. There are ready-to-use recipes that will be useful every day and have been selected over many others.

Fun with anonymous methods – using higher-order functions

Since Version 2009, the Delphi language (or better, its Object Pascal dialect) supports anonymous methods. What's an anonymous method? Not surprisingly, an anonymous method is a procedure or a function that does not have an associated name.

An anonymous method treats a block of code just like a value so that it can be assigned to a variable or used as a parameter to a method or returned by a function as its result value. In addition, an anonymous method can refer to variables and bind values to the variables in the context scope in which the anonymous method is defined. Anonymous methods are similar to closures defined in other languages such as JavaScript or C#. An anonymous method is declared as a reference to a method:

type
  TFuncOfString = reference to function(S: String): String;

Anonymous methods (or anonymous functions) are convenient to pass as an argument to a higher-order function. What's a higher-order...

Writing enumerable types

When the for..in loop was introduced in Delphi 2005, the concept of enumerable types was also introduced into the Delphi language.

As you know, there are some built-in enumerable types. However, you can create your own enumerable types using a very simple pattern.

To make your container enumerable, implement a single method called GetEnumerator, that must return a reference to an object, interface, or record, that implements the following three methods and one property (in the sample, the element to enumerate is TFoo):

function GetCurrent: TFoo;
function MoveNext: Boolean;
property Current: TFoo read GetCurrent;

There are a lot of samples related to standard enumerable types, so in this recipe you'll look at some not-so-common utilizations.

Getting ready

In this recipe, you'll see a file enumerable function as it exists in other, mostly dynamic, languages. The goal is to enumerate all the rows in a text file without actual opening, reading and closing the file...

RTTI to the rescue – configuring your class at runtime

Since Delphi 2010, the Delphi RTTI has been greatly expanded. Now it is comparable to what is called Reflection in other languages such as C# or Java. A much-improved RTTI can dramatically change the way you write, or even think about, your code and your architecture. Now, it is possible to write highly flexible code without too much effort.

Getting ready

What we want to do in this recipe is dynamically create a class looking for it by name among the classes that have been linked in the executable (or loaded from dynamic packages). The goal is to change the behavior of the program using only an external file without relying on a lot of parameters and complex configuration code; just create the right class. Wonderful!

Let's say you've developed a program to do orders. Your program allows only one-line orders, so you cannot buy different things in the same orders (this is a sample, man!). The form is shown in this screenshot...

Duck typing using RTTI

 

"When I see a bird that walks like a duck and swims like a duck and quacks like a duck, I call that bird a duck."

 
 --James Whitcomb Riley

Clear, isn't it? What may not be so clear is that this approach can be used also in computer programming. Yes, even without an actual duck!

Getting started

Referring to Duck typing, Wikipedia gives the following explanation (http://en.wikipedia.org/wiki/Duck_typing):

In computer programming with object-oriented programming languages, duck typing is a style of typing in which an object's methods and properties determine the valid semantics, rather than its inheritance from a particular class or implementation of an explicit interface.

How can all these concepts be used in everyday programming? This is the question that this recipe aims to answer.

Let's say that you have a form and you want to inform the user that something bad happened, changing all the colorable components to clRed. I don&apos...

Introduction


This chapter explains some of the not-so-obvious features of the language and the RTL that every Delphi programmer should know. There are ready-to-use recipes that will be useful every day and have been selected over many others.

Fun with anonymous methods – using higher-order functions


Since Version 2009, the Delphi language (or better, its Object Pascal dialect) supports anonymous methods. What's an anonymous method? Not surprisingly, an anonymous method is a procedure or a function that does not have an associated name.

An anonymous method treats a block of code just like a value so that it can be assigned to a variable or used as a parameter to a method or returned by a function as its result value. In addition, an anonymous method can refer to variables and bind values to the variables in the context scope in which the anonymous method is defined. Anonymous methods are similar to closures defined in other languages such as JavaScript or C#. An anonymous method is declared as a reference to a method:

type
  TFuncOfString = reference to function(S: String): String;

Anonymous methods (or anonymous functions) are convenient to pass as an argument to a higher-order function. What's a higher-order function?

Wikipedia...

Left arrow icon Right arrow icon

Description

Intended to refresh the basics of Delphi as well as advance your knowledge to the next level, it is assumed you will know RAD studio and the Object Pascal language. However, if you are not an experienced RAD studio programmer this accessible guide will still develop those initial crucial skills.

What you will learn

  • Create visually stunning applications using FireMonkey
  • Effectively use LiveBindings with the right OOP approach
  • Create serverside programs to serve RESTful web services and provide data to your mobile apps
  • Develop mobile apps using welldefined GUI design patterns for a great user experience
  • Build efficient mobile apps that read data from a remote server
  • Call the platformnative API on Android and iOS even for an unpublished API
  • Use extended RTTI to better manage the software customization for your customer
  • Leverage the most popular design patterns for a better design without wasting too much time debugging

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Sep 25, 2014
Length: 328 pages
Edition : 1st
Language : English
ISBN-13 : 9781783559596
Category :
Languages :

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Product Details

Publication date : Sep 25, 2014
Length: 328 pages
Edition : 1st
Language : English
ISBN-13 : 9781783559596
Category :
Languages :

Packt Subscriptions

See our plans and pricing
Modal Close icon
$19.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
$199.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just ₱260 each
Feature tick icon Exclusive print discounts
$279.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just ₱260 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total 8,114.97
Expert Delphi
₱2806.99
Delphi Cookbook
₱2500.99
Delphi Cookbook
₱2806.99
Total 8,114.97 Stars icon
Banner background image

Table of Contents

8 Chapters
1. Delphi Basics Chevron down icon Chevron up icon
2. Become a Delphi Language Ninja Chevron down icon Chevron up icon
3. Going Cross Platform with FireMonkey Chevron down icon Chevron up icon
4. The Thousand Faces of Multithreading Chevron down icon Chevron up icon
5. Putting Delphi on the Server Chevron down icon Chevron up icon
6. Riding the Mobile Revolution with FireMonkey Chevron down icon Chevron up icon
7. Using Specific Platform Features Chevron down icon Chevron up icon
Index Chevron down icon Chevron up icon

Customer reviews

Top Reviews
Rating distribution
Full star icon Full star icon Full star icon Full star icon Half star icon 4.5
(19 Ratings)
5 star 57.9%
4 star 31.6%
3 star 10.5%
2 star 0%
1 star 0%
Filter icon Filter
Top Reviews

Filter reviews by




Schrabi Feb 21, 2015
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Das Buch hat wertvolle Tips und Tricks parat - kommt schnell zur Sache und hat state-of-the-art Rezepte. EInfach empfehlenswert! AAA+
Amazon Verified review Amazon
Disappointed consumer Jul 14, 2015
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Great recipes for my toolbox. The techniques are excellent and anyone who reads this book will walk away with a lot of good knowledge. Why aren't they more books like this one out there? If you are a Delphi programmer making a leaving in this field it behooves you to own this book. Dear Mr. author, please sign me up for the next recipe book and also please tell all your guru friends to contribute or write one too.
Amazon Verified review Amazon
David Millington Oct 30, 2014
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Covers an amazingly wide variety of topics, with examples that show a lot more than just the nominal subject. It shows 'modern Delphi' - the 2014 environment and language and capabilities. There's something in here even for experienced Delphi programmers, but if you haven't used Delphi for a long time, read it to get a great overview of the state of the language. If you're new to Delphi and need to learn, this book will cover the entire environment, and you'll be up to speed with a good general knowledge of the development environment quickly.Some of the post-recipe discussion could have more detail.Buy it? Yes.(This is a short version of a full review posted here: http://parnassus.co/review-delphi-cookbook-daniele-teti/ Please note that I was given a free review copy of the book by the publisher, but was not paid and have no undisclosed affiliations.)
Amazon Verified review Amazon
John Kaster Nov 26, 2014
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Before proceeding with my review, you may want to read this other review by Simon J Stuart which provides a good overview of what you can find in the book. I also received a review copy from the publisher, and I’m glad I requested one!Overall impressionThe book uses a great pattern, where a topic is introduced, code is shown to demonstrate it, explanations and observations about the technique are discussed, and suggestions for additional usage or other resources are provided. This “explore more” section of the book may actually be its most valuable part, because Daniele provides specific links that point to good resources to use for each of the recipes covered.The code examples are useful and as concise as is reasonable.The order of the book is logical and does a good job of building on some of the previous recipes in later parts of the book:1.Delphi Basics2.Become a Delphi Language Ninja3.Going Cross-Platform with FireMonkey4.The Thousand Faces of Multithreading5.Putting Delphi on the Server6.Riding the Mobile Revolution with FireMonkey7.Using Specific (mobile) Platform FeaturesThis is a valuable book to keep handy for when you may need to implement any of the techniques it covers. In short, it is exactly what a “cookbook” should be, with quick and reliable recipes to help you create a good solution for a technical issue.Some specificsFor example, there’s a great, short exploration for taking advantage of generics. Hidden in one of the discussed “higher order” functions for generics (map, reduce/fold, filter) is something that deserves to be called out briefly in the book (although it is not).class function HigherOrder.Filter(InputArray: TArray; FilterFunction: TFunc): TArray;var I: Integer; List: TList;begin List := TList.Create; try for I := 0 to length(InputArray) - 1 do if FilterFunction(InputArray[I]) then List.Add(InputArray[I]); Result := List.ToArray; finally List.Free; end;end;This small sample shows one of the cleaner methods of populating a dynamic array result that can subsequently be lifetime managed by the runtime. The TList object is efficient for growing one item at a time. A dynamic array is not. Often, the result for the dynamic array is preallocated to some high bound (in the sample above, it would be the length of the input array), then resized before the return to shrink the array back down to the number of elements matching the filter. By using TList<T>, then freeing it, the array size is only manipulated once and the List.ToArray call makes the code clean and sustainable.Mobile development with FiremonkeyThe book has many detailed and useful examples for mobile development with FireMonkey. This is the section of the book where I learned the most, because I haven’t worked on mobile apps with Delphi. There are many useful tidbits covered in this section that are important for mobile development: using mobile databases, implementing server and client-side functionality, camera and phone manipulation, application lifecycle awareness, and more.Platform-specific featuresThe platform-specific features chapter goes beyond mobile FireMonkey recipes to making calls from Delphi to iOS and Android SDK calls, and is an excellent way to wrap up the book.Other recipes I’d like to seeSome things I think would be good to have in the book are:•In the UI section: ◦effective use of actions◦effective use of frames◦input value validations (form validation)•in the section on streaming, it would be handy to explore reliable determination of the encoding used by a file.•for threading, effective use of the new XE7 Parallels library•for Delphi on the server, ◦how to secure your web application (including using secure CDN resources for jQuery, etc)◦using dynamic arrays and records for JSON to avoid all the “free” calls for TObjectList<T> (this works very well with SuperObject, for example)A minor nitI don’t understand what Daniele means by “bounce” as in: “I see too many business applications that are composed by a bounce of dialog windows.”ConclusionDaniele did a great job on this book, and I hope he continues to update it with new recipes in the future.
Amazon Verified review Amazon
PJ-Jim Apr 14, 2015
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Very useful book for the Delphi programmer. I have studied it many times and am awaiting the next book from this same author.
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

How do I buy and download an eBook? Chevron down icon Chevron up icon

Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.

If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.

Please Note: Packt eBooks are non-returnable and non-refundable.

Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:

  • You may make copies of your eBook for your own use onto any machine
  • You may not pass copies of the eBook on to anyone else
How can I make a purchase on your website? Chevron down icon Chevron up icon

If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:

  1. Register on our website using your email address and the password.
  2. Search for the title by name or ISBN using the search option.
  3. Select the title you want to purchase.
  4. Choose the format you wish to purchase the title in; if you order the Print Book, you get a free eBook copy of the same title. 
  5. Proceed with the checkout process (payment to be made using Credit Card, Debit Cart, or PayPal)
Where can I access support around an eBook? Chevron down icon Chevron up icon
  • If you experience a problem with using or installing Adobe Reader, the contact Adobe directly.
  • To view the errata for the book, see www.packtpub.com/support and view the pages for the title you have.
  • To view your account details or to download a new copy of the book go to www.packtpub.com/account
  • To contact us directly if a problem is not resolved, use www.packtpub.com/contact-us
What eBook formats do Packt support? Chevron down icon Chevron up icon

Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.

You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.

What are the benefits of eBooks? Chevron down icon Chevron up icon
  • You can get the information you need immediately
  • You can easily take them with you on a laptop
  • You can download them an unlimited number of times
  • You can print them out
  • They are copy-paste enabled
  • They are searchable
  • There is no password protection
  • They are lower price than print
  • They save resources and space
What is an eBook? Chevron down icon Chevron up icon

Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.

When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.

For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.