This question already has answers here:
How to force an error in a gnumake file
(3 answers)
Closed 8 years ago.
Since an undefined variable can lead to an unexpected behavior if it is simply replaced with empty text (consider target directories), is there a way to have make check if a variable is undefined and stop with an error in that case?
The condition can be detected with conditionals but then how to stop execution?
ifeq ($(strip $(notdefinedforsure_man)),)
out = Undefined variable detected
endif
I'm looking for something like
requiredef var1, var2
or a simple return with error statement to be used in the conditional above
Use the origin builtin function:
ifeq (undefined,$(origin VARIABLE))
$(error VARIABLE is not defined)
endif
See the documentation for details.
Related
This question already has answers here:
"Unclassifiable statement" when referencing a function
(1 answer)
Fortran array cannot be returned in function: not a DUMMY variable
(1 answer)
Closed 3 years ago.
I tried to write a code which gives GCD of two number:
program main
implicit none
integer::A,B,gcd,ans=0
read*,A,B
gcd(A,B)
write(*,*)'GCD of ',A,' and ',B,': ',ans
end program main
recursive function gcd(A,B) result(ans)
implicit none
integer,intent(in)::A,B
integer::ans
if (A==0) ans=B
if (B==0) ans=A
!base_case
if (A==B) ans=A
!recursive_case
if (A>B)then
ans=gcd(A-B,B)
else
ans=gcd(A,B-A)
end if
end function gcd
My input was:
98 56
I expect 14 but got this error:
source_file.f:5:4:
gcd(A,B)
1
Error: Unclassifiable statement at (1)
I didn't understand why I am getting this error? I heartily thank if anyone explain me why am I getting error.
You cannot specify intent(out) or any other intent or related attribute for the result variable. See Fortran array cannot be returned in function: not a DUMMY variable
Use just
integer::ans
In addition, just
gcd(A,B)
is not a valid way to use a function in Fortran. Use
ans = gcd(A,B)
or
print *, gcd(A,B)
or similar.
Please realize that ans declared in the main program is a variable that is not related to the result variable of the function. Even if the name is the same, they are two different things. It will be better to rename one of them to make it clear.
This question already has an answer here:
Error in read.table: !header: invalid argument type
(1 answer)
Closed 3 years ago.
I just noticed that I now have to explicitly state FALSE when using such functions as read.csv or setting printFlag to FALSE in mice. Is this an update to RStudio or just a bug?
Sample:
read.csv(text="1,S0006,C000124,12Jan2017,179,7296
2,S0002,C000124,26Feb2017,109,7941
3,S0008,C000124,22Feb2017,190,4511
4,S0006,C000124,03Jan2017,150,7296
5,S0005,C000124,08Feb2017,120,5812
6,S0003,C000124,26Apr2017,46,7512",header=F)
fails with:
Error in !header : invalid argument type
which can then be fixed by setting header=FALSE
RStudio: $version
[1] ‘1.1.463’
R: 3.5.3RC
I can almost replicate this (not quite the same error message) by assigning a character value to F:
F <- "abc"
read.csv(text="1,S0006,C000124,12Jan2017,179,7296
2,S0002,C000124,26Feb2017,109,7941
3,S0008,C000124,22Feb2017,190,4511
4,S0006,C000124,03Jan2017,150,7296
5,S0005,C000124,08Feb2017,120,5812
6,S0003,C000124,26Apr2017,46,7512",header=F)
Error in !header : invalid argument type
using FALSE instead of F is a good idea (for precisely this reason); you could also try rm(F) and see if that allows your original code to work.
This question already has an answer here:
Unexpected behaviour with argument defaults
(1 answer)
Closed 5 years ago.
Can anyone explain me what is wrong in this code below. What I thought I am doing here is
a declaration of a global variable a=5
a definition of a function fun which takes one argument which defaults to the aforementioned global variable a
And when I call fun() without any parameters the local variable a becomes a copy of the global variable a and at any point in the function code it takes precedence over the global a (unless I specifically use get("a", envir=parent.frame))
But I must be wrong. Why isn't it allowed?
> a = 5
> fun = function(a=a) { a + 1 }
> fun(4)
[1] 5
> fun()
Error in fun() :
promise already under evaluation: recursive default argument reference or earlier problems?
And when I call fun() without any parameters the local variable a becomes a copy of the global variable a
No: default arguments are evaluated inside the scope of the function. Your code is similar to the following code:
fun = function(a) {
if (missing(a)) a = a
a + 1
}
This makes the scoping clearer and explains why your code doesn’t work.
Note that this is only true for default arguments; arguments that are explicitly passed are (of course) evaluated in the scope of the caller.
This question already has answers here:
Go: invalid operation - type *map[key]value does not support indexing
(2 answers)
Closed 9 months ago.
I try to loop through a map, that I pass as a pointer to a function, but I can't find a way to access the elements. This is the code:
func refreshSession(sessions *map[string]Session) {
now := time.Now()
for sid := range *sessions {
if now.After(*sessions[sid].timestamp.Add(sessionRefresh)) {
delete( *sessions, sid )
}
}
}
Line 4 in this example return following compile error:
./controller.go:120: invalid operation: sessions[sid] (type *map[string]Session does not support indexing)
I tried brackets, but it had no effect. If I take away all reference operators (* &) then it compiles fine.
How must I write this?
You don't need to use a pointer with a map.
Map types are reference types, like pointers or slices
If you needed to change the Session you could use a pointer:
map[string]*Session
De-reference the map first and then access it (Example on play):
(*sessions)[sid]
It's also noteworthy that maps are actually reference types and therefore there is a very limited use-case of using pointers. Just passing a map value to a function will not copy the content. Example on play.
You are not taking into account the precedence of *.
*session[sid] really means *(session[sid]), that is, first indexing the pointer to map (hence the error), then dereferencing it.
You should use (*session)[sid].timestamp to first dereference the pointer to the map and then access it using the key.
I have made a progress bar using the winProgressBar method in R. What I want to do is if someone instantiates my program while it is doing all of its processing, I want the current progress bar to close. I tried using a statement that says
if(exists(progressBar)) {
close(progressBar);
}
but I get an error from the console that says
Error in exists(progressBar) : object 'progressBar' not found
I know that it will not exist during the first iteration of my program, but there is no reason that I can find that would make an if statement cause the program to crash.
If you read the help for exists you will see the following under Arguments
x a variable name (given as a character string).
So
exists('progressBar')
will return TRUE or FALSE.