Philip Ira Alan Maynard

Slides:



Advertisements
Similar presentations
JavaScript I. JavaScript is an object oriented programming language used to add interactivity to web pages. Different from Java, even though bears some.
Advertisements

Semantic Analysis and Symbol Tables
Introduction to ML - Part 2 Kenny Zhu. What is next? ML has a rich set of structured values Tuples: (17, true, “stuff”) Records: {name = “george”, age.
Operators. Perl has MANY operators. –Covered in Chapter 3 of Prog.Perl Most operators have numeric and string version –remember Perl will convert variable.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
C Programming Tutorial – Part I CS Introduction to Operating Systems.
Chapter 3: Data Types and Operators JavaScript - Introductory.
Introduction to C++ // Program description #include directives int main() { constant declarations variable declarations executable statements return.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
Chapter 7 Templates. Objectives Introduction Function Templates Class Templates Standard Template Library.
C++ / G4MICE Course Session 2 Basic C++ types. Control and Looping Functions in C Function/method signatures and scope.
C++ Basics. Compilation What does compilation do? g++ hello.cpp g++ -o hello.cpp hello.
Introduction to Perl. What is Perl Perl is an interpreted language. This means you run it through an interpreter, not a compiler. Similar to shell script.
How to execute Program structure Variables name, keywords, binding, scope, lifetime Data types – type system – primitives, strings, arrays, hashes – pointers/references.
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 – Introduction to C# Programming Outline 3.1 Introduction 3.2 Simple Program: Printing a Line.
General Computer Science for Engineers CISC 106 Lecture 12 James Atlas Computer and Information Sciences 08/03/2009.
THE PREPROCESSOR
2: Basics Basics Programming C# © 2003 DevelopMentor, Inc. 12/1/2003.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Introduction to C++
Chad’s C++ Tutorial Demo Outline. 1. What is C++? C++ is an object-oriented programming (OOP) language that is viewed by many as the best language for.
Operators. Perl has MANY operators. –Covered in Chapter 3 of Camel –perldoc perlop Many operators have numeric and string version –remember Perl will.
Current Assignments Project 3 has been posted, due next Tuesday. Write a contact manager. Homework 6 will be posted this afternoon and will be due Friday.
Windows Programming Lecture 06. Data Types Classification Data types are classified in two categories that is, – those data types which stores decimal.
1Object-Oriented Program Development Using C++ Built-in Data Types Data type –Range of values –Set of operations on those values Literal: refers to acceptable.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
C++ Lesson 1.
C++ First Steps.
Chapter 1.2 Introduction to C++ Programming
Andy Wang Object Oriented Programming in C++ COP 3330
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Ruby: An Introduction Created by Yukihiro Matsumoto in 1993 (named after his birthstone) Pure OO language (even the number 1 is an instance of a class)
Chapter 1.2 Introduction to C++ Programming
Data types Data types Basic types
CSE341: Programming Languages Lecture 15 Macros
2.5 Another Java Application: Adding Integers
C Programming Tutorial – Part I
Data types and variables
Computer Programming Methodology Introduction to Java
C Basics.
Programming Language Concepts (CIS 635)
CSE341: Programming Languages Lecture 15 Macros
Starting JavaProgramming
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
2.1 Parts of a C++ Program.
Functions A function is a “pre-packaged” block of code written to perform a well-defined task Why? Code sharing and reusability Reduces errors Write and.
Variables Numbers can be stored and retrieved while a program is running if they are given a home. The way that integers and decimal numbers are stored.
CISC101 Reminders Assn 3 due tomorrow, 7pm.
Andy Wang Object Oriented Programming in C++ COP 3330
Dr. Bhargavi Dept of CS CHRIST
CSE341: Programming Languages Lecture 15 Macros
Chapter 3 – Introduction to C# Programming
CISC124 Labs start this week in JEFF 155. Fall 2018
Python Primer 1: Types and Operators
C Programming Getting started Variables Basic C operators Conditionals
Chapter 2: Introduction to C++.
CSE341: Programming Languages Lecture 15 Macros
Programming Introduction to C++.
CSE341: Programming Languages Lecture 15 Macros
JavaScript CS 4640 Programming Languages for Web Applications
Fundamental OOP Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Let’s start from the beginning
The C Language: Intro.
In this class, we will cover:
Just Enough Java 17-May-19.
CISC101 Reminders Assignment 3 due today.
CSE 3302 Programming Languages
CSE341: Programming Languages Lecture 15 Macros
JavaScript CS 4640 Programming Languages for Web Applications
CSE341: Programming Languages Lecture 15 Macros
Presentation transcript:

Philip Ira Alan Maynard

History First appeared on June 18th 2014 Developed by Ary Borenszweig and Juan Wajnerman Originally named Joy, but renamed to Crystal Actively being contributed on GitHub In July 2016 Crystal joins the TIOBE index Is one of the hottest languages in 2018

Purpose Resemble Ruby in syntax, to be simplistic Performs with the efficacy of C/C++ Static typed language Heavy use of Macros Use of Shard technology

Hello World! Hello World! The classic "hello world" program looks like this in Crystal: puts "Hello world!" From this you can see that the main routine is simply the program itself: there's no need to define a "main" function or something similar.

Semantics Comments Comments start with the # character. Only one-line comments are supported for now. # This is a comment

Static Typing Crystal is a statically typed language Users need to be specific about the type of types they give objects This means that the compiler catches more mistakes Users will have more confidence in the final product

Literals Literal Sample values Nil Bool Integers Floats Char String true, false Integers 18, -12, 19_i64, 14_u32,64_u8 Floats 1.0, 1.0_f32, 1e10, -0.5 Char 'a', '\n', 'あ' String "foo\tbar", %("あ"), %q(foo #{foo}) Symbol :symbol, :"foo bar" Array [1, 2, 3], [1, 2, 3] of Int32, %w(one two three) Array-like Set{1, 2, 3} Hash {"foo" => 2}, {} of String => Int32

Literals Continued Hash-likeMyType{"foo" => "bar"} Range1..9, 1...10, 0..var Regex/(foo)?bar/, /foo #{foo}/imx, %r(foo/) Tuple{1, "hello", 'x'} NamedTuple{name: "Crystal", year: 2011}, {"this is a key": 1} Proc->(x : Int32, y : Int32) { x + y }

Binary Operators + – addition - – subtraction * – multiplication / – division % – modulo & – bitwise and | – bitwise or ^ – bitwise xor ** – exponentiation << – shift left, append >> – shift right == – equals != – not equals < – less <= – less or equal > – greater >= – greater or equal <=> – comparison === – case equality

Assignments and Multiple Assignments Assignment is done with the equals (=) character. # Assigns to a local variable local = 1 # Assigns to an instance variable @instance = 2 # Assigns to a class variable @@class = 3 Multiple assignment You can declare/assign multiple variables at the same time by separating expressions with a comma (,): name, age = "Crystal", 1 # The above is the same as this: temp1 = "Crystal" temp2 = 1 name = temp1 age = temp2

Splats A method can receive a variable number of arguments by using a splat (*), which can appear only once and in any position def sum(*elements, initial) total = initial elements.each do |value| total += value end total end sum 1, 2, 3 # Error, missing argument: initial sum 1, 2, 3, initial: 10 # => 16

Type Restrictions Type restrictions are type annotations put to method arguments to restrict the types accepted by that method. def add(x : Number, y : Number) x + y end # Ok add 1, 2 # Ok # Error: no overload matches 'add' with types Bool, Bool add true, false Possible to use Classes as restrictions. Using, for example, Int32 as a type restriction makes the method only accept instances of Int32: def foo(x : Int32) end foo 1 # OK foo "hello" # Error

Requiring Requiring files Writing a program in a single file is OK for little snippets and small benchmark code. Big programs are better maintained and understood when split across different files. To make the compiler process other files you use require "...". It accepts a single argument, a string literal, and it can come in many flavors. Once a file is required, the compiler remembers its absolute path and later requires of that same file will be ignored.

Object Oriented Programming Crystal is an OOP language Everything in a Crystal Program is an object Example in creation of a class class Person def initialize(name : String) @name = name @age = 0 end def name @name end def age @age end end

Method Creation Creation of Methods is straightforward In action class Person def initialize(@name : String) @age = 0 end def age @age end def become_older @age += 1 end end In action john = Person.new "John" peter = Person.new "Peter" john.age #=> 0 john.become_older john.age #=> 1 peter.age #=> 0

Method Visibility Protected Methods A protected method can only be invoked on: instances of the same type as the current type instances in the same namespace (class, struct, module, etc.) as the current type module Namespace class Foo protected def foo puts "Hello" end end class Bar def bar # Works, because Foo and Bar are under Namespace Foo.new.foo end end end Namespace::Bar.new.bar Method Visibility Methods are public by default: the compiler will always let you invoke them, even if there is no public keyword. Methods can be marked as private or protected. Private Method class Person private def say(message) puts message end def say_hello say "hello" # OK, no receiver self.say "hello" # Error, self is a receiver other = Person.new "Other" other.say "hello" # Error, other is a receiver end end

Macros Macros are methods that receive AST nodes at compile-time and produce code that is pasted into a program. For example: macro define_method(name, content) def {{name}} {{content}} end end # This generates: # # def foo # 1 # end define_method foo, 1 foo #=> 1

More on Macros The scope of a macro is to anything below it in the hierarchy. Macros will tend to be localized inside their macro when called upon.

Concurrency By using the keyword spawn it is possible to have multiple threads. Not ideal but is definitely possible Done better by binding to C libraries. channel = Channel(Int32).new spawn do puts "Before first send" channel.send(1) puts "Before second send" channel.send(2) end puts "Before first receive" value = channel.receive puts value # => 1 puts "Before second receive" value = channel.receive puts value # => 2

Binding C Ability to use C libraries Uses C Code as objects Very useful for ease of programming No need to rewrite everything require "./sayhi_c/*" @[Link(ldflags: "#{__DIR__}/sayhi_c/sayhi.o")] lib Say fun hi(name : LibC::Char*) : Void end Say.hi("Auth0") $ crystal build --release src/sayhi_c.cr $ ./sayhi_c > Hi Auth0!

Multiple Web Frameworks

Issues with the Language No Parallelism with pure Crystal Need to link to C code to have this ability Different from Ruby where Parallelism is weak Relatively New so a lot of uncharted territory