No, members must be known at compile time so that access to members can be checked by the compiler.
One option is to use Option:
class c(a: Int, b: Int){ val variable = if (a==1) Some(a) else None def function() = if (a==1) Some(f(b)) else None }
You can use variable.getOrElse(default) to extract variable from an instance of c.
Another option is to use a class hierarchy
trait C class B() extends C class A(a: Int, b: Int) extends C { val variable: Int = a def function: Int = b } object C { def apply(a: Int, b: Int): C = if (a == 1) { new A(a, b) } else { new B() } }
You create the instance with C(...,...) and when you need to access variable you use match to check that you have an instance of A rather than B.
c, inside that function how would I know that I can (or can't) use eithervariableorfunction? I expect that explains why this won't be useful. - Now, why do you want this? What is your use case? What is the bigger picture? Probably there are ways to model what you want to express.class Cis a compile-time property. The value ofa(1, -2, 37, whatever) is a run-time property. "...and never the twain shall meet" -Kipling.