From 0cf568d278db4cb766723e30d88b15c38f1d4536 Mon Sep 17 00:00:00 2001 From: Leah Hanson Date: Tue, 2 Jul 2013 18:55:23 -0400 Subject: finished first draft :) --- julia.html.markdown | 35 +++++++++++++++++++++++++++++------ 1 file changed, 29 insertions(+), 6 deletions(-) (limited to 'julia.html.markdown') diff --git a/julia.html.markdown b/julia.html.markdown index e18fc3cd..f722f7ee 100644 --- a/julia.html.markdown +++ b/julia.html.markdown @@ -429,14 +429,37 @@ type Panther <: Cat # Panther is also a subtype of Cat Panther() = new("green") # Panthers will only have this constructor, and no default constructor. end -#### -#### In progress point -#### +# Multiple Dispatch + +# In Julia, all named functions are generic functions +# This means that they are built up from many small methods +# For example, let's make a function meow: +function meow(cat::Lion) + cat.roar # access properties using dot notation +end + +function meow(cat::Panther) + "grrr" +end + +function meow(cat::Tiger) + "rawwwr" +end + +meow(tigger) #=> "rawwr" +meow(Lion("brown","ROAAR")) #=> "ROAAR" +meow(Panther()) #=> "grrr" + +function pet_cat(cat::Cat) + println("The cat says $(meow(cat))") +end + +pet_cat(tigger) #=> ERROR: no method pet_cat(Tiger,) +pet_cat(Lion(Panther(),"42")) #=> prints "The cat says 42" -#### Multiple Dispatch ## Further Reading -#### Link to Maunual -Still up for more? Try [Learn Python The Hard Way](http://learnpythonthehardway.org/book/) +You can get a lot more detail from [The Julia Manual](http://docs.julialang.org/en/latest/manual/) + -- cgit v1.2.3