Programming Languages Concepts: Difference between revisions
Line 79: | Line 79: | ||
=Polymorphism= | =Polymorphism= | ||
Polymorphism is a feature of a programming language allowing to write code that behaves differently depending on the runtime state at a specific moment in time. The contract of the behavior is defined by an ''interface'', while the implementation of the interface can vary. In Java, different classes may implement an interface, and instances of those classes can be used interchangeably as that interface. In Go, different concrete types implement an interface. | Polymorphism is a feature of a programming language allowing to write code that behaves differently depending on the runtime state at a specific moment in time. The contract of the behavior is defined by an '''interface''', while the implementation of the interface can vary. In Java, different classes may implement an interface, and instances of those classes can be used interchangeably as that interface. In Go, different concrete types implement an interface. | ||
<blockquote style="background-color: #f9f9f9; border: solid thin lightgrey;"> | <blockquote style="background-color: #f9f9f9; border: solid thin lightgrey;"> |
Revision as of 16:40, 30 August 2023
External
Internal
Programming Language Categories
- Machine languages. These languages are directly executed on the CPU. The instructions refer simple operations and registers.
- Assembly languages. An assembly languages is almost one-to-one mapping to the equivalent machine language. It uses English mnemonics.
- High-level language: C, C++, Java, Python, Go. The come with higher level abstractions like types, variables, etc.
Compilation vs. Interpretation
In compiled languages, the translation between high-level language to machine code is done only once. It does not occur while you're running the code, it's being done before running the code, and at execution time, the machine code generated in advance it is executed directly.
A compiler does the transaction between the high-level language into machine code. An interpreted language translates high-level instructions into machine language every time the code is executed, while the code is being executed.
Interpreted languages require an interpreter. Interpreters come with a few advantages: they manage the memory automatically, by performing garbage collection, they infer variable types, etc. Python has an interpreter, that provides the runtime for Python programs.
Java is both compiled and interpreted. Java high-level language is compiled into bytecode, and the bytecode is interpreted to machine language every time it runs in the JVM.
The compiled code is generally faster than the interpreted code because you need to do the translation every time you run interpreted code.
Variables, Parameters, Arguments
Statement
Expression
Typing
Type
A type determines the set of values and operations specific to values of that type, and the way the instances of the type are stored in memory - the size of the values. Expressions of a certain type (variables, functions) take their values from the type's set of values.
The Primitive vs. Non-Primitive Nature of Types
This conversation started in the Go Language page. Next time it's needed to be applied to a different language, hoist it here.
Static Typing vs Dynamic Typing
- Wikipedia Type System https://en.wikipedia.org/wiki/Type_system
For a statically typed system, the variables and expressions always have a specific type, and that type cannot be changed. This is where "static" comes from: a variable will always have the same type. The type is known at compile-time. The compiler uses this information to catch some classes of errors at compile time and produce low-level machine language that typically runs faster. C, C++, Java and Go are statically typed languages, and much of the code written in such language is used to declare types.
Dynamically typed languages are convenient, because there are no intermediate steps between writing the code and executing it. A dynamically typed language, also called a scripting language, do not forces the programmer to declare the type of a variable before using them. Dynamic languages are not compiled, but interpreted. However certain types of errors cannot be caught until the program executes. For statically typed languages, many of these errors are caught at the compilation phase. On the downside, static languages usually comes with a great deal of ceremony around everything that happens in the program (heavy syntax, type annotations, complex type hierarchies). Python is a dynamically typed language.
Strong Typing vs Loose Typing
- Wikipedia Strong and Weak Typing https://en.wikipedia.org/wiki/Strong_and_weak_typing
In a strong typed language the type of an instance does not change, even if the value is mutable.
Python and Go are strongly typed languages.
Type Safety
Type safety is the extent to which a programming language discourages or prevents type errors. Type enforcement can be static, exercised at compile-time, or dynamic, associated type information with values and detecting type errors at run-time, or both. Java and C# are examples of type-safe languages.
Type Systems
- The Python Type System
- The Go Type System
- The JavaScript Type System
Scoping
Static and dynamic scoping: https://www.geeksforgeeks.org/static-and-dynamic-scoping/
Duck Typing
Polymorphism
Polymorphism is a feature of a programming language allowing to write code that behaves differently depending on the runtime state at a specific moment in time. The contract of the behavior is defined by an interface, while the implementation of the interface can vary. In Java, different classes may implement an interface, and instances of those classes can be used interchangeably as that interface. In Go, different concrete types implement an interface.
Metaprogramming
Metaprogramming is writing code that manipulates other code, or even itself.