StaDyn (programming language)
![]() | |
Paradigm | Object orientedf |
---|---|
Designed by | Francisco Ortin |
Developer | Computational Reflection research group[1] of the University of Oviedo |
First appeared | 2007 |
Stable release | 2.2.1
/ 11 May 2022 |
Typing discipline | Hybrid static and dynamic typing, gradual typing, strong, inferred |
Implementation language | C# |
Platform | Common Language Infrastructure (.NET Framework) |
License | MIT License[2] |
Website | reflection |
Major implementations | |
C# | |
Influenced by | |
C#, OCaml, StrongTalk, Boo |
StaDyn is an object-oriented general-purpose programming language for the .NET platform that supports both static and dynamic typing in the same programming language.
The StaDyn compiler gathers type information for the dynamically typed code. That type information is used to detect type errors at compilation time and to perform significant optimizations. For that purpose, it provides type reconstruction (inference), flow-sensitive types, union and intersection types, constraint-based typing, alias analysis and method specialization.
Its first prototype appeared in 2007, as a modification of C# 3.0. Type inference was supported by including var
as a new type, unlike C#, which only offers var
to define initialized local variables. Flow-sensitive types of var
references are inferred by the compiler, providing type-safe duck typing.[3] When a more lenient approach is required by the programmer, the dynamic
type could be used instead of var
. Although type inference is still performed, dynamic
references behave closer to those in dynamic languages.
StaDyn is designed by Francisco Ortin[4] from the University of Oviedo. The language has been implemented by different members of the Computational Reflection research group,[1] including Miguel Garcia, Jose Baltasar García Perez-Schofield and Jose Quiroga, besides Francisco Ortin.
The name StaDyn is a portmanteau of static and dynamic, denoting its aim to provide the benefits of both static and dynamic typing.
Code samples
Variables with different types
Just like dynamic languages, variables may hold different types in the same scope:
using System;
class Program {
public static void Main() {
Console.Write("Number: ");
var age = Console.In.ReadLine();
Console.WriteLine("Digits: " + age.Length);
age = Convert.ToInt32(age);
age++;
Console.WriteLine("Happy birthday, you are " + age +
" years old now.");
int length = age.Length; // * Compiler error
}
}
The age
variable is first inferred as string, so it is safe to get its Length
property. Then, it holds an integer, so age++
is a valid expression. The compiler detects an error in the last line, since Length
is no longer provided by age
.
The generated code does not use a single Object
variable to represent age, but two different variables whose types are string and int. This is achieved with a modification of the algorithm to compute the SSA form.[5] This makes the generated code to be more efficient, since runtime type conversions are not required.
Flow-sensitive types
var
and dynamic
variables can hold flow-sensitive types:
using System;
class Program {
public static void Main(String[] args) {
var exception;
if (args.Length > 0)
exception = new ApplicationException("An application exception.");
else
exception = new SystemException("A system exception.");
Console.WriteLine(exception.Message);
}
}
It is safe to get the Message
property from exception
because both ApplicationException
and SystemException
provide that property. Otherwise, a compiler error is shown. In this way, StaDyn provides a type-safe static duck-typing system.
In the following program:
using System;
class Program {
public static void Main(String[] args) {
var exception;
switch (args.Length) {
case 0:
exception = new ApplicationException("An application exception.");
break;
case 1:
exception = new SystemException("A system exception.");
break;
default:
exception = "This is not an exception.";
break;
}
Console.WriteLine(exception.Message); // * Compiler error with var, but not with dynamic
Console.WriteLine(exception.Unknown); // * Compiler error
}
}
The Message
property is not provided by String
, so a compiler error is shown for exception.Message
. However, if we declare exception
as dynamic
, the previous program is accepted by the compiler. dynamic
is more lenient than var
, following the flavor of dynamic languages. However, static type checking is still performed. This is shown in the last line of code, where the compiler shows an error for exception.Unknown
even if exception
is declared as dynamic
. This is because neither of the three possible types (ApplicationException
, SystemException
and String
) supports the Unknown
message.[6]
Although dynamic
and var
types can be used explicitly to obtain safer or more lenient type checking, the dynamism of single var
references can also be modified with command-line options, XML configuration files and a plugin for Visual Studio.[7]
See also
References
- ^ a b "Computational Reflection Research Group". uniovi.es. Retrieved May 17, 2022.
- ^ "StaDyn Download". uniovi.es. Retrieved May 17, 2022.
- ^ Francisco Ortin; Miguel Garcia; Sean McSweeney. "Rule-based program specialization to optimize gradually typed code". doi.org. Retrieved May 17, 2022.
- ^ "Francisco Ortin". uniovi.es. Retrieved May 17, 2022.
- ^ Jose Quiroga; Francisco Ortin. "SSA Transformations to Facilitate Type Inference in Dynamically Typed Code". doi.org. Retrieved May 17, 2022.
- ^ Francisco Ortin; Miguel Garcia. "Union and intersection types to support both dynamic and static typing". doi.org. Retrieved May 19, 2022.
- ^ Francisco Ortin; Francisco Morero; Anton Morant. "Static type information to improve the IDE features of hybrid dynamically and statically typed languages". doi.org. Retrieved May 19, 2022.