MiniD
File:Minidlogo2.png Logo designed by Andrzej Rutkowski | |
Usual file extensions | .md |
---|---|
Paradigm | Multi-paradigm |
Appeared in | 2006 |
Designed by | Jarrett Billingsley |
Developer | Jarrett Billingsley |
Stable release | 2.0 (June 16, 2009) |
Typing discipline | Dynamic |
Influenced by | D, Lua, Squirrel, Python, Io, ECMAScript |
OS | Cross-platform |
License | zlib/libpng |
Website | MiniD DSource Page |
The MiniD programming language is a small, lightweight, extension language in the vein of Lua or Squirrel, but designed to be used mainly with the D programming language. It supports both object-oriented and imperative programming paradigms, as well as some simple functional aspects.
Distributed under the licence of zlib/libpng, MiniD is free software.
History
MiniD began in June 2006 as an idea for a statically-typed language, much like a stripped-down version of the D programming language. This is the reason for the name "MiniD". After work began on the compiler, the creator, Jarrett Billingsley, realized just how large a project this language was becoming, and decided to recast the language into something simpler to implement. The result was a Lua-like language with a C-style syntax. Over the next several months, MiniD acquired features from various languages, such as Squirrel-like classes, a D-like module system, and Lua-like collaborative multithreading. On August 1, 2007, after more than thirteen months of planning and programming, version 1.0 of the reference implementation was released. The version 1.0 language specification is frozen.
As of June 15, 2009, version 2 of MiniD has been released. Version 2 brings a major reimplementation of most of the library in order to support its own garbage collector rather than relying on the underlying D garbage collector, for better behavior in realtime applications such as games. Version 2 also brings several changes to the language and standard libraries.
Features
MiniD provides a small but flexible set of data types, similar to that of Lua's or Squirrel's. Unlike Lua, MiniD provides explicit support for object-oriented programming with classes. MiniD also provides a module system and coroutines as core language features, like Lua. MiniD is garbage-collected, with support for first-class functions, closures, and tail recursion.
MiniD also tries to be more robust than typical dynamic languages, making it easier to catch bugs sooner. For example, it does not have implicit variable declarations and accessing globals that do not exist throws an error instead of giving a default value (as in Lua). Another very helpful feature is "parameter type constraints," which are a way of specifying valid types that function parameters may accept. These checks are still performed at runtime unlike in static languages, but their concise syntax and negligible performance impact make them much more attractive and easy-to-use than similar solutions in other dynamic languages. They can help greatly in catching bugs that would cause functions to malfunction or corrupt data structures if called with unexpected parameter types. A small example is given below.
Example code
The following example code is for MiniD 2. (Note that due to technical limitations, some keywords are not highlighted here, as Wikipedia does not have a source highlighter for MiniD.)
Here is the Hello world program in MiniD.
module test
writeln("Hello, world!")
Every MiniD source file must begin with a module declaration. For simplicity, the module declaration has been omitted in the rest of the examples.
class Test
{
x = 0
y = 0
this(x, y)
{
:x = x
:y = y
}
function toString() = format("x = {} y = {}", :x, :y)
}
local t = Test(3, 4)
writeln(t)
This example shows a simple class with two fields, x and y, which are initialized to 0 by default. The class's constructor, declared with the 'this' keyword, takes two parameters and assigns them to the instance's fields. The syntax ":x" is shorthand for "this.x", where "this" is the object upon which a method was called. Just like in Lua or Python, members of "this" must be accessed explicitly.
The class has one method, 'toString', which is called automatically when the object needs to be converted to a string. The somewhat unusual syntax used here is inspired by many functional languages and is shorthand for the following:
function toString()
{
return format("x = {} y = {}", :x, :y)
}
Finally, the class is instantiated by calling it like a function, similarly to Python or Squirrel. When the instance is printed out using 'writeln', the 'toString' method is called, and so this program outputs "x = 3 y = 4".
local a = array.range(1, 11)
local b = a.map(\x -> x * x)
writeln(b)
This example demonstrates some of MiniD's array manipulation abilities. Unlike Lua, MiniD has a separate array type. In this example, an array is created that holds the values 1 through 10 using the 'array.range' function. Then the 'map' method of arrays is used on 'a', and it takes a function literal which returns the square of its parameter. This literal syntax is again inspired by functional languages (such as Haskell) and is shorthand for the following:
local b = a.map(function(x) { return x * x })
When 'b' is printed, it shows the first ten squares, that is "[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]".
writeln([x * x for x in 1 .. 11])
This shows a shorter way of achieving the same result, using MiniD's list comprehensions. The syntax is very close to Python's.
local a = [1, 2, 3, 4, 5]
local t = { x = 5, y = 10 }
local s = "hello"
foreach(i, v; a)
writefln("a[{}] = {}", i, v)
writeln()
foreach(i, v; t)
writefln("t.{} = {}", i, v)
writeln()
foreach(i, v; s)
writefln("s[{}] = {}", i, v)
This example shows the 'foreach' loop, which can be used to iterate over arrays, tables, and strings as shown here, as well as other types.
function countDown(val: int) = coroutine function()
{
while(val > 0)
{
yield(null, val) // this is like yielding an index and a value
val--
}
}
foreach(v; countDown(5))
writefln(v)
This example shows the use of the 'foreach' loop to iterate over a coroutine. In this way, coroutines can be used as generators.
function first(x: array|string) = x[0]
writeln(first([1, 2, 3])) // prints 1
writeln(first("hello")) // prints h
writeln(first(45)) // error, invalid parameter type 'int'
This shows a simple use of parameter type constraints, a way of putting runtime checks on parameters to restrict their allowable types. The 'first' function allows only arrays and strings for its parameter 'x'. The first two print statements work fine, but the third throws an error since integers are not an allowable type for 'x'.
External links
If you like SEOmastering Site, you can support it by - BTC: bc1qppjcl3c2cyjazy6lepmrv3fh6ke9mxs7zpfky0 , TRC20 and more...