How to callbacks events like bind in incr tcl? - itcl

For example I tried this:
package require Itcl
package require Tk
::itcl::class X {
constructor { } {
canvas .c -height 200 -width 200
bind .c <ButtonPress-1> {::itcl::code $this A}
}
method A { } {
puts "inside A"
}
}
X aa
but after clicking mouse on canvas it is not going inside method A ?
Kindly help me here .

I found the solution :
change {::itcl::code $this A} to [::itcl::code $this A]

Related

refer from inner `with` function to higher level `with` function

In my code I have a struct like this
post { req ->
with(req.objectBody<Person>()) {
logger.info { "Attempt to save person $this" }
with(require<SessionFactory>().openSession()) {
save(this#with)
}
}
}
But IDE warns me that there is more than one label with such a name. In this case
save(this#with)
I want to refer to with(req.objectBody<Person>) instance. How to achieve that?
Technically, you can mark lambdas with custom labels and then use labeled this with those labels. such as:
with(foo()) mylabel#{
with(bar()) {
baz(this#mylabel)
}
}
However, to improve readability, instead of with, you can use the let scoping function and provide a name for the parameter:
foo().let { fooResult ->
bar().let { barResult ->
baz(fooResult)
}
}

How do I extract parts of code into local variables in Kotlin when using Ktor's HTML builder?

I am trying to understand the HTML builder in Kotlin / Ktor.
The example here uses the HTML builder to build the result:
call.respondHtml {
head {
title { +"HTML Application" }
}
body {
h1 { +"Sample application with HTML builders" }
widget {
+"Widgets are just functions"
}
}
}
I am trying to extract the body into a variable like this:
val block: HTML.() -> Unit = {
head {
title { +"HTML Application" }
}
body {
h1 { +"Sample application with HTML builders" }
}
}
call.respondHtml(block)
Now I get the following compile error:
Error:(37, 22) Kotlin: None of the following functions can be called with the arguments supplied:
public suspend fun ApplicationCall.respondHtml(status: HttpStatusCode = ..., versions: List<Version> = ..., cacheControl: CacheControl? = ..., block: HTML.() -> Unit): Unit defined in org.jetbrains.ktor.html
public suspend fun ApplicationCall.respondHtml(status: HttpStatusCode = ..., block: HTML.() -> Unit): Unit defined in org.jetbrains.ktor.html
When I add the first (optional) argument it works again: call.respondHtml(HttpStatusCode.OK, block).
Why does it not work, when I simply try to extract the body into a variable?
I think the compiler doesn't like having a mandatory after default parameters, unless it is a lambda outside of the braces.
Try to name it:
call.respondHtml(block = block)
BTW, if what you want is to extract logic, I would recommend using functions. For your example:
fun HTML.headAndBody() {
head {
title { +"HTML Application" }
}
body {
h1 { +"Sample application with HTML builders" }
widget {
+"Widgets are just functions"
}
}
}
call.respondHtml {
headAndBody()
}
That way you can even add parameters to your block of html, creating a custom component out of it.

Why this Swift code doesn't work - mixing Objective-C class in Swift

I have a huge background as c++ dev, but I'm still newbie in Swift/Objective-C zone. I'm trying to build a class hierarchy and I'm kind of stuck with this code:
protocol CADelegate : class {
func g()
}
class CA : NSObject {
init(i: Int) {}
func f() {
if let del = getDelegate() {
del.g()
} else {
println("delegate is null")
}
}
private func getDelegate() -> CADelegate? {
return nil
}
}
class CB : CA {
override init(i: Int) {
super.init(i:i)
}
var delegate: CADelegate!
private override func getDelegate() -> CADelegate? {
return delegate
}
}
class Client : CADelegate {
func g() {
println("CA delegate")
}
}
class ViewController : ... {
override func viewDidLoad() {
var cb = CB(i: 1)
var client = Client()
cb.delegate = client // the problem row!!!
cb.f()
}
}
On "the problem row" I got an runtime error EXC_BAD_ACCESS(code=1,address=...).
Can somebody help me figure this out? I know I'm doing something wrong, because I'm use to C++ idioms and programming style.
NOTE: also the same is true if the delegate property is weak, as it is in my original code.
Edit 1:
I'm using XCode6 Beta 7
Edit 2:
As it turns out it is not 100% reproducible. 3 out of 5 times it is working but for the other 2 times I've got the bad access exception. It looks like a race condition, but I'm not using any other tasks/threads. This is my main view controller and viewDidLoad should be called on view controller initialization.
Edit 3:
Even more strange - if I remove my constructors in CA and CB, and change the creation of CB without int parameter, the problem disappears! I'm sure now it looks like a bug :)

TypeScript modules

I am wondering if it is possible somehow to have two or more classes in two or more files added to the same module in TypeScript. Something like this:
//src/gui/uielement.ts
module mylib {
module gui {
export interface UIElement {
public draw() : void;
}
}
}
//src/gui/button.ts
///<reference path='uielement.ts'/>
module mylib {
module gui {
export class Button implements UIElement {
constructor(public str : string) { }
draw() : void { }
}
}
}
There will probably be dozens of GUI classes, so having them all in the same file will not be possible. And all my classes will be in the 'mylib' module.
But how do I do that?
If the module mylib {...} is translated into a function then all content of all mylib blocks in all files should be contained within the same function.
Is this at all possible?
When I compile I get this:
$ tsc src/gui/button.ts
src/gui/button.ts(4,39): The name 'UIElement' does not exist in the current scope
This is exactly how it works! If you look at the generated javascript code, it add as an anonymous function that accepts an object, the "the module object":
var mylib;
(function (mylib) {
var Button = (function () {
function Button(x) {
this.x = x;
}
return Button;
})();
mylib.Button = Button;
})(mylib || (mylib = {}));
If you look at the last line (})(mylib || (mylib = {}));) you see that it instantiates a new ojbect (mylib = {}) only if the existing variable is false (or something that evaluates to false, like null).
That way, all "modules" that are named the same will be merged to the same object.
Therefore, internal modules extend each other. I have to note that I have not quite figured out what happens to nested modules.
Update: Your code works for me if I do not use the nested module syntax, but change it to the dot syntax. e.g.:
module mylib.gui {
}
instead of
module mylib {
module gui {
}
}
I'll try to investigate in why this is happening, as far as I have read the spec, both ways should be equal.
Update: if the nested referenced module is marked as exported, it works:
module mylib {
export module gui {
}
}

Is there a way to make [incr Tcl] classes friends?

Is there a way to obtain a friendship between classes in incr Tcl?
Consider the code below.
package require Itcl
::itcl::class A {
private {
proc f { } {
puts "==== A::f"
}
}
}
::itcl::class B {
public {
proc g { } {
puts "==== want to be able to call A::f"
}
}
}
I want A::f to be invisible outside A bur functions of B. How can I achieve this?
Itcl doesn't provide friends.
You can hack around this by constructing a call using namespace inscope, like so:
namespace inscope A {A::f}