- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Re: pattern in for loop
Fri, 2009-07-31, 05:17
Use multiple parts to the for.
for(x <- xs;
if x.isInstanceOf[Y];
val y = x.asInstanceOf...
-------------------------------------
Ronald Steinhau wrote:
I have a model with a class "DFeature" and two inheriting classes
"DAttr" and "DRef" of feature. Furthermore a class "DClass" with an
association 1:N to features. Now i want to use patterns in for loops, like
for (attr:DAttr <- cls.getFeatures) ...
with cls being of type "DClass", but scala won't compile. I hoped, that
"attr:DAttr" would be treated as a pattern with a variable and a class
type (like in regular type pattern matches) and would only yield the
proper elements of the list. I know, that the condition "if
(attr.isInstanceOf[DAttr])" in the for part would do it, too, but it is
longer and I need a cast to DAttr afterwards, too. Is there a reason,
that this pattern is not recognized/allowed here? Am I missing something?
Ronald Steinhau
Fri, 2009-07-31, 17:47
#2
Re: pattern in for loop
"val" has been deprecated in for comprehensions, I think. Not the concept, just using the keyword.
On Fri, Jul 31, 2009 at 1:17 AM, Naftoli Gugenheim <naftoligug@gmail.com> wrote:
--
Daniel C. Sobral
Something I learned in academia: there are three kinds of academic reviews: review by name, review by reference and review by value.
On Fri, Jul 31, 2009 at 1:17 AM, Naftoli Gugenheim <naftoligug@gmail.com> wrote:
Use multiple parts to the for.
for(x <- xs;
if x.isInstanceOf[Y];
val y = x.asInstanceOf...
-------------------------------------
Ronald Steinhau<st@entimo.de> wrote:
I have a model with a class "DFeature" and two inheriting classes
"DAttr" and "DRef" of feature. Furthermore a class "DClass" with an
association 1:N to features. Now i want to use patterns in for loops, like
for (attr:DAttr <- cls.getFeatures) ...
with cls being of type "DClass", but scala won't compile. I hoped, that
"attr:DAttr" would be treated as a pattern with a variable and a class
type (like in regular type pattern matches) and would only yield the
proper elements of the list. I know, that the condition "if
(attr.isInstanceOf[DAttr])" in the for part would do it, too, but it is
longer and I need a cast to DAttr afterwards, too. Is there a reason,
that this pattern is not recognized/allowed here? Am I missing something?
Ronald Steinhau
--
Ronald Steinhau, Entimo AG
Stralauer Platz 33-34 | 10243 Berlin | Germany
Tel +49(0)30 520 024 122
Fax +49(0)30 520 024 101
Mob +49(0) 151 14 84 49 90
mailto:st@entimo.com | http://www.entimo.com
Vorstand: Jürgen Spieler (Vors.), Erika Tannenbaum , Marianne Neumann
Aufsichtratsvorsitzender: Klaus Kögler
Sitz der Gesellschaft: Berlin, Germany | Handelsregister: HRB Berlin-Charlottenburg 85073
--
Daniel C. Sobral
Something I learned in academia: there are three kinds of academic reviews: review by name, review by reference and review by value.
Sorry, I missed the end... :)
-------------------------------------
Ronald Steinhau wrote:
I have a model with a class "DFeature" and two inheriting classes
"DAttr" and "DRef" of feature. Furthermore a class "DClass" with an
association 1:N to features. Now i want to use patterns in for loops, like
for (attr:DAttr <- cls.getFeatures) ...
with cls being of type "DClass", but scala won't compile. I hoped, that
"attr:DAttr" would be treated as a pattern with a variable and a class
type (like in regular type pattern matches) and would only yield the
proper elements of the list. I know, that the condition "if
(attr.isInstanceOf[DAttr])" in the for part would do it, too, but it is
longer and I need a cast to DAttr afterwards, too. Is there a reason,
that this pattern is not recognized/allowed here? Am I missing something?
Ronald Steinhau