Reputation: 189
//attempting to call methods in different class
val t1 = test
t1.func1()
t1.func1(df).func2("test")
object test {
def func1 (df: dataframe): tt = {
def func2 (ab: String): String = {
// do something
} else {
// do nothing
}
def func3 (ab2: String): String = {
//do something
} else {
// do nothing
}
}
val t1 = test
t1.func1()
t1.func1(df).func2("test")
I'm expecting to access func2 without any errors
Upvotes: 0
Views: 1910
Reputation: 39
The closure property prevents captured identifiers to be accessed from outside the enclosed scope. Using an object or class instead of a function and overloading apply appropriately would make the inner scope accessible.
object test {
case class func1(df: dataframe){
def apply(..)= {..}
def func2 (ab: String): String = {..}
def func3 (ab2: String): String = {..}
}
}
Upvotes: 3
Reputation: 930
You can consider func2
to be the equivalent of a nested variable. You aren't able to access this outside of its parent function.
Try changing a few things around to make this example more obvious:
object test {
def func1 (df: dataframe): String = {
val data = "hi"
"bye"
}
}
val t1 = test
t1.func1(df).data
Would you expect to be able to access data
here?
If you try to make a reference to func2
outside of func1
, you will receive a compiler error.
If you need access to func2
and don't have any other constraints, I'd recommend defining it outside the scope of func1
.
Upvotes: 4