Showing posts with label learning. Show all posts
Showing posts with label learning. Show all posts

18 September 2013

Scala Notes 8: keyword list ( reserved words )

Information:
List of  keywords (reserved words/keywords) based on  "The Scala Language Specification v 2.9 (DRAFT) from 24.5.2011)".
Definition based on:


Info  for Java folks.
There is no break* , continue , public keywords in Scala.


List of  keywords ( reserved words )
abstract - It makes a declaration abstract. It is not required for abstract members for most cases( I found case,where you need do that: http://stackoverflow.com/questions/2038370/scala-traits-and-abstract-methods-override ),but i don't why.

case - It starts clause for matched expression.

catch - It starts a clause for thrown exception.

class - It starts a class declaration.

def  - It starts a method declaration. 

do - it starts do-while loop.

else - it starts else clause for an if clause (when evaluation of if is false)

extends - it indicates  inheritance. that class or trait is   declared as child of the parent type of the class or trait.

false  -  one of Boolean's value.

final  - It addes "prohibit" attribute  .For class it means   prohibit inheritance and for method  prohibit   overriding.

finally - it starts clause after try/catch blocks (whatever exception is thrown or not).

for  - It starts for loop.

forSome -  According to O'reilly book( as I do not come across yet) :  Used in existential type declarations to constrain the allowed concrete types that can be used. 

if  - it starts if clause.

implicit - Marks a method as eligible to be used as an implicit type converter. Marks a method parameter as optional, as long as a type-compatible substitute object is in the scope where the method is called. 

import - it imports member(s) into the current scope. 


lazy - According to O'reilly book( as I do not come across yet) :   Defer evaluation of a val.

match - it starts pattern matching clause.

new - It creates a new instance of a class.

null  - It exists for compatibility and interoperability with Java and .NET libraries only. It means variable that has not been assigned a value.

object  - it starts signleton version of class (One instance only)

override - it indicates change of original member of class (like function etc. ) for non final members(See final for more details)

package - it starts declaration of package (scope)

private - it describes visibility of a declaration.

protected  - it describes visibility of a declaration. 

return -  a return from a function.

sealed - Applied to a parent class to require all directly derived classes to be declared in the same source file.(I do not come across this yet)


super  - An object refers to parent of itself.  (it is the same as this,but it binds into parent of a object 

this - An object refers to itself.

throw  - It indicates what type  exception  can be a thrown, if things went wrong.

trait - A mixin module that adds additional state and behavior to an instance of a class.

try - It starts scope of block of code that may throw an exception.

true  - one of Boolean's value.

type - It starts a type declaration.

val (value) - It starts declaration of  read-only variable.

var  (variable) -  It starts declaration of  read/write variable.

while - It starts a while loop (or starts while section of do/while loop). 

with - keyword is similar to Java’s implements keyword for interfaces. In Scala it includes the trait in class or object.

yield  -  is a result ( as collection ) from for loop (see http://pastorcmentarny.blogspot.co.uk/2013/06/scala-notes-10-yield-for-yield.html)

@  - It starts an annotation.

_  - it has many functions (it used in  many cases  like function literals , import ,place holder and etc.

:   - Separator between identifiers and type annotations. 

= - is an assignment 

<- - is used in for comprehensions in generator expressions. 

- is a unicode representation for <-.  [Unicode:  \u2190]


<: - It is used in abstract and  parametrized  type declarations ( It is used to constrain the allowed types).

<%  - It is used in abstract and   parametrized  type “view bounds” declarations. 


=> - It is used in function literals to separate the argument list from the function body. 

  is a unicode representation for =>.  [Unicode: \u21D2] 


>:  - It is used in abstract and  parametrized  type declarations ( It is used to constrain the allowed types).

# - it used in type projections (I do not come across yet)


List of  deprecated keywords ( reserved words )
requires - it was used for self-typing. (I didn't find why)


* break  appear in  Scala version 2.8  but it is implemented as a library method not as built-in break keyword.

14 June 2013

Scala Notes 11: Enumerations

Information:
Scala notes is series of my "side comments" .
They can be useful for me during revision of  Scala in future use.
They contains bunch of information about interesting bits.
They helps me to understand difference between Java and Scala. 
 They contains an explanation of things that I didn't come across yet or I  attempt to explain things in easy and friendly way of topics and theories that I didn't understand.

Enumerations:

Wikipedia said: "An enumeration of a collection of items is a complete, ordered listing of all of the items in that collection. "

Javadoc said:  "An enum type is a special data type that enables for a variable to be a set of predefined constants "
  • Examples:  days,directions and etc.
  • As they are  constants, the names of an enum type's fields are in uppercase letters.
  • In the Java programming language, you define an enum type by using the enum keyword
So ... How it looks in Scala ?
Scala API defines Enumeration as "a finite set of values specific to the enumeration"


Scala implements Enumeration as a class in its standard library not as a built-in part (like Java).   Scala’s Enumeration class supports the usual set of methods to work with collections.


Resources:
http://www.scala-lang.org/api/current/index.html#scala.Enumeration
http://ofps.oreilly.com/titles/9780596155957/RoundingOutTheEssentials.html
http://docs.oracle.com/javase/tutorial/java/javaOO/enum.html
http://en.wikipedia.org/wiki/Enumerated_type
http://en.wikipedia.org/wiki/Enumeration





11 June 2013

Scala notes 9: left arrow operator definition and example

Information: THIS IS NOT TUTORIAL.
Scala notes is series of my "side comments" that can be useful during learning Scala.
Their contains bunch of information about interesting bits, explanation of things that i didn't come across yet or attempt to explain things which i didn't understand.

<- left-arrow operator is called generator.
It is called a generator, because "it is 'generating individual values' from a collection used in an expression.
for example:
package lab

object LeftArrowGenerator {

def main(args: Array[String]): Unit = {

val zoo = List("Wolf", "Giraffe", "Elepthant", "Gepard", "Lion", "Owl", "Pather")

for (animal <- zoo) {
println(animal)
}

}

}

16 May 2013

Scala notes: part 2: How to print something on screen(console) in Scala

In Scala ,to print out the result with Console.println(…)

Example:
Console.println( "Print this string" )

but you can write :
println("Print this string")


Note to myself: As i keep using accidentaly Java's way to display stuff on screen,so i add this entry to remind myself ;)

15 May 2013

Learning Scala will be fun...

I decided to learn Scala and Play framework 2.x.
On starts.. most scary sentences is:

" Scala uses the underscore to mean different things in different contexts, but you can usually think of it as an unnamed magical wildcard. "
When see "different things in different contexts" or "magical wildcard" ,I know,what it means... TROUBLE as big flexibility come with bigger confusion  and even bigger production of sentence "WTF is going on here!"

As far as I see Scala will be a good fun for experienced developers who used Java  + Functional programing language(C?) + some experience with dynamic language (Groovy)

I will learn based on book Dean Wampler -  Programming Scala (can be found here -> http://ofps.oreilly.com/titles/9780596155957/ ) , practice by writing many lines of code (Plan 10.000 lines) and on the end  I will read  Marius Eriksen  - Effective Scala  (can be found here -> http://twitter.github.io/effectivescala/ )  to improve my general knowledge  .

I will write some blog entries about  interesting , confusing or annoying experience during learning Scala. I will try add code examples ,where possible and add sources.

Wish me a good luck.