Thank you Peter and Duncan, for the explanation and discussion. As for a
workaround, I think it is more readable to define,

test <- function(a = complex(real=1, imaginary=2)){}

Best regards,

baptiste



On 19 January 2014 18:45, Duncan Murdoch <murdoch.dun...@gmail.com> wrote:

> On 14-01-19 4:16 PM, peter dalgaard wrote:
>
>> It's not formals() that is doing you in. Rather, it is a conspiration
>> between two things:
>> (a) R always displays complex constants as x+yi, even if x is zero and
>> (b) there really is no way to specify complex constants with non-zero  real
>> part, i.e. 1+2i is a sum of a real and and imaginary complex constant. You
>> can see the effect already at
>>
>>  quote(1+2i)
>>>
>> 1 + (0+2i)
>>
>>
>>  q <- quote(1+2i)
>>> q[[1]]
>>>
>> `+`
>>
>>> q[[2]]
>>>
>> [1] 1
>>
>>> q[[3]]
>>>
>> [1] 0+2i
>>
>>> str(q)
>>>
>>   language 1 + (0+2i)
>>
>>> str(q[[3]])
>>>
>>   cplx 0+2i
>>
>> Someone might want to fix this by implementing a full syntax for complex
>> constants, but meanwhile, I think a passable workaround could be
>>
>
> That might be nice to do.  Not sure if it's easy or hard...
>
>
>
>>  formals(test)$a <- 1+2i
>>> args(test)
>>>
>> function (a = 1+2i)
>> NULL
>>
>>> test
>>>
>> function (a = 1+2i)
>> {
>> }
>>
>>
>> Or maybe, less sneaky
>>
>> Cplx_1plus2i <- 1+2i
>> test <- function(a = Cplx_1plus2i){}
>>
>
> Less sneaky, but a tiny bit different due to scoping issues:  if the
> function happens to assign something to a local variable Cplx_1plus2i
> before evaluating a, the local variable will be used rather than the global
> one.
>
> Duncan Murdoch
>

        [[alternative HTML version deleted]]

______________________________________________
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel

Reply via email to