Presentation is loading. Please wait.

Presentation is loading. Please wait.

What do you need to know about a new language?

Similar presentations


Presentation on theme: "What do you need to know about a new language?"— Presentation transcript:

1 What do you need to know about a new language?
How to execute Program structure Variables name, keywords, binding, scope, lifetime Data types type system primitives, strings, arrays, hashes pointers/references type conversions and equality Expressions Operators, overloading, booleans, short-circuiting, conditional expression Referential transparency Statements vs Expressions Control flow conditionals loops Functions Classes Exception handling Other features Threads Reflection Libraries Functional Language – other aspects, covered later

2 Language Principles

3 The Concept of Binding A binding is an association, such as:
bind type of variable bind operation to symbol (e.g., meaning of *) bind function to its definition Binding time is the time at which a binding takes place. Bindings may be static or dynamic explicit or implicit

4 Possible Binding Times
Language design time -- bind operator symbols to operations : sum = sum + count msg = “Hello” + name Language implementation time-- bind type to a representation : int => number of bits, etc. Compile time -- bind a variable to a type: int count; Link time – bind library subprogram to code: cout << x; Load time -- bind a FORTRAN 77 variable to a memory cell (or a C static variable) Runtime -- bind a nonstatic local variable to a memory cell

5 Static vs. Dynamic Binding
A binding is static if: it first occurs before run time and it remains unchanged throughout program execution. A binding is dynamic if: it first occurs during execution or it can change during execution of the program NOTE: doesn't consider paging etc. which is at the hardware level

6 Static vs Dynamic This distinction may apply to: variable typing
variable lifetime variable scope polymorphism overloaded operators vs late binding Static is also used to identify class vs instance variables – not really a static vs dynamic example

7 Dynamic Type Binding Type not specified by declaration, not determined by name (JavaScript, PHP, Ruby) Specified through an assignment statement list = [2, 4.33, 6, 8]; list = 17.3; Advantage: flexibility (generic program units) Disadvantages: High cost (dynamic type checking requires run-time descriptors, normally interpreted… upcoming discussion) Type error detection by the compiler is difficult Explored on following slides

8 Explore Generics Read this: With a partner:
With a partner: Read the definition of Type variable Look at how List is defined What is the type variable? How is it used? Skip Wildcards Be sure you understand class Entry Turn in for class participation

9 Explore Generics Read this: Discuss the Stack example
Discuss the Stack example You don’t need to memorize syntax, but you should understand the purpose of templates. We’ll be contrasting Java/C++ with Ruby.

10 Assume you are designing a language with dynamic typing
How would you implement dynamic types? What data structure(s) would you use? list = 3.6 list = [3.4, 5.6] list = [1,2,3] How does this impact your code – consider efficiency, reliability. Now think about challenges with + total = 3 + 5 message = “hello” + “ world” something = “count “ other = 3 + “count” Turn in for class participation No right answer, just give it some thought Would this be a challenge for compiler or runtime system?

11 Dynamic Type Reliability Issue
i = x; // desired, x is scalar i = y; // typed accidentally, y is array

12 Strong vs weak typing Definitions are not precise
In general, a strongly typed language will generate a compiler error if the value used (e.g., passed to a function, assigned to a variable) does not match the expected type May also be considered strongly typed if type errors are prevented at runtime due to dynamic typing (so type safety is more important for reliability) A language may be considered weakly typed if it includes features that allow types to be used interchangeably

13 Features regarded as “weaker”
Implicit type conversions Pointers* Untagged unions* * covered later

14 Type conversions Widening Conversions: can include at least approximations to all of the values of the original type. Examples (Java/C++) byte to short, int, long, float or double short to int, long, float or double char to int, long, float or double int to long, float or double long to float or double float to double Narrowing conversions: cannot include all of the values of the original type short to byte or char char to byte or short int to byte, short, or char long to byte, short, char or int float to byte, short, char, int or long double to byte, short, char, int, long or float

15 Type conversions – dangerous?
Even widening conversions may lose accuracy. Example: integers stored in 32 bits, 9 digits of precision. Floating point values also stored in 32 bits, only about 7 digits of precision (because of space used for exponent). Conversions should be used with care! Warnings should not just be ignored… Strongly typed language minimizes type conversions

16 Implicit Type Conversions
A language with more implicit conversions is considered less strongly typed C supports more implicit conversions than Java

17 Explore Implicit Conversions
Implicit conversions. Write a line of code that would illustrate one of the scenarios. Array to pointer conversion. Draw a picture and 1-2 lines of code that illustrate. Did you know: C++ will do an implicit conversion if there is a single-argument ctor that will do the needed conversion? Add to page for class participation

18 Type safety Type safety is the extent to which a programming language discourages or prevents type errors A type error is erroneous or undesirable program behavior caused by a discrepancy between differing data types (e.g., trying to perform an operation that is not valid for that type) Type enforcement can be static (compile time) or dynamic (run-time)

19 Explicit vs Implicit Explicit – stated by programmer
Implicit – determined by language Can be applied to: type declaration variable lifetime These are NOT the same as static/dynamic, but are often confused

20 Explicit/Implicit Declaration
An explicit declaration is a program statement used for declaring the types of variables: int count; An implicit declaration is a default mechanism for specifying types of variables Both create static bindings to types (i.e., type doesn’t change during execution of program) FORTRAN, PL/I, BASIC, and Perl provide implicit declarations Advantage: writability Disadvantage: reliability is array, % is hash, $ is scalar Fortran: I-N integer, others single precision, can override

21 Keywords vs Reserved Words
many words have special meaning (e.g. if, true, def) Keyword: has special meaning in particular context, but can be used as variable name Algol, PL/I, Fortran Reserved: can’t be used as variable COBOL has ~400, Java has ~50 Ruby has reserved words (evolving, no fixed number) Advantage: may avoid confusion Disadvantage: may need to be aware of language parts you aren’t even using

22 Keyword vs Reserved In Fortran, this is potentially valid:
if if then then else else In Java, goto is a reserved word (you can’t use) but not a keyword (language doesn’t use) Functions in libraries are not keywords OR reserved words. Can sometimes cause confusion.

23 JavaScript example <!DOCTYPE html> <html> <body> <pre> <script> function bark() {}; bark.prototype.alert = "woof"; bark.prototype.alertOwner = function(){return this.alert}; var myDog = new bark(); alert(myDog.alert); // displays dialog saying woof </script> </pre> </body> </html>

24 Unconditional Branching
Transfers execution control to a specified place in the program Represented one of the most heated debates in 1960’s and 1970’s Well-known mechanism: goto statement Major concern: Readability Some languages do not support goto statement (e.g., Module-2, Java, Python, Ruby) C# offers goto statement (can be used in switch statements) Loop exit statements are restricted and somewhat camouflaged goto’s

25 Go To Read this: Why are goto statements harmful??
Why are goto statements harmful?? Add brief explanation to class participation


Download ppt "What do you need to know about a new language?"

Similar presentations


Ads by Google