Current Position:Home > When is a def assigned to his constant value in a derived class ?

When is a def assigned to his constant value in a derived class ?

Update:10-11Source: network consolidation
Advertisement
Hi ,
I encountered some strange thing in my app when using a def in a derived class from abstract base class. (dived by zero exception)
I point it out here using a small code snippet which reflects the problem in my app:
public abstract class Base{
    public var varBase;
public class BaseDerived extends Base{
    def const = 1;
    var temp : Double;
    public override var varBase on replace{
        temp = varBase / const;
        println("{const}");
function run(){
       var test = BaseDerived {varBase:10};
       println ("{test.temp}");
result is:
0
InfinityI assumed that all "defs" will already be assigned before any single use in the code when creating an object. If I put the "def" in the base class all works fine. But apparently this is not case when implementing an abstract class and use a def in that class.
What am I missing here ?
thanks
Guy

The Best Answer

Advertisement
I changed the code to:
public mixin class Base {
    public var varBase;
    init { println("Base Init"); }
    postinit { println("Base PostInit"); }
public class BaseDerived extends Base {
    def constant = 1;
    var temp: Double;
    init { println("BaseDerived Init"); }
    postinit { println("BaseDerived PostInit"); }
    public function showVar(msg: String) {
        println("# BaseDerived ({msg})");
        println("VarBase: {varBase}");
        println("Constant: {constant}");
        println("Temp: {temp}");
    public override var varBase on replace {
        showVar("Before computation");
        temp = varBase / constant;
        showVar("After computation");
function run() {
    var test = BaseDerived { varBase: 10 };
    test.showVar("Created test");
}The output of abstract class:
# BaseDerived (Before computation)
VarBase: 10.0
Constant: 0 --> BAD
Temp: 0.0
# BaseDerived (After computation)
VarBase: 10.0
Constant: 0
Temp: Infinity
Base Init
BaseDerived Init
Base PostInit
BaseDerived PostInit
# BaseDerived (Created test)
VarBase: 10.0
Constant: 1
Temp: Infinity --> NOK and replace not newly triggeredWhile the mixin base class indeed solves it:
# BaseDerived (Before computation)
VarBase: 10.0
Constant: 1 --> constant here already been assigned
Temp: 0.0
# BaseDerived (After computation)
VarBase: 10.0
Constant: 1
Temp: 10.0
Base Init
BaseDerived Init
Base PostInit
BaseDerived PostInit
# BaseDerived (Created test)
VarBase: 10.0
Constant: 1
Temp: 10.0 --> OKThanks for the quick goodies !
I just need to read something on what "mixin" differs from abstract.
cheers
Guy
  • When is a def assigned to his constant value in a derived class ? Update:10-11

    Hi , I encountered some strange thing in my app when using a def in a derived class from abstract base class. (dived by zero exception) I point it out here using a small code snippet which reflects the problem in my app: public abstract class Base{  

  • Assigning constant value to Account Derivation Rule for FA Update:11-30

    Hi, I have the following requirement for SLA in FA Requirement : Cost of Removal Gain should pick the segment1(balancing segment) value from Asset Assignment screen and rest all segments from Book level when the CC <> 'XXXX' (particular CC) and A/C

  • Document Splitting: Editing constant for non-assigned processes Update:10-11

    This query concerns assignment/editing of constants to non-assigned processes which is part of document splitting We currently have one controlling area and one profit center. we have assigned one constant named BQE: "imputation sur comptes de banque

  • Is it possible to use constants in when condition of a trigger Update:11-30

    I need to check for a condition (say user = 'XYZ'). Since this value XYZ has not yet been finalized, I would like to assign this value to a constant and use it in the trigger. Hence any change would require me to only change the value assigned to the

  • BSP ASSIGNMENT ERROR WHEN SELECT A TAB IN TABSTRIP Update:11-30

    Hi all, I have a dump when select one of the four tabs, in which three working fine. Plese see the below dump and help as soon as possible. Runtime Errors         MOVE_TO_LIT_NOTALLOWED                           Exception              CX_SY_DYN_CALL_

  • About constants Update:11-30

    can we assign some dynamic value while declaring the constant. if not give some other logic to assign dynamic value to a constant. parameter: p_char(25) obligatory. data: len type i. len = strlen(p_char). constant c value len. here 'len' field contai

  • Managing constants in base / derived classes Update:11-30

    Hi there, The problem: I'm facing what is certainly a simple problem but fail to come up with an acceptable solution. What I'd like to do is managing constants in a base class and derived class... which for some reasons are not parts of the same pack

  • Assignment of Business place to  Plant Update:11-30

    Hi, Can you please tell me where is option for assignment of Business place to Plant Regards, YadayyaHi, If the business place is assigned to a plant, the system derives the business place from that plant when you post a document through Materials Ma

  • [Solved] Help needed with getting fonts antialiased. Update:10-11

    Greetings.  I'm in the process of installingArch and I must be missing something as fonts just aren't antialiasing.  Any help would be appreciated.  I'm using the Fonts guide on the wiki.  Here's the steps I did: pacman -Rd libxft yaourt -S fontconfi

  • Can we create .CIF file in PI 7.0 Update:10-11

    Hi experts, I have a requirement in which I have to convert the source xml file into target file in CIF format. So my target my file should be target.cif. I want to know whether we can handle this format in XI or not. Below is the example file in CIF