(std interop)

Reference

Overview

What .NET wants and Bjolang cannot write: functions as delegates of any type and as event handlers, and null.

(import (std interop))
(import/class
  ((Action %a) (: System.Action))
  (Component   (: System.ComponentModel.Component (-> Component))))

;; An Action, for an API that takes one.
(def say (: (as-delegate (fun (s) (println s))) (Action string)))

;; A handler of a .NET event.
(def c (Component.))
(add-event-handler! c "Disposed" (fun (sender e) (println "gone")))
(.Dispose c)                    ; prints gone

A Bjolang function is a Func, and a function that returns void returns the unit value. A .NET API often wants another delegate type: Action, EventHandler, or a type of its own. The functions here make a delegate of that type that calls the Bjolang function.

What fits. The delegate and the function take the same number of arguments. Each argument is converted to the parameter type of the function, so a function that takes Object gets any argument. When the delegate returns void, the result of the function is dropped. Otherwise the result is converted to the result type of the delegate, and a function that returns void does not fit. A delegate with a ref or out parameter does not fit. A function that does not fit raises ArgumentException, when the delegate is made.

Cost. The converter for one delegate type and one function type is compiled the first time, which takes some milliseconds. After that, making a delegate is one allocation, and a call is one more call.

Native AOT. An ahead-of-time build has no JIT, so the converter is interpreted. A call through it is slower than in a JIT build.

See also: as-delegate, as-delegate/type, add-event-handler!, remove-event-handler!, clr-default

Reference

Types

Functions

Types

Delegate

type alias ↑ Types

Type alias for System.Delegate, the type of every delegate.

See also: as-delegate/type, add-event-handler!

ClrType

type alias ↑ Types

Type alias for System.Type, which as-delegate/type takes.

See also: as-delegate/type

Functions

as-delegate

function ↑ Functions

(: as-delegate (-> %f %d))

Returns a delegate that calls f. The type of the delegate is the type that the call site expects.

f
The function.
returns
A delegate of the expected type.
(import/class ((Func %a %b) (: System.Converter)))
(def twice (: (as-delegate (fun (x) (* x 2))) (Func int int)))

When the call site does not give a delegate type, use as-delegate/type. A type that is not a delegate raises ArgumentException.

as-delegate/type

function ↑ Functions

(: as-delegate/type (-> ClrType %f Delegate))

Returns a delegate of the given type that calls f.

type
The delegate type, a System.Type.
f
The function.
returns
The delegate, as a Delegate.

This is for a type that is known only when the program runs, such as the handler type of an event that reflection finds.

add-event-handler!

function ↑ Functions

(: add-event-handler! (-> %t string %f Delegate))

Adds f as a handler of the event name of target.

target
The object that has the event.
name
The name of the event, such as "Disposed".
f
The handler. It takes the same arguments as the handler type of the event, usually the sender and the event arguments.
returns
The delegate that was added. Give it to remove-event-handler! to remove the handler.

The event is found by reflection, among the public instance events of the type of target. An event that is not there raises ArgumentException.

remove-event-handler!

function ↑ Functions

(: remove-event-handler! (-> %t string Delegate void))

Removes a handler that add-event-handler! added.

target
The object that has the event.
name
The name of the event.
handler
The delegate that add-event-handler! returned.

clr-default

function ↑ Functions

(: clr-default (-> %a))

Returns .NET’s default value of a type. The call site gives the type.

returns
Null for a reference type and for a nullable value type such as int?, and zero for a number.
(import/class ((Nullable %a) (: System.Nullable)))
(: (clr-default) (Nullable int))   ; a null int?

Bjolang has no literal for null. Some .NET parameters want one, such as an optional int? that a call must still give. Use this only for such an argument: a null in a Bjolang value of a reference type throws when it is used.