masking public property?

Writing and using Classes in Dyalog APL

masking public property?

Postby Manuel Kassens on Sun Oct 25, 2020 12:32 pm

I want to have some factory methods.

A parameter of a shared method with the name of a instance property masks this property if it creates an instance.

In the sample code an instance created by #createWrong has a public field #name insted of a property.
Code: Select all
:Class Text
    ∇ p←createOk name_
      :Access Public Shared
      p←#.⎕NEW ⎕THIS name_
    ∇
    ∇ p←createWrong name
      :Access Public Sharedhave som [i][i][/i][/i]
      p←#.⎕NEW ⎕THIS name
    ∇
    :Field Private m_name←'unnown'
    :Property name
    :Access Public
        ∇ r←get
          r←m_name
        ∇
    :EndProperty
   
    ∇ make1 name 
      :Implements Constructor
      :Access Public
      m_name←⌽name
    ∇
    ∇ r←show;∆ok
      :Access Public
      ∆ok←{≡/⍵:⍵,⊂'okay' ⋄ ⍵,⊂'wrong'}
      r←(⊂'#name:'),∆ok m_name name
    ∇
:EndClass


In the sample ccde has an instance created by #createWrong a public field with the name
Code: Select all
      (#.ok←Text.createOk'reteP').show
 #name:  Peter  Peter  okay
      (#.wrong←Text.createWrong'luaP').show
 #name:  Paul  luaP  wrong
      #.wrong.⎕NL¯2.1
 name
Manuel Kassens
 
Posts: 2
Joined: Wed Apr 22, 2020 2:17 pm

Re: masking public property?

Postby RichardP|Dyalog on Mon Oct 26, 2020 10:30 am

I think this is just how it is and that you will need to use different names.

Code: Select all
:Class C
    :field public name
    ∇ Init name
      :Access public
      :Implements constructor
      name←name
    ∇
:EndClass


Code: Select all
      c←⎕NEW C 'John'
      c.name
VALUE ERROR
      c.name
            ∧


Compared to:
Code: Select all
:Class C
    :field public name
    ∇ Init arg
      :Access public
      :Implements constructor
      name←arg
    ∇
:EndClass


Code: Select all
      c←⎕NEW C 'John'
      c.name
John
RichardP|Dyalog
 
Posts: 6
Joined: Fri Oct 12, 2018 3:05 pm

Re: masking public property?

Postby gil on Wed Oct 28, 2020 6:33 am

Surely this must be a bug? I understand that the name is shadowed while createWrong is on the stack, but it seems that the instance created with createWrong has managed to change the property name into a variable type. Setters and getters are no longer triggered at all?!

      (#.ok←Text.createOk'reteP').show
#name: Peter Peter okay
ok.⎕NC⊂'name'
¯2.3
(#.wrong←Text.createWrong 'reteP').show
#name: Peter reteP wrong
wrong.⎕NC⊂'name'
¯2.1
gil
 
Posts: 68
Joined: Mon Feb 15, 2010 12:42 am


Return to Object Oriented Programming

Who is online

Users browsing this forum: No registered users and 1 guest