Scala Class that containing List
I have a very basic and simple Scala question. For example, I have a java class like that
class Dataset{
private List<Record> records;
Dataset(){
records = new ArrayList<Record>()
}
public void addItem(Record r){
records.add(r)
}
}
When I try to write same class in Scala, I encoutered with some error:
class RecordSet() {
private var dataset:List[Record]
def this(){
dataset = new List[Record]
}
def addRecord(rd: Record)={
dataset :+ rd
}
}
I cannot declare a List variable like ( private var dataset:List[Record])
and cannot write a default constructor.
scala list class
|
show 1 more comment
I have a very basic and simple Scala question. For example, I have a java class like that
class Dataset{
private List<Record> records;
Dataset(){
records = new ArrayList<Record>()
}
public void addItem(Record r){
records.add(r)
}
}
When I try to write same class in Scala, I encoutered with some error:
class RecordSet() {
private var dataset:List[Record]
def this(){
dataset = new List[Record]
}
def addRecord(rd: Record)={
dataset :+ rd
}
}
I cannot declare a List variable like ( private var dataset:List[Record])
and cannot write a default constructor.
scala list class
2
"I cannot declare" / "I cannot write" is not a precise enough error description for us to help you. What doesn't work? How doesn't it work? What trouble do you have with your code? Do you get an error message? What is the error message? Is the result you are getting not the result you are expecting? What result do you expect and why, what is the result you are getting and how do the two differ? Is the behavior you are observing not the desired behavior? What is the desired behavior and why, what is the observed behavior, and in what way do they differ?
– Jörg W Mittag
Nov 24 '18 at 10:57
Can you provide a precise specification of what it is that you want to happen, including any and all rules, exceptions from those rules, corner cases, special cases, boundary cases, and edge cases? Can you provide sample inputs and outputs demonstrating what you expect to happen, both in normal cases, and in all the exceptions, corner cases, special cases, boundary cases, and edge cases? Please, also make sure to provide a Minimal, Complete, and Verifiable example.
– Jörg W Mittag
Nov 24 '18 at 10:57
2
class RecordSet() { private var dataset: List[Record] = Nil; def addRecord(rd: Record)=dataset = dataset ::: List(rd) }
I write code like that and everything is ok
– ugur
Nov 24 '18 at 10:58
"I encoutered with some error" – And what is the error? Where does it occur? What does it say? The error message should tell you what the problem is, and it should provide the exact location of where the error occurs. Since you don't tell us what the error is and where it occurs, there is no way to help you.
– Jörg W Mittag
Nov 24 '18 at 11:00
1
You're not writing what you think you're writing. Try working with something likeclass Dataset(val records: mutable.MutableList[Record])
. And also perhaps try knocking out a few Scala tutorials online...
– Lasf
Nov 24 '18 at 11:02
|
show 1 more comment
I have a very basic and simple Scala question. For example, I have a java class like that
class Dataset{
private List<Record> records;
Dataset(){
records = new ArrayList<Record>()
}
public void addItem(Record r){
records.add(r)
}
}
When I try to write same class in Scala, I encoutered with some error:
class RecordSet() {
private var dataset:List[Record]
def this(){
dataset = new List[Record]
}
def addRecord(rd: Record)={
dataset :+ rd
}
}
I cannot declare a List variable like ( private var dataset:List[Record])
and cannot write a default constructor.
scala list class
I have a very basic and simple Scala question. For example, I have a java class like that
class Dataset{
private List<Record> records;
Dataset(){
records = new ArrayList<Record>()
}
public void addItem(Record r){
records.add(r)
}
}
When I try to write same class in Scala, I encoutered with some error:
class RecordSet() {
private var dataset:List[Record]
def this(){
dataset = new List[Record]
}
def addRecord(rd: Record)={
dataset :+ rd
}
}
I cannot declare a List variable like ( private var dataset:List[Record])
and cannot write a default constructor.
scala list class
scala list class
asked Nov 24 '18 at 10:45
ugurugur
11010
11010
2
"I cannot declare" / "I cannot write" is not a precise enough error description for us to help you. What doesn't work? How doesn't it work? What trouble do you have with your code? Do you get an error message? What is the error message? Is the result you are getting not the result you are expecting? What result do you expect and why, what is the result you are getting and how do the two differ? Is the behavior you are observing not the desired behavior? What is the desired behavior and why, what is the observed behavior, and in what way do they differ?
– Jörg W Mittag
Nov 24 '18 at 10:57
Can you provide a precise specification of what it is that you want to happen, including any and all rules, exceptions from those rules, corner cases, special cases, boundary cases, and edge cases? Can you provide sample inputs and outputs demonstrating what you expect to happen, both in normal cases, and in all the exceptions, corner cases, special cases, boundary cases, and edge cases? Please, also make sure to provide a Minimal, Complete, and Verifiable example.
– Jörg W Mittag
Nov 24 '18 at 10:57
2
class RecordSet() { private var dataset: List[Record] = Nil; def addRecord(rd: Record)=dataset = dataset ::: List(rd) }
I write code like that and everything is ok
– ugur
Nov 24 '18 at 10:58
"I encoutered with some error" – And what is the error? Where does it occur? What does it say? The error message should tell you what the problem is, and it should provide the exact location of where the error occurs. Since you don't tell us what the error is and where it occurs, there is no way to help you.
– Jörg W Mittag
Nov 24 '18 at 11:00
1
You're not writing what you think you're writing. Try working with something likeclass Dataset(val records: mutable.MutableList[Record])
. And also perhaps try knocking out a few Scala tutorials online...
– Lasf
Nov 24 '18 at 11:02
|
show 1 more comment
2
"I cannot declare" / "I cannot write" is not a precise enough error description for us to help you. What doesn't work? How doesn't it work? What trouble do you have with your code? Do you get an error message? What is the error message? Is the result you are getting not the result you are expecting? What result do you expect and why, what is the result you are getting and how do the two differ? Is the behavior you are observing not the desired behavior? What is the desired behavior and why, what is the observed behavior, and in what way do they differ?
– Jörg W Mittag
Nov 24 '18 at 10:57
Can you provide a precise specification of what it is that you want to happen, including any and all rules, exceptions from those rules, corner cases, special cases, boundary cases, and edge cases? Can you provide sample inputs and outputs demonstrating what you expect to happen, both in normal cases, and in all the exceptions, corner cases, special cases, boundary cases, and edge cases? Please, also make sure to provide a Minimal, Complete, and Verifiable example.
– Jörg W Mittag
Nov 24 '18 at 10:57
2
class RecordSet() { private var dataset: List[Record] = Nil; def addRecord(rd: Record)=dataset = dataset ::: List(rd) }
I write code like that and everything is ok
– ugur
Nov 24 '18 at 10:58
"I encoutered with some error" – And what is the error? Where does it occur? What does it say? The error message should tell you what the problem is, and it should provide the exact location of where the error occurs. Since you don't tell us what the error is and where it occurs, there is no way to help you.
– Jörg W Mittag
Nov 24 '18 at 11:00
1
You're not writing what you think you're writing. Try working with something likeclass Dataset(val records: mutable.MutableList[Record])
. And also perhaps try knocking out a few Scala tutorials online...
– Lasf
Nov 24 '18 at 11:02
2
2
"I cannot declare" / "I cannot write" is not a precise enough error description for us to help you. What doesn't work? How doesn't it work? What trouble do you have with your code? Do you get an error message? What is the error message? Is the result you are getting not the result you are expecting? What result do you expect and why, what is the result you are getting and how do the two differ? Is the behavior you are observing not the desired behavior? What is the desired behavior and why, what is the observed behavior, and in what way do they differ?
– Jörg W Mittag
Nov 24 '18 at 10:57
"I cannot declare" / "I cannot write" is not a precise enough error description for us to help you. What doesn't work? How doesn't it work? What trouble do you have with your code? Do you get an error message? What is the error message? Is the result you are getting not the result you are expecting? What result do you expect and why, what is the result you are getting and how do the two differ? Is the behavior you are observing not the desired behavior? What is the desired behavior and why, what is the observed behavior, and in what way do they differ?
– Jörg W Mittag
Nov 24 '18 at 10:57
Can you provide a precise specification of what it is that you want to happen, including any and all rules, exceptions from those rules, corner cases, special cases, boundary cases, and edge cases? Can you provide sample inputs and outputs demonstrating what you expect to happen, both in normal cases, and in all the exceptions, corner cases, special cases, boundary cases, and edge cases? Please, also make sure to provide a Minimal, Complete, and Verifiable example.
– Jörg W Mittag
Nov 24 '18 at 10:57
Can you provide a precise specification of what it is that you want to happen, including any and all rules, exceptions from those rules, corner cases, special cases, boundary cases, and edge cases? Can you provide sample inputs and outputs demonstrating what you expect to happen, both in normal cases, and in all the exceptions, corner cases, special cases, boundary cases, and edge cases? Please, also make sure to provide a Minimal, Complete, and Verifiable example.
– Jörg W Mittag
Nov 24 '18 at 10:57
2
2
class RecordSet() { private var dataset: List[Record] = Nil; def addRecord(rd: Record)=dataset = dataset ::: List(rd) }
I write code like that and everything is ok– ugur
Nov 24 '18 at 10:58
class RecordSet() { private var dataset: List[Record] = Nil; def addRecord(rd: Record)=dataset = dataset ::: List(rd) }
I write code like that and everything is ok– ugur
Nov 24 '18 at 10:58
"I encoutered with some error" – And what is the error? Where does it occur? What does it say? The error message should tell you what the problem is, and it should provide the exact location of where the error occurs. Since you don't tell us what the error is and where it occurs, there is no way to help you.
– Jörg W Mittag
Nov 24 '18 at 11:00
"I encoutered with some error" – And what is the error? Where does it occur? What does it say? The error message should tell you what the problem is, and it should provide the exact location of where the error occurs. Since you don't tell us what the error is and where it occurs, there is no way to help you.
– Jörg W Mittag
Nov 24 '18 at 11:00
1
1
You're not writing what you think you're writing. Try working with something like
class Dataset(val records: mutable.MutableList[Record])
. And also perhaps try knocking out a few Scala tutorials online...– Lasf
Nov 24 '18 at 11:02
You're not writing what you think you're writing. Try working with something like
class Dataset(val records: mutable.MutableList[Record])
. And also perhaps try knocking out a few Scala tutorials online...– Lasf
Nov 24 '18 at 11:02
|
show 1 more comment
2 Answers
2
active
oldest
votes
Here is how you will replicate the Java code you mentioned in your question:
// defining Record so the code below compiles
case class Record()
// Here is the Scala implementation
class RecordSet(private var dataset:List[Record]) {
def addRecord(rd: Record)={
dataset :+ rd
}
}
Some explanation:
In Scala, when you define a class, you have the ability to pass parameter to the class definition. eg: class Foo(num:Int, descr:String)
Scala would automatically use the given parameter to create a primary constructor for you. So you can now instantiate the Foo, like so new Foo(1, "One")
. This is different in Java where you have to explicitly define parameter accepting constructors.
You have to be aware that the parameter passed do not automatically become instance member of the class. Although if you want, you can tell Scala to make them instance member. There are various ways to do this, one way is to prefix the parameter with either var
or val
. For example class Foo(val num:Int, val descr:String)
or class Foo(var num:Int, var descr:String)
. The difference is that with val
, the instance variable are immutable. With var
they are mutable.
Also, by default the instance member Scala will generate would be public. That means they can be accessed directly from an instance of the object. For example:
val foo = new Foo(1, "One")
println(foo.num) // prints 1.
If you want them to be private, you add private keyword to the definition. So that would become:
class Foo(private var num:Int, private var desc:String)
The reason why your code fails to compile is you define a method called this()
which is used to create multiple constructors. (and not to create a constructor that initiates a private field which is your intention judging from the Java code you shared). You can google for multiple constructors or auxiliary constructors to learn more about this.
You should not usevar dataset: List[Record]
, as then you are creating a new list each time you add an element. Instead you should useval
and a mutable collection such asval dataset: mutable.MutableList[Record]
.
– Lasf
Nov 24 '18 at 22:51
dataset :+ rd
will be creating a new List and the current collection won't be changed. Hence the new element will be lost.
– anuj saxena
Nov 25 '18 at 3:16
add a comment |
As dade told the issue in your code is that with
this
keyword you are actually creating anauxilary
constructor which has some limitations likethe first line of your auxilary constructor must be another constructor (auxilary/primary)
. Hence you cannot use such a way to create a class.
- Also you can not write such lines in a scala concrete class
private var dataset:List[Record]
as it is considered as abstract (no definition provided).
- Also you can not write such lines in a scala concrete class
Now with the code. Usually in Scala we don't prefer mutability because it introduces side-effects in our functions (which is not the functional way but as scala is not purely functional you can use mutability too).
In Scala way, the code should be something like this:
class RecordSet(private val dataset:List[Record]) {
def addRecord(rd: Record): RecordSet ={
new RecordSet(dataset :+ rd)
}
}
Now with the above class there is no mutability. Whenever you are adding on an element to the dataset a new instance of RecordSet is being created. Hence no mutability.
However, if you have to use the same class reference in your application use your a mutable collection for your dataset like below:
class RecordSet(private val dataset:ListBuffer[Record]) {
def addRecord(rd: Record): ListBuffer[Record] ={
dataset += rd
}
}
Above code will append the new record in the existing dataset with the same class reference.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53457341%2fscala-class-that-containing-list%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Here is how you will replicate the Java code you mentioned in your question:
// defining Record so the code below compiles
case class Record()
// Here is the Scala implementation
class RecordSet(private var dataset:List[Record]) {
def addRecord(rd: Record)={
dataset :+ rd
}
}
Some explanation:
In Scala, when you define a class, you have the ability to pass parameter to the class definition. eg: class Foo(num:Int, descr:String)
Scala would automatically use the given parameter to create a primary constructor for you. So you can now instantiate the Foo, like so new Foo(1, "One")
. This is different in Java where you have to explicitly define parameter accepting constructors.
You have to be aware that the parameter passed do not automatically become instance member of the class. Although if you want, you can tell Scala to make them instance member. There are various ways to do this, one way is to prefix the parameter with either var
or val
. For example class Foo(val num:Int, val descr:String)
or class Foo(var num:Int, var descr:String)
. The difference is that with val
, the instance variable are immutable. With var
they are mutable.
Also, by default the instance member Scala will generate would be public. That means they can be accessed directly from an instance of the object. For example:
val foo = new Foo(1, "One")
println(foo.num) // prints 1.
If you want them to be private, you add private keyword to the definition. So that would become:
class Foo(private var num:Int, private var desc:String)
The reason why your code fails to compile is you define a method called this()
which is used to create multiple constructors. (and not to create a constructor that initiates a private field which is your intention judging from the Java code you shared). You can google for multiple constructors or auxiliary constructors to learn more about this.
You should not usevar dataset: List[Record]
, as then you are creating a new list each time you add an element. Instead you should useval
and a mutable collection such asval dataset: mutable.MutableList[Record]
.
– Lasf
Nov 24 '18 at 22:51
dataset :+ rd
will be creating a new List and the current collection won't be changed. Hence the new element will be lost.
– anuj saxena
Nov 25 '18 at 3:16
add a comment |
Here is how you will replicate the Java code you mentioned in your question:
// defining Record so the code below compiles
case class Record()
// Here is the Scala implementation
class RecordSet(private var dataset:List[Record]) {
def addRecord(rd: Record)={
dataset :+ rd
}
}
Some explanation:
In Scala, when you define a class, you have the ability to pass parameter to the class definition. eg: class Foo(num:Int, descr:String)
Scala would automatically use the given parameter to create a primary constructor for you. So you can now instantiate the Foo, like so new Foo(1, "One")
. This is different in Java where you have to explicitly define parameter accepting constructors.
You have to be aware that the parameter passed do not automatically become instance member of the class. Although if you want, you can tell Scala to make them instance member. There are various ways to do this, one way is to prefix the parameter with either var
or val
. For example class Foo(val num:Int, val descr:String)
or class Foo(var num:Int, var descr:String)
. The difference is that with val
, the instance variable are immutable. With var
they are mutable.
Also, by default the instance member Scala will generate would be public. That means they can be accessed directly from an instance of the object. For example:
val foo = new Foo(1, "One")
println(foo.num) // prints 1.
If you want them to be private, you add private keyword to the definition. So that would become:
class Foo(private var num:Int, private var desc:String)
The reason why your code fails to compile is you define a method called this()
which is used to create multiple constructors. (and not to create a constructor that initiates a private field which is your intention judging from the Java code you shared). You can google for multiple constructors or auxiliary constructors to learn more about this.
You should not usevar dataset: List[Record]
, as then you are creating a new list each time you add an element. Instead you should useval
and a mutable collection such asval dataset: mutable.MutableList[Record]
.
– Lasf
Nov 24 '18 at 22:51
dataset :+ rd
will be creating a new List and the current collection won't be changed. Hence the new element will be lost.
– anuj saxena
Nov 25 '18 at 3:16
add a comment |
Here is how you will replicate the Java code you mentioned in your question:
// defining Record so the code below compiles
case class Record()
// Here is the Scala implementation
class RecordSet(private var dataset:List[Record]) {
def addRecord(rd: Record)={
dataset :+ rd
}
}
Some explanation:
In Scala, when you define a class, you have the ability to pass parameter to the class definition. eg: class Foo(num:Int, descr:String)
Scala would automatically use the given parameter to create a primary constructor for you. So you can now instantiate the Foo, like so new Foo(1, "One")
. This is different in Java where you have to explicitly define parameter accepting constructors.
You have to be aware that the parameter passed do not automatically become instance member of the class. Although if you want, you can tell Scala to make them instance member. There are various ways to do this, one way is to prefix the parameter with either var
or val
. For example class Foo(val num:Int, val descr:String)
or class Foo(var num:Int, var descr:String)
. The difference is that with val
, the instance variable are immutable. With var
they are mutable.
Also, by default the instance member Scala will generate would be public. That means they can be accessed directly from an instance of the object. For example:
val foo = new Foo(1, "One")
println(foo.num) // prints 1.
If you want them to be private, you add private keyword to the definition. So that would become:
class Foo(private var num:Int, private var desc:String)
The reason why your code fails to compile is you define a method called this()
which is used to create multiple constructors. (and not to create a constructor that initiates a private field which is your intention judging from the Java code you shared). You can google for multiple constructors or auxiliary constructors to learn more about this.
Here is how you will replicate the Java code you mentioned in your question:
// defining Record so the code below compiles
case class Record()
// Here is the Scala implementation
class RecordSet(private var dataset:List[Record]) {
def addRecord(rd: Record)={
dataset :+ rd
}
}
Some explanation:
In Scala, when you define a class, you have the ability to pass parameter to the class definition. eg: class Foo(num:Int, descr:String)
Scala would automatically use the given parameter to create a primary constructor for you. So you can now instantiate the Foo, like so new Foo(1, "One")
. This is different in Java where you have to explicitly define parameter accepting constructors.
You have to be aware that the parameter passed do not automatically become instance member of the class. Although if you want, you can tell Scala to make them instance member. There are various ways to do this, one way is to prefix the parameter with either var
or val
. For example class Foo(val num:Int, val descr:String)
or class Foo(var num:Int, var descr:String)
. The difference is that with val
, the instance variable are immutable. With var
they are mutable.
Also, by default the instance member Scala will generate would be public. That means they can be accessed directly from an instance of the object. For example:
val foo = new Foo(1, "One")
println(foo.num) // prints 1.
If you want them to be private, you add private keyword to the definition. So that would become:
class Foo(private var num:Int, private var desc:String)
The reason why your code fails to compile is you define a method called this()
which is used to create multiple constructors. (and not to create a constructor that initiates a private field which is your intention judging from the Java code you shared). You can google for multiple constructors or auxiliary constructors to learn more about this.
edited Nov 24 '18 at 15:48
answered Nov 24 '18 at 12:17
dadedade
1,45231932
1,45231932
You should not usevar dataset: List[Record]
, as then you are creating a new list each time you add an element. Instead you should useval
and a mutable collection such asval dataset: mutable.MutableList[Record]
.
– Lasf
Nov 24 '18 at 22:51
dataset :+ rd
will be creating a new List and the current collection won't be changed. Hence the new element will be lost.
– anuj saxena
Nov 25 '18 at 3:16
add a comment |
You should not usevar dataset: List[Record]
, as then you are creating a new list each time you add an element. Instead you should useval
and a mutable collection such asval dataset: mutable.MutableList[Record]
.
– Lasf
Nov 24 '18 at 22:51
dataset :+ rd
will be creating a new List and the current collection won't be changed. Hence the new element will be lost.
– anuj saxena
Nov 25 '18 at 3:16
You should not use
var dataset: List[Record]
, as then you are creating a new list each time you add an element. Instead you should use val
and a mutable collection such as val dataset: mutable.MutableList[Record]
.– Lasf
Nov 24 '18 at 22:51
You should not use
var dataset: List[Record]
, as then you are creating a new list each time you add an element. Instead you should use val
and a mutable collection such as val dataset: mutable.MutableList[Record]
.– Lasf
Nov 24 '18 at 22:51
dataset :+ rd
will be creating a new List and the current collection won't be changed. Hence the new element will be lost.– anuj saxena
Nov 25 '18 at 3:16
dataset :+ rd
will be creating a new List and the current collection won't be changed. Hence the new element will be lost.– anuj saxena
Nov 25 '18 at 3:16
add a comment |
As dade told the issue in your code is that with
this
keyword you are actually creating anauxilary
constructor which has some limitations likethe first line of your auxilary constructor must be another constructor (auxilary/primary)
. Hence you cannot use such a way to create a class.
- Also you can not write such lines in a scala concrete class
private var dataset:List[Record]
as it is considered as abstract (no definition provided).
- Also you can not write such lines in a scala concrete class
Now with the code. Usually in Scala we don't prefer mutability because it introduces side-effects in our functions (which is not the functional way but as scala is not purely functional you can use mutability too).
In Scala way, the code should be something like this:
class RecordSet(private val dataset:List[Record]) {
def addRecord(rd: Record): RecordSet ={
new RecordSet(dataset :+ rd)
}
}
Now with the above class there is no mutability. Whenever you are adding on an element to the dataset a new instance of RecordSet is being created. Hence no mutability.
However, if you have to use the same class reference in your application use your a mutable collection for your dataset like below:
class RecordSet(private val dataset:ListBuffer[Record]) {
def addRecord(rd: Record): ListBuffer[Record] ={
dataset += rd
}
}
Above code will append the new record in the existing dataset with the same class reference.
add a comment |
As dade told the issue in your code is that with
this
keyword you are actually creating anauxilary
constructor which has some limitations likethe first line of your auxilary constructor must be another constructor (auxilary/primary)
. Hence you cannot use such a way to create a class.
- Also you can not write such lines in a scala concrete class
private var dataset:List[Record]
as it is considered as abstract (no definition provided).
- Also you can not write such lines in a scala concrete class
Now with the code. Usually in Scala we don't prefer mutability because it introduces side-effects in our functions (which is not the functional way but as scala is not purely functional you can use mutability too).
In Scala way, the code should be something like this:
class RecordSet(private val dataset:List[Record]) {
def addRecord(rd: Record): RecordSet ={
new RecordSet(dataset :+ rd)
}
}
Now with the above class there is no mutability. Whenever you are adding on an element to the dataset a new instance of RecordSet is being created. Hence no mutability.
However, if you have to use the same class reference in your application use your a mutable collection for your dataset like below:
class RecordSet(private val dataset:ListBuffer[Record]) {
def addRecord(rd: Record): ListBuffer[Record] ={
dataset += rd
}
}
Above code will append the new record in the existing dataset with the same class reference.
add a comment |
As dade told the issue in your code is that with
this
keyword you are actually creating anauxilary
constructor which has some limitations likethe first line of your auxilary constructor must be another constructor (auxilary/primary)
. Hence you cannot use such a way to create a class.
- Also you can not write such lines in a scala concrete class
private var dataset:List[Record]
as it is considered as abstract (no definition provided).
- Also you can not write such lines in a scala concrete class
Now with the code. Usually in Scala we don't prefer mutability because it introduces side-effects in our functions (which is not the functional way but as scala is not purely functional you can use mutability too).
In Scala way, the code should be something like this:
class RecordSet(private val dataset:List[Record]) {
def addRecord(rd: Record): RecordSet ={
new RecordSet(dataset :+ rd)
}
}
Now with the above class there is no mutability. Whenever you are adding on an element to the dataset a new instance of RecordSet is being created. Hence no mutability.
However, if you have to use the same class reference in your application use your a mutable collection for your dataset like below:
class RecordSet(private val dataset:ListBuffer[Record]) {
def addRecord(rd: Record): ListBuffer[Record] ={
dataset += rd
}
}
Above code will append the new record in the existing dataset with the same class reference.
As dade told the issue in your code is that with
this
keyword you are actually creating anauxilary
constructor which has some limitations likethe first line of your auxilary constructor must be another constructor (auxilary/primary)
. Hence you cannot use such a way to create a class.
- Also you can not write such lines in a scala concrete class
private var dataset:List[Record]
as it is considered as abstract (no definition provided).
- Also you can not write such lines in a scala concrete class
Now with the code. Usually in Scala we don't prefer mutability because it introduces side-effects in our functions (which is not the functional way but as scala is not purely functional you can use mutability too).
In Scala way, the code should be something like this:
class RecordSet(private val dataset:List[Record]) {
def addRecord(rd: Record): RecordSet ={
new RecordSet(dataset :+ rd)
}
}
Now with the above class there is no mutability. Whenever you are adding on an element to the dataset a new instance of RecordSet is being created. Hence no mutability.
However, if you have to use the same class reference in your application use your a mutable collection for your dataset like below:
class RecordSet(private val dataset:ListBuffer[Record]) {
def addRecord(rd: Record): ListBuffer[Record] ={
dataset += rd
}
}
Above code will append the new record in the existing dataset with the same class reference.
answered Nov 25 '18 at 3:14
anuj saxenaanuj saxena
24917
24917
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53457341%2fscala-class-that-containing-list%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
"I cannot declare" / "I cannot write" is not a precise enough error description for us to help you. What doesn't work? How doesn't it work? What trouble do you have with your code? Do you get an error message? What is the error message? Is the result you are getting not the result you are expecting? What result do you expect and why, what is the result you are getting and how do the two differ? Is the behavior you are observing not the desired behavior? What is the desired behavior and why, what is the observed behavior, and in what way do they differ?
– Jörg W Mittag
Nov 24 '18 at 10:57
Can you provide a precise specification of what it is that you want to happen, including any and all rules, exceptions from those rules, corner cases, special cases, boundary cases, and edge cases? Can you provide sample inputs and outputs demonstrating what you expect to happen, both in normal cases, and in all the exceptions, corner cases, special cases, boundary cases, and edge cases? Please, also make sure to provide a Minimal, Complete, and Verifiable example.
– Jörg W Mittag
Nov 24 '18 at 10:57
2
class RecordSet() { private var dataset: List[Record] = Nil; def addRecord(rd: Record)=dataset = dataset ::: List(rd) }
I write code like that and everything is ok– ugur
Nov 24 '18 at 10:58
"I encoutered with some error" – And what is the error? Where does it occur? What does it say? The error message should tell you what the problem is, and it should provide the exact location of where the error occurs. Since you don't tell us what the error is and where it occurs, there is no way to help you.
– Jörg W Mittag
Nov 24 '18 at 11:00
1
You're not writing what you think you're writing. Try working with something like
class Dataset(val records: mutable.MutableList[Record])
. And also perhaps try knocking out a few Scala tutorials online...– Lasf
Nov 24 '18 at 11:02