close Warning: Can't synchronize with repository "(default)" (/var/svn/tolp does not appear to be a Subversion repository.). Look in the Trac log for more information.

Opened 15 years ago

Closed 15 years ago

Last modified 15 years ago

#898 closed doubt (fixed)

Can I define a function whose existence is unkown to me

Reported by: imendez Owned by: Víctor de Buen Remiro
Priority: low Milestone:
Component: Kernel Version: 1.1.7
Severity: minor Keywords:
Cc:

Description

Hi, I have a doubt about the functions definition.

When I can't know if a certain global variable (A) exists, I can define it using ObjectExist code:

Real If(ObjectExist("Real", "A"), ?, Real A = 4);

This works right.
However, in the case of a function, I'm not sure about how must I define it:

Real If(ObjectExist("Code", "MultiplicaPorDos"), ?, Real MultiplicaPorDos(Real n) { 2*n });
Real m = MultiplicaPorDos(3);

The code above seems to work right, because variable "m" is created, but an error message is shown:

ERROR: [1] No ha sido posible convertir de Code hacia Real para el objeto 'MultiplicaPorDos'

Is this an error of mine in programming or is it an error of TOL?

Change History (2)

comment:1 Changed 15 years ago by Jorge

Resolution: fixed
Status: newclosed

The evaluator is complaining about the object returned by the If expression because it is evaluated in the context of the Real grammar. If you rewrite the code as follow you will not get any error message.

Code If(!ObjectExist("Code", "MultiplicaPorDos"), 
         Real MultiplicaPorDos(Real n) { 2*n } );
Real m = MultiplicaPorDos(3);

of course you can keep the Real in front of the If expression but in that case you should ensure that every branch of the If return a Real object.

comment:2 Changed 15 years ago by Víctor de Buen Remiro

Note that if you want to know if there is a function returning just a given type as Real or a grammar inside a set of options you also can use

  Real FunctionExist({Text|Set} gramatica, Text reference)
Note: See TracTickets for help on using tickets.