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
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Arrow up icon
GO TO TOP
Kotlin Quick Start Guide

You're reading from   Kotlin Quick Start Guide Core features to get you ready for developing applications

Arrow left icon
Product type Paperback
Published in Aug 2018
Publisher Packt
ISBN-13 9781789344189
Length 178 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Marko Devcic Marko Devcic
Author Profile Icon Marko Devcic
Marko Devcic
Arrow right icon
View More author details
Toc

Building a simple DSL


We can now put to use what we learned about receiver and infix functions and create a DSL for validating objects. To keep it simple, we'll only be validatingInttypes.

First, we'll add the NumberValidator interface. This is where all the different validators can represent their validation logic:

interface NumberValidator 
{

fun isValueValid(value: Int): Boolean

}

Then, we need a type that will keep all the validators and the validated object. We'll name it Validator:

class Validator<T> private constructor(private val validatedObject: T) {

internal val validators = mutableListOf<NumberValidator>()
private lateinit var valueFactory: (T) -> Int
}

To enforce type safety, the Validator type is generic. It has a list of validators and also holds a function type called valueFactory, which knows how to return a property that will be validated. Notice how the constructor is private. We don’t allow the validator type to be created from outside; instead, we use an...

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime
Visually different images