forked from ankitsindhu/scala-programming
-
Notifications
You must be signed in to change notification settings - Fork 0
/
3. Unified Types.scala
48 lines (30 loc) · 1.01 KB
/
3. Unified Types.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// Databricks notebook source
val list: List[Any] = List(
"a string",
100,
'x',
true,
() => "Junk Function"
)
// COMMAND ----------
// f = x => println(x) -> x is a function that takes one argument & prints it
def fun(x:Any): Unit = println(x)
//list.foreach(x => println(x))
list.foreach(fun)
// COMMAND ----------
val funclist: List[()=>String] = List( ()=>"Hello", ()=>"World", ()=>"Again")
// COMMAND ----------
funclist.foreach(x=> println(x()))
// COMMAND ----------
val funclist: List[Any] = List( ()=>"Hello", ()=>"World", ()=>"Again")
// COMMAND ----------
//Call function corresponding to the obect for 0th element
val f = funclist(0)
// COMMAND ----------
val g:Any = 9
//println(g + g) g is of type Any, + operator is not defined for Any
val f = g.asInstanceOf[Integer]
// COMMAND ----------
// funclist.foreach(x => println(x())) - this will not work because each x is not a function rather an object of type Any
funclist.foreach(x => println( x.asInstanceOf[()=>String]()))
// COMMAND ----------