Check-in [bdb99db270]

Login
Bounty program for improvements to Tcl and certain Tcl packages.
Tcl 2019 Conference, Houston/TX, US, Nov 4-8
Send your abstracts to tclconference@googlegroups.com
or submit via the online form by Sep 9.

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

Overview
Comment:Another less-synthetic example; menton correct implementation branch
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256:bdb99db270c0601c7646feda6c9da278d2cfa6464ef1aa58c4da29191d7e9512
User & Date: dkf 2018-10-28 16:43:02
Context
2018-10-29
13:17
TIP 522 Accepted, Implemented, and Final check-in: 0b332f3d8e user: dgp tags: trunk
2018-10-28
16:43
Another less-synthetic example; menton correct implementation branch check-in: bdb99db270 user: dkf tags: trunk
16:28
updated index check-in: 22c79b6593 user: dkf tags: trunk, minor change
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to tip/524.md.

106
107
108
109
110
111
112





































































113
114
115
116

117
118
119
120
    <<POINT-3>>
    This is ::gorp
    abc = xyz
    <<POINT-4>>
    ::gorp
    example=123






































































# Implementation

Conceptual outline in the branch `dkf-oo-override-definition-namespaces` but
that uses a different API and lacks introspection.


# Copyright

This document has been placed in the public domain.







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


<
<
>




106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183


184
185
186
187
188
    <<POINT-3>>
    This is ::gorp
    abc = xyz
    <<POINT-4>>
    ::gorp
    example=123

This second example (adapted from the proposed manual page) shows just how thoroughly the ability to control the definition language can change things.

    namespace eval myDefinitions {
        # Delegate to existing definitions where not overridden
        namespace path ::oo::define

        # A custom type of method
        proc exprmethod {name arguments body} {
            tailcall method $name $arguments [list expr $body]
        }

        # A custom way of building a constructor
        proc parameters args {
            uplevel 1 [list variable {*}$args]
            set body [join [lmap a $args {
                string map [list %VAR% $a] {
                    set [my varname %VAR%] [expr {double($%VAR%)}]
                }
            }] ";"]
            tailcall constructor $args $body
        }
    }

    # Bind the namespace into a (very simple) metaclass for use
    oo::class create exprclass {
        superclass oo::class
        definitionnamespace myDefinitions
    }

    # Use the custom definitions
    exprclass create quadratic {
        parameters a b c

        exprmethod evaluate {x} {
            ($a * $x**2) + ($b * $x) + $c
        }
    }
    exprclass create cubic {
        parameters a b c d

        exprmethod evaluate {x} {
            ($a * $x**3) + ($b * $x**2) + ($c * $x) + $d
        }
    }

    # Showing the resulting classes and objects in action
    quadratic create quad 1 2 3
    for {set x 0} {$x <= 4} {incr x} {
        puts [format "quad(%d) = %.2f" $x [quad evaluate $x]]
    }

    cubic create cub 3 1 0.5 0.25
    for {set x 0} {$x <= 4} {incr x} {
        puts [format "cub(%d) = %.2f" $x [cub evaluate $x]]
    }

This prints:

    quad(0) = 3.00
    quad(1) = 6.00
    quad(2) = 11.00
    quad(3) = 18.00
    quad(4) = 27.00
	cub(0) = 0.25
	cub(1) = 4.75
	cub(2) = 29.25
	cub(3) = 91.75
	cub(4) = 210.25

# Implementation



See the branch `tip-524`.

# Copyright

This document has been placed in the public domain.