Check-in [a0932d4977]

Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Working on improving TIP 478
Downloads: Tarball | ZIP archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: a0932d497703009a7f693c29de41fa6ef5271256e347e49f963bc2890a5cec59
User & Date: dkf 2018-06-14 13:51:19.790
Context
2018-06-15
09:02
fixed code error check-in: 51aba99441 user: rene tags: trunk
2018-06-14
13:51
Working on improving TIP 478 check-in: a0932d4977 user: dkf tags: trunk
2018-06-13
20:50
examples and docu of tip 507 added check-in: 8331611a7f user: rene tags: trunk
Changes
Unified Diff Ignore Whitespace Patch
Changes to tip/478.md.
1
2

3
4
5
6
7
8
9
# TIP 478: Add Expected Class Level Behaviors to oo::class
	Author:         Gerald Lester <[email protected]>

	State:          Draft
	Type:           Project
	Vote:           Pending
	Created:        18-Oct-2017
	Post-History:   
	Keywords:       Tcl
	Tcl-Version:    8.7


>







1
2
3
4
5
6
7
8
9
10
# TIP 478: Add Expected Class Level Behaviors to oo::class
	Author:         Gerald Lester <[email protected]>
	Author:         Donal K. Fellows <[email protected]>
	State:          Draft
	Type:           Project
	Vote:           Pending
	Created:        18-Oct-2017
	Post-History:   
	Keywords:       Tcl
	Tcl-Version:    8.7
18
19
20
21
22
23
24












































25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
class variable and not to instance/object variables.  They also provide a way to initialize the class variables.  This TIP seeks to add them into TclOO.

# Proposal

The proposal is to add some or all of the functionality of `oo::util`, in particular the `classvariable` and `classmethod`.
Additionally, it is proposed to add an `initialise` class definition command to all the initialization of class variables.













































# Reference Implementation

Reference implementations are mentioned in the proposal section. Additionally, this is a reference implementation for `initialise`:
<pre>
  proc ::oo::define::initialise {body} {
      if {[package vsatisfies [package require Tcl] 8.7]} {
          set cls [uplevel 1 self]
      } else {
          set cls [lindex [info level -1] 1]
      }
      tailcall apply [list {} $body [info object namespace $cls]]
  }
</pre>

# Copyright

This document has been placed in the public domain.







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>





<
|
<
<
<
|






19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74

75



76
77
78
79
80
81
82
class variable and not to instance/object variables.  They also provide a way to initialize the class variables.  This TIP seeks to add them into TclOO.

# Proposal

The proposal is to add some or all of the functionality of `oo::util`, in particular the `classvariable` and `classmethod`.
Additionally, it is proposed to add an `initialise` class definition command to all the initialization of class variables.

## New Metaclasses

The `oo::singleton` metaclass only allows a single instance of the class that
it is mixed into to exist at a time. It hides the `create` method on the class and
implements a cache so that the `new` method will only create an instance of
the class if there is not an existing instance. Subclasses of the singleton
class are _not_ restricted.

The `oo::abstract` metaclass does not allow any instances of the class that it
is mixed into to be created, hiding the instantiation methods (`create`,
`new`, etc.) of that class. The class can still be inherited from and those
subclasses can be instantiated.

## New Definitions

The `classmethod` class definition creates a method that can be used when
invoked against its defining class or any of its subclasses.

The `classvariable` class definition creates a variable that exists in a
class's namespace and which is linked into any instance of that class
(including instances of subclasses); setting the variable with that name from
any of those instances or from the class will result in changes that can be
observed from all instances of that class.

The `initialise` class definition evaluates a script in a context where it can
access the class's namespace, allowing for easier initialisation of a class
than simply overriding its constructor.

## New Helper Commands

The `callback`/`mymethod` command, available within methods, takes the name of
a method and zero-or-more arguments and returns a script fragment that will
allow that method easy to invoke from a callback (e.g., a variable `trace`,
`chan event` callback, or Tk event binding). The command will be available
with both names.

The `link` command, available within methods, creates a binding for methods
so that calling the command with the given name is equivalent to calling
`my $name` instead. It can link multiple methods with one call, one per
argument, and those created commands can be renamed without losing the link;
if an argument is a two-element list, the first element is the name of the
method and the second is the name of the command (which will be resolved
relative to the current namespace if it is not an absolute command name).

# Reference Implementation

Reference implementations are mentioned in the proposal section. Additionally, this is a reference implementation for `initialise`:
<pre>
  proc ::oo::define::initialise {body} {

      set clsns [info object namespace [uplevel 1 self]]



      tailcall apply [list {} $body $clsns]
  }
</pre>

# Copyright

This document has been placed in the public domain.