Comparison of Programming Languages

From Unify Community Wiki
Revision as of 10:03, 9 October 2011 by Skoobiedu (Talk | contribs)

Jump to: navigation, search


This is a comparison of the features and instructions of the three high-level programming languages offered by Unity: JavaScript, C#, and Boo.


Contents

Conventions of This Article

The bold is the literal code. The non-bold is interpreted by the reader. Statements in guillemets (« … ») are optional. “⇥ ” indicates a necessary indent.

The following tables compare code differences of the Unity programming languages. See also the Comparison of Programming Languages for general information.


Features

The following tables compares major features of the Unity programming languages.

Language Boo C# UnityScript/JavaScript
Paradigm(s)
Standardized Variation? Yes Yes, ECMA, ISO ECMA-334; ISO/IEC 23270:2006 No
Type Strength strong, duck strong strong1, duck
Type Safety safe safe safe
Expression of Types implicit with optional explicit typing explicit implicit with optional explicit typing
Compatibility Among Composite Types ? name-based ?
Type Checking static with optional dynamic typing static with optional dynamic typing static with optional dynamic typing

1 Normally Javascript in Unity uses type inference to statically type all variables when possible, in cases where the type is not explicitly stated. Dynamic typing can be forced if desired, and, in certain circumstances, dynamic typing is used unless forced otherwise. Use of the "#pragma strict" directive disables all dynamic typing.

Type Identifiers

Integer

Language Boo C# UnityScript/JavaScript
8-bit (byte) Signed sbyte sbyte sbyte
8-bit (byte) Unsigned byte byte byte
16-bit (short integer) Signed short short short
16-bit (short integer) Unsigned ushort ushort ushort
32-bit Signed int int int
32-bit Unsigned uint uint uint
64-bit (long integer) Signed long long long
64-bit long integer) Unsigned ulong ulong ulong

Floating Point

Language Boo C# UnityScript/JavaScript
Single Precision single float float
Double Precision double double double
Arbitrarily Precise (bignum) decimal decimal ?

Other Types

Language Boo C# UnityScript/JavaScript
Text Character N/A char char
Text String string string String
Boolean) bool bool boolean
Object/Universal) object object Object


Derived Types

Array

Language Boo C# UnityScript/JavaScript
one-dimensional fixed size array <boo>(type)</boo> <csharp>type[size]</csharp> <javascript>type[size]</javascript>
multi-dimensional fixed size array ? <csharp>type[size1, size2,...]</csharp>

or <csharp>type[size1][size2]...</csharp>

<javascript>type[size1, size2,...]</javascript>

(note: currently these types of arrays can be created implicitly and used, but cannot be created explicitly)

one-dimensional dynamic size array <boo>List</boo>

or <boo>System.Collections.Generic.List[type]</boo> or <boo>System.Collections.ArrayList</boo>

<csharp>System.Collections.ArrayList</csharp>

or <csharp>System.Collections.Generic.List<type></csharp>

<javascript>Array</javascript>

or <javascript>System.Collections.ArrayList</javascript>

multi-dimensional dynamic size array ? <csharp>System.Collections.Generic.List<System.Collections.Generic.List<...>></csharp> ?

Other Types

Language Boo C# UnityScript/JavaScript
Record Simple Composite Types ? <csharp>struct name { type name; ... }</csharp> via objects: <javascript>class name { type name; ... }</javascript>
Enumeration Simple Composite Types <boo>enum condition:

⇥ item1 ⇥ «item2 ⇥ …»</boo>

<csharp>enum name «: integral-type» { name «= value», ... }</csharp> <javascript>enum type { name, ... }</javascript>

Basic Unity-Specific Types

Language Boo C# UnityScript/JavaScript
array ? ? <javascript>Array</javascript>


Declarations

Language Boo C# UnityScript/JavaScript
variable <boo>name = initial_value</boo> <csharp>type name «= initial_value»;</csharp> <javascript>var name «= initial_value»;</javascript>
constant <boo>final name = initial_value</boo> <csharp>const type name = value;</csharp> <javascript>N/A</javascript>
type synonym <boo>synonym = typeof(type)</boo> <csharp>using synonym = type;</csharp> ?

Flow of Control

Conditional Statements

Language Boo C# UnityScript/JavaScript
if <boo>if (condition):
   instructions

else:

   instructions</boo>
<csharp>if (condition) { instructions; }

«else { instructions; }»</csharp>

<javascript>if (condition) { instructions; }

«else { instructions; }»</javascript>

else if <boo>if (condition):

instructions elif: instructions</boo>

<csharp>if (condition) { instructions; }

else if (condition) { instructions; } ... «else { instructions; }»</csharp>

<javascript>if (condition) { instructions; }

else if (condition) { instructions; } ... «else { instructions; }»</javascript>

select case ? <csharp>switch (variable) {

case case1: instructions; jump-statement; ... «default: instructions; jump-statement;» }</csharp>

<javascript>switch (variable) {

case case1: instructions; jump-statement; ... «default: instructions; jump-statement;» }</javascript>

conditional expression (ternary) N/A <csharp>condition ? valueIfTrue : valueIfFalse;</csharp> <javascript>condition ? valueIfTrue : valueIfFalse;</javascript>

Loop Statements

Language Boo C# UnityScript/JavaScript
while <boo>while (condition):
   instructions</boo>
<csharp>while (condition) { instructions; }</csharp> <javascript>while (condition) { instructions; }</javascript>
do-while ? <csharp>do { instructions; } while(condition);</csharp> ?
for <boo>for i in range(first, last):
   instructions</boo>
<csharp>for («initializer»; «condition»; «modifier») { instructions; }</csharp> ?
foreach ? <csharp>foreach (type name in collection) { instructions; }</csharp> ?

Exceptions

Language Boo C# UnityScript/JavaScript
throw ? <csharp>throw new exceptionType(«arguments»);</csharp>

or <csharp>throw exceptionVariable;</csharp> or <csharp>throw; // re-throws an exception; can only be used inside a catch block</csharp>

?
handler ? <csharp>try { instructions; }

«catch(exceptionType «variable») { «instructions;» }» ... catch«(exceptionType «variable»)» { «instructions;» }</csharp> or <csharp>try { instructions; } finally { instructions; }</csharp> or <csharp>try { instructions; } «catch(exceptionType «variable») { «instructions;» }» ... catch«(exceptionType «variable»)» { «instructions;» } finally { instructions; }</csharp>

?
assertion ? <csharp>System.Diagnostics.Debug.Assert(condition «, message «, detailsMessage»»);</csharp> ?

Other Flow of Control Statements

Language Boo C# UnityScript/JavaScript
exit block(break) <boo>break</boo> <csharp>break;</csharp> ?
continue <boo>continue</boo> <csharp>continue;</csharp> ?
label ? <csharp>labelName:</csharp> ?
branch (goto) ? <csharp>goto labelName;</csharp>

or <csharp>goto case caseValue; // can only be used in a switch block</csharp>

?
return value from generator ? ? ?

Functions

Language Boo C# UnityScript/JavaScript
calling a function ? <csharp>functionName(arg1, arg2, ...);</csharp> ?
basic/void function ? <csharp>void functionName(type param1, type param2, ...) { instructions; }</csharp> ?
value-returning function ? <csharp>type functionName(type param1, type param2, ...) { instructions; return valueOfFunctionType; }</csharp> ?

Type Conversions

Language Boo C# UnityScript/JavaScript
basic type to basic type ? <csharp>(type) name</csharp> ?
string to basic type ? <csharp>type.Parse(name) /* THIS IS HIGHLY DEPENDENT UPON THE BASIC TYPE */</csharp> ?
basic type to string ? <csharp>name.ToString()</csharp> ?
complex type to string ? <csharp>name.ToString()</csharp> ?
complex type to complex type ? <csharp>(type) name</csharp>

or <csharp>name as type</csharp>

<javascript>name as type</javascript>



Programming Index

Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox