toExternal

Summary

Defines a conversion to external.

Usage

external toExternal()

Returns

The converted external equivalent of the internal type.

Description

Defines a conversion from an internal type to external.

Examples

Basic Usage
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import System;
 
class Point : IExportable
{
    int x;
    int y;
  
    Point(int x, int y) {
        this.x = x;
        this.y = y;
    }
  
    override function toExternal() {
        return {
            x: x,
            y: y
        };
    }
}
  
Point p = new Point(2, 3);
var p2 = p.toExternal(); // conversion to 'external'
Console.log(p2.x); // 2
Console.log(p2.y); // 3
'IExportable' Generic Constraint for working with external APIs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import System;
 
external mysql = { query: function(q, a) { Console.log(q, a); } };
 
// Constrain type arguments to only classes that can convert to 'external'
interface ICommand<T: IExportable>
{
    void execute(...T args);
}
 
class CreateUser : ICommand<String>
{
    final void execute(...String args) {
        string username = args[0] ?? "";
        mysql.query("INSERT INTO `users`(`username`) VALUES(?);", username.toExternal());
    }
}
 
auto createUserCommand = new CreateUser();
createUserCommand.execute("foo"); // "INSERT INTO `users`(`username`) VALUES(?); foo"

Share

HTML | BBCode | Direct Link