Tour #1 - Hello world.
Let's start with the basic Hello world example:
class HelloWorld {
Void main() {
echo("Hello world")
}
}
P.S: If you are from the Java world
- You don't need semi-colon (;) after every line
- you can do "echo()" instead of "System.out.println"
Saying hello to Fantom?
Welcome. To get started type your fantom code on the text editor on the right and press execute.
If you are new to Fantom Take a tour
Editor available in Themes
Tour #2 - String interpolation
Fantom lets you include values of variables within a string, try this
class StringInterpolation{
Void main(){
count := 3
//Not so sexy way
echo("Count is " + count)
//The sexy way with string-interpolation
echo("Count is $count")
}
}
P.S: If you are from the Java world
- You use := for declaration
- Type can be inferred. For eg., you don't need to type "Int count := 3", but just "count :=3"
Tour #3 - Literals
Play with some of the data types which has literal syntax
class DataTypes{
Void main(){
Int count := 3
Str name := "Green fox"
Bool isTrue := true
Uri google := `http://www.google.com`
}
}
P.S: If you are from the Java world
- There are no primitives.Everything's an object. you could do for eg., 3.toStr()
- Fantom calls em Int, Str, Bool etc. instead of "Integer", "String" and "Boolean"
- Yes you saw it right. Uri's are
enclosed by ` `(back ticks)
Tour #4 - Arrays are Lists.
Think of it as lists having Array-like concise syntax. or Arrays having the auto-grow feature of list. Whatever you think, this is how you code
class FanLists{
Void main(){
arr := Str["foo", "bar"]
echo("Value at position 0 is ${arr[0]}" )
arr.add("beer")
echo("newly added value is ${arr[2]}")
}
}
Tour #5 - Cooler Maps
Just like Arrays, Maps have a cooler syntax
class FanMaps{
Void main(){
arr := Str:Int["foo":1, "bar":2]
echo("Value of key 'foo' is " + arr["foo"])
}
}
Tour #6 - Non nullable fields
Fields are by default non-nullable. You can't assign null to them. However you can add a "?" to the type of the field name to make it nullable
class Nullables{
Void main(){
Int count := 3 //Non-Nullable Int
Int? anotherCount := 3 //Nullable Int
//This will be a compiler error
count = null
//This will not be
anotherCount = null
}
}
Tour #7 - More classes
Here's is an example of creating and using more than one class
class Caller{
Void main(){
a := Another()
a.hi
}
}
class Another{
Void hi(){
echo("Hey!, Hi.")
}
}
P.S: If you are from the Java world
- You can call an empty method without the (). for Eg., "a.hi", instead of "a.hi()"
Tour #8 - Default parameter values
Method's can have default parameter values. try this
class DefaultParams{
Void printMyLang(Str language := "java"){
echo("My current language is $language")
}
Void main(){
printMyLang()//prints "java"
printMyLang("Fantom") //prints "Fantom"
}
}
P.S: If you are from the Java world
- Fantom doesn't support method overloading. Default parameter values takes care of 80% of the cases why we do method overloading. For the rest, you just need to use different method names. This feature really simplifies Reflection
Tour #9 - Easy Reflection
Let's call a method using reflection
class Ref{
Void sayHi(){
echo("Hi")
}
Void main(){
Ref#.method("sayHi").call(this)
}
}
P.S: If you are from the Java world
- Since there no method overloading you don't need to pass a said of types of parameter to uniquely identify a method
- Ref# is equivalent of Ref.class
Tour #10 - Mixins - interfaces with code
Mixins are just like java interfaces, except they can have some method implementations. They cannot have state however.
mixin Audio{
abstract Int getVolume()
Int getHighVolume(){
return getVolume + 10
}
}
class Radio : Audio
{
override Int getVolume(){
return 3
}
Void main(){
echo(this.getHighVolume)
}
}
P.S: If you are from the Java world
- You use ":" to define both "extends" and "implements" (as used in java)
Tour #11 - Closures - Passing code around
Just like data, you can store methods in variables, put them into lists, pass to another function and later execute it if required. Think of it as anonymous inner classes on steroids.
class Closures
{
Void main(){
amethod := |Str name->Str|{
return "You get $name.size points"
}
//At this point the method is not executed
//just stored in a variable called "aMethod"
echo(amethod("kaushik"))
//now it's executed
//with value "kaushik"
}
}
P.S: If you are from the Java world
- closures makes for elegant APIs like this
3.times{echo("hi")}
Here "times" could be a method in Int class, to which you pass a closure, which is called as many times as the value of the Int. Makes sense?
Tour #12 - IO
I've created a file for you in location "files/secret.txt". Try reading it
class Secret
{
Void main(){
f := File(`files/secret.txt`)
echo(f.readAllStr)
}
}
P.S: If you are from the Java world
- If you are familiar with Java's IO, you'll find Fantom's IO refreshingly simple
We're Done
Actually there is much more to fantom that what you saw here. But you got a start. Head to fantom.org to learn more and come back here to execute and check.
I would love your feedback. I am on twitter and mail kaushik.sathupadi@gmail.com