When I right-click on a return value in the IntelliJ debugger and choose "Evaluate Expression", nothing happens.
I then have to switch to another stack frame and back again to get Evaluate Expression to work again.
This is with Scala code.
How can I evaluate an expression that refers to the return value? The return value's name in the debugger is not a legal identifier.
It's not clear what you mean by clicking "on a return value" in the debugger.
If you have a breakpoint set on a particular line, then the execution of that thread stops before the line is executed. But Scala does not assign the return value of a function or block to any particular name, so it's not clear what "the return value's name in the debugger" refers to.
If you are using the recommended implicit-return style,
def repeatUppercase(base: String, count: Int = 3): String = {
val baseUpper = base.toUpperCase
val parts = List.fill(count)(baseUpper)
parts.mkString
}
and you put a breakpoint on the last line of the block, then your debugger will show the names base, count, baseUpper, and parts, along with their values, when it stops there. If you paste the expression parts.mkString into the evaluation window, it will evaluate the expression. If you then step forward in the debugger, the return value will be assigned to whatever name it was assigned to in the calling scope (unless the calling scope discards the return value or uses it anonymously).
On the other hand, if you use explicit-return style,
def repeatUppercase(base: String, count: Int = 3): String = {
val baseUpper = base.toUpperCase
val parts = List.fill(count)(baseUpper)
return parts.mkString
}
then you cannot evaluate the statement return parts.mkString in the expression evaluator because a statement is not an expression! (Also a return statement is syntactically invalid outside a function definition.)
Related
I know that in the latest version of dart we can use if else statements inside the build method. Does anyone know if we can use also if else statement when we setting class parameters? I know I can do inline statement there but inline is a bit hard to read when there are multiple conditions
const int i = 0;
class Person {
// NewClass n = NewClass(a: i == 0 ? 'a' : 'b'); //<- inline statement working
NewClass n = NewClass(a: if(i == 0) 'a' else 'b'); //<- if statement doesn't
}
class NewClass {
final String a;
const NewClass({this.a});
}
Edit:
Basically in my case I've got an TextField widget where I set its's type parameter from enum (Type.text, Type.numeric...) According to this parameter I want to set The textField parameters (textCapitalization, maxLength and so on)
As per your comment, you are already creating an enum for specifying the type of the fields.
enum Type {text, numeric}
Now for specifying the properties of that particular type, you can add an extension on this enum, as shown below:
extension TextFieldProperties on Type {
int get maxLength {
if (this == Type.text) {
return 10;
}
return 12;
}
}
So in your field class you already have a type defined, you can use that type variable to get the properties of that particular type of field.
Type type = Type.text;
print(type.maxLength); // Will print 10
type = Type.numeric;
print(type.maxLength); // Will print 12
Note: It will work only in Dart 2.7 and above
You want the conditional expression (?:), not the conditional statement or literal entry (if), as you have already discovered.
The reason if doesn't work is that if only works as a statement or as a collection literal entry. It doesn't work in arbitrary expressions.
The reason for the distinction is that the if syntax allows you to omit the else branch. That only makes sense in places where "nothing" is a valid alternative. For a statement, "doing nothing" is fine. For a collection, "adding nothing" is also fine.
In an expression context, you must evaluate to a value or throw. There is no reasonable default that we can use instead of "nothing", so an if is not allowed instead of an expression.
Doesn't work because this syntax doesn't exist in Dart. The only way to do what you would like to do is to use the ternary operator.
If you try it in the DartPad you will get an error.
I suggest you to use a function to return the right value.
james=open('C:/Users/skora da_bura/Documents/data.txt')
jake=james.read()
james.close()
numblist=[]
charlist=[]
def Read(numblist,charlist):
for i in range(0,len(jake),4):
numblist.append(int(jake[i]))
for i in range(2,len(jake),4):
charlist.append(jake[i])
Bring = numblist,charlist
james = open('C:/Users/skora da_bura/Documents/data.txt')
jake22 = james.readlines()
james.close()
back='Number of lines read',len(jake22)
return back
print(Read([],[]))
print(charlist)
the charlist returns [] even though I had appended values to it to make a list when I was defining the function Read.
I don't seem to see what the problem is with the code
The charlist you define in the signature of Read shadows the global charlist. They're different variables that happen to have the same name. If you intend to modify the global variable, you shouldn't try to pass it as a parameter.
I need help in understanding that algorithm:
[[Call]]
When the [[Call]] internal method for a Function object F is called with a this value and a list of arguments, the following steps are taken:
Let funcCtx be the result of establishing a new execution context for function code using the value of F's [[FormalParameters]] internal property, the passed arguments List args, and the this value as described in 10.4.3.
Let result be the result of evaluating the FunctionBody that is the value of F's [[Code]] internal property. If F does not have a [[Code]] internal property or if its value is an empty FunctionBody, then result is (normal, undefined, empty).
Exit the execution context funcCtx, restoring the previous execution context.
If result.type is throw then throw result.value.
If result.type is return then return result.value.
Otherwise result.type must be normal. Return undefined.
https://www.ecma-international.org/ecma-262/5.1/#sec-13.2.1
Exactly I need in explanation in 2,4,5,6 clauses.
About 2: First, what does the result of the FunctionBody calculation mean? How is it calculated? What does it mean there is no [[Code]] property when this happens? And most importantly, what does this record mean (normal, undefined, empty).
About 4,5,6: What does result.type mean, result.value? Where does this value come from? Explain for each point
P.S If you vote down, explain why you do that!
First, the [[Call]] is propabily about Function.prototype.call(), so it's better to understand call() first because it's easier than algorithm.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/call
I get this sample code from the MDN, and try to answer the question roughly. (If you want to understand in detail, you and I have to understand the whole specification, but I don't!)
function greet() {
var reply = [this.person, 'Is An Awesome', this.role].join(' ');
console.log(reply);
}
var obj = {
person: 'Douglas Crockford', role: 'Javascript Developer'
};
greet.call(obj); // Douglas Crockford Is An Awesome Javascript Developer
About 2
First, what does the result of the FunctionBody calculation mean?
FunctionBody is function's code. From the sample, FunctionBody is below.
var reply = [this.person, 'Is An Awesome', this.role].join(' ');
console.log(reply);
the result of the FunctionBody calculation is the result of the code execution. The result is expressed by Completion Specification Type(explain below). It includes the function return value, but has more wider information.
What does it mean there is no [[Code]] property when this happens?
It's mean empty function as below.
function greet() {
// empty
}
And most importantly, what does this record mean (normal, undefined, empty).
It's Completion Specification Type. This is an expression for value or statement in specification.
Values of the Completion type are triples of the form (type, value, target).
About 4,5,6
What does result.type mean, result.value? Where does this value come from? Explain for each point
result is Completion Specification Type, and the form is (type, value, target).
My code looks like:
case class SRecord(trialId: String, private var _max:Int) {
def max=_max
def max_=(value:Int):Unit=_max=value
}
Then later on I apply a function onto it:
def groupSummaryRecords(it:Iterator[Option[SRecord]], optionSummary:Option[SRecord]):Option[SRecord] = {
var max=0;
var sRecord1 : Option[SRecord] = None
var i=0
while(it.hasNext) {
var sRecord:Option[SRecord] = it.next();
if(i==0) {
sRecord1 = sRecord;
}
..
}
sRecord1.max=max; // getting 'reassignment to val' compilation error
..
}
Why am i getting this compilation error, and how to fix it ?
If I instead change sRecord and sRecord1 instances to be of type SRecord instead of Option[SRecord] as well as the method signature, it all works fine however.
But in some cases I may have a null SRecord hence the use of None/Some. I am new to Scala, using Option/Some all over feels like a real pain if you ask me, i am just thinking of removing all this Option nonsense and testing for 'null' in good ol' Java, at least my code would work ??!
With the line sRecord1.max=max you are trying to call the max method on an Option[SRecord], not an SRecord. You want to access the contained SRecord (if any) and call the method on that, which can be done using foreach:
sRecord1.foreach(_.max=max)
which is desugared to:
sRecord1.foreach( srec => srec.max=max )
(the actual name "srec" is made up, the compiler will assign some internal name, but you get the idea). If sRecord1 is None, this won't do anything, but if it is Some(srec), the method execution will be passed in to operate on the contained instance.
I'm trying to write a class for a scala project and I get this error in multiple places with keywords such as class, def, while.
It happens in places like this:
var continue = true
while (continue) {
[..]
}
And I'm sure the error is not there since when I isolate that code in another class it doesn't give me any error.
Could you please give me a rule of thumb for such errors? Where should I find them? are there some common syntactic errors elsewhere when this happens?
It sounds like you're using reserved keywords as variable names. "Continue", for instance, is a Java keyword.
You probably don't have parentheses or braces matched somewhere, and the compiler can't tell until it hits a structure that looks like the one you showed.
The other possibility is that Scala sometimes has trouble distinguishing between the end of a statement with a new one on the next line, and a multi-line statement. In that case, just drop the ; at the end of the first line and see if the compiler's happy. (This doesn't seem like it fits your case, as Scala should be able to tell that nothing should come after true, and that you're done assigning a variable.)
Can you let us know what this code is inside? Scala expects "expressions" i.e. things that resolve to a particular value/type. In the case of "var continue = true", this does not evaluate to a value, so it cannot be at the end of an expression (i.e. inside an if-expression or match-expression or function block).
i.e.
def foo() = {
var continue = true
while (continue) {
[..]
}
}
This is a problem, as the function block is an expression and needs to have an (ignored?) return value, i.e.
def foo() = {
var continue = true
while (continue) {
[..]
}
()
}
() => a value representing the "Unit" type.
I get this error when I forget to put an = sign after a function definition:
def function(val: String):Boolean {
// Some stuff
}