Presentation is loading. Please wait.

Presentation is loading. Please wait.

Philip Ira Alan Maynard

Similar presentations


Presentation on theme: "Philip Ira Alan Maynard"— Presentation transcript:

1 Philip Ira Alan Maynard

2 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

3 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

4 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.

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

6 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

7 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

8 Literals Continued Hash-likeMyType{"foo" => "bar"}
Range1..9, , 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 }

9 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

10 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 = 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

11 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

12 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 # OK foo "hello" # Error

13 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.

14 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

15 Method Creation Creation of Methods is straightforward In action
class Person def : 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

16 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

17 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 # # end define_method foo, 1 foo #=> 1

18 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.

19 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

20 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!

21 Multiple Web Frameworks

22 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


Download ppt "Philip Ira Alan Maynard"

Similar presentations


Ads by Google