|
View:
New views
14 Messages
—
Rating Filter:
Alert me
|
| < Prev | 1 - 2 | Next > |
|
|
Re: Reassignment to ValImmutable values can't be changed, ever, this is by design.
It might first look like you cant doing a lot of things, but to see how this idea works it helps to think about the way that numbers behave. Take the expression "1 + 2": This takes 1, adds 2, and returns a NEW number, 3 In this case, neither 1 nor 2 have changed, you can use them in other parts of your program and they will mean the same thing they always did. The same idea can be used with lists: val x = List(2, 3, 4) val y = 1 :: x y is now a new list containing 1, 2, 3, 4, the meaning of x is unchanged (note: it's always better to work at the head of lists for reasons of performance, but that's a different discussion) I can also say val z = y map (n => 2 * n) This now gives me ANOTHER new list, containing 2, 4, 6, 8, once again x and y are unchanged Thinking like this is one of the core ideas behind functional programming. If you have good reason to work differently and you want to have a list x that can change but still be called x, then you have two options: You can use a MutableList, where x is still a val (as it always point to the same, mutable, list), but the contents of that list can now be modified. Or you can use a var, where x points to an immutable list, but you can exchange that list for another one at any time. the only real difference is that you can supply a mutable list to a function, and have the function update it for you. However, it's better design use an immutable list for the function argument, and have it return another NEW immutable list which has the modifications in. On Tue, Jul 7, 2009 at 5:18 AM, Naftoli Gugenhem <naftoligug@...> wrote: You always have two choices: either those functions can return relevant information, or they can modify a var that the function and its caller have access to. The first way I believe is more FP, the second more OOP. |
|
|
Re: Reassignment to ValI have tried this method, but now I get an error when I try to use a List method on Botin.botin
this is the code : var tempB = Botin.botin.filter(n => n.individu != tempNom); The error I get is : value filter is not a member of object List. If i understand correctly this means that "filter" is not recognized as neither a parameter of botin nor a function that can be applied to a List, which is odd to me since "filter" is a function that can be applied to a List no ? p.s.: I'm using netbeans at the moment and when I type "Botin.botin." it then shows the possible function calls I can make, I recognize a few from the Scala Library for List such as "apply" but "filter" and a few others are missing.
|
|
|
Re: Reassignment to ValWe'll need to see some more code (especially the Botin object definition you used). Also what version of scala are you using?
On Thu, Jul 9, 2009 at 4:45 PM, Joob <travaildereligion@...> wrote:
|
|
|
Re: Reassignment to ValAre you sure you have a Scala List, not a Java one?
On Thu, Jul 9, 2009 at 12:45 PM, Joob <travaildereligion@...> 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. |
|
|
Re: Reassignment to ValThis is how I declared my Botin object
object Botin { var botin = List } the version is Scala 2.7.3 (Default) and also, I thought of this recently, how do I declare the botin list as a list that contains "Adresses", "Adresses" being a class that I made just for storing a couple of variables together, because when I tried something like var tempAdr = new Adresse(tempNom, tempNum, tempRue, tempVille); var tempAdreListe = List(tempAdr) Botin.botin = tempAdreListe I got an error saying type mismatch : found : List[tp2scala.Main.Adresse] required : object List I tried the following syntax : object Botin { var botin = List[tp2scala.Main.Adresse] } but I get an error saying: missing arguments for method apply in object List; follow this method with `_' if you want to treat it as a partially applied function
|
|
|
|
|
|
Re: Reassignment to Valhmm
How do I check that :o ? p.s.: I am prepared for various insults and bashing :)
|
|
|
Re: Reassignment to ValIf you use "=", you are assigning a value to the var. If you used ":", then you would be declaring a type (though you'd have to assign a value too). Try this instead:
var botin = List.empty[tp2scala.Main.Adresse]
Or whatever other type you think it's appropriate for botin.
On Thu, Jul 9, 2009 at 1:35 PM, Joob <travaildereligion@...> 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. |
|
|
Re: Reassignment to ValI get a similar error saying that the value "empty" is not a member of object List
|
|
|
Re: Reassignment to ValI meant to do that instead of "var botin = List". If you did change that, then please post the error message and relevant line.
On Thu, Jul 9, 2009 at 1:49 PM, Joob <travaildereligion@...> 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. |
|
|
Re: Reassignment to Val object Botin {
var botin = List.empty[tp2scala.Main.Adresse] <--- Error Line } error msg : value empty is not a member of object List
|
|
|
Re: Reassignment to ValMmmm. You have "List" defined somewhere as something other than the class. Ok, replace that with:
var botin = _root_.scala.List.empty[tp2scala.Main.Adresse] On Thu, Jul 9, 2009 at 1:56 PM, Joob <travaildereligion@...> 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. |
|
|
Re: Reassignment to ValOn Thursday July 9 2009, Daniel Sobral wrote:
> If you use "=", you are assigning a value to the var. If you used > ":", then you would be declaring a type (though you'd have to assign > a value too). Try this instead: > > var botin = List.empty[tp2scala.Main.Adresse] It's probably a good idea to check if you're referring to 2.8-specific classes or methods. The "empty" method in object List is a 2.8 thing. > Or whatever other type you think it's appropriate for botin. Randall Schulz |
|
|
Re: Reassignment to ValI usually do, but it so happens that this time I didn't. I apologize, Joob. Use one of the following instead:
var botin = List[tp2scala.Main.Adresse]()
or
var botin = Nil[tp2scala.main.Adresse]
On Thu, Jul 9, 2009 at 3:03 PM, Randall R Schulz <rschulz@...> 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. |
| < Prev | 1 - 2 | Next > |
| Free embeddable forum powered by Nabble | Forum Help |