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 123456789101112131415161718192021222324import 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); // 2Console.log(p2.y); // 3 'IExportable' Generic Constraint for working with external APIs 1234567891011121314151617181920import 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