This page is no longer maintained — Please continue to the home page at www.scala-lang.org

Re: pattern in for loop

2 replies
Naftoli Gugenheim
Joined: 2008-12-17,
User offline. Last seen 42 years 45 weeks ago.

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

Naftoli Gugenheim
Joined: 2008-12-17,
User offline. Last seen 42 years 45 weeks ago.
Re: pattern in for loop

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

dcsobral
Joined: 2009-04-23,
User offline. Last seen 38 weeks 5 days ago.
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:
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.

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland