键盘快捷键

使用 跳转章节

使用 S/ 在本书内搜索

使用 ? 显示帮助页面

使用 Esc 隐藏帮助页面

Get

Retrieves the value associated with a given key from an object.

Parameters

Get[object, key]

The function requires exactly two arguments.

  • object: The Object from which to retrieve the value.
  • key: An Atom representing the key of the property to access.

Return Value

Returns the value associated with the specified key. The type of the returned value depends on what was stored in the object.

Errors

The evaluation of Get will fail with an error if the key does not exist in the object.

Examples

Let[stu, Object[Student, {
    { #name, "Kevin Stephen" },
    { #age, 24 },
    { #single, #t },
    { #hobbies, { #eat, #book } }
}]];

(* Successfully retrieves the value for the key #name *)
Get[stu, #name]; (* "Kevin Stephen" *)

(* This will cause an error because the key #oops does not exist *)
Get[stu, #oops];
(* ?
Error[ksl::builtin::Get]: Unknown key `oops` for `Object[Student]{age, name, hobbies, single}`.
*)

GetAddress

Retrieves the network address of an active UDP Socket.

Parameters

GetAddress[stream]

The function requires exactly one argument.

  • stream: An active UDP Socket object.

    A UDP Socket is a NativeObject with the type tag #UDPSocket, created by OpenStream.

Return Value

Returns a String representing the IP address and port to which the socket is bound.

No domain-specific errors are thrown by this function.

Examples

(* Create a new UDP Socket bound to a specific address *)
OpenStream[s, "127.0.0.1:11453"];

(* Retrieve the address string from the stream handle *)
GetAddress[s]; (* "127.0.0.1:11453" *)

(* Close the stream to release the socket *)
CloseStream[s];

GetDate

Retrieves the current local date from the system.

Parameters

GetDate[]

The function requires no arguments.

Return Value

Returns a List containing three Numbers representing the current local date in the format:

  • {year, month, day}

No domain-specific errors are thrown by this function.

Examples

(* Assuming the current date is November 17, 2025 *)
GetDate[]; (* {2025, 11, 17} *)

GetEnv

Retrieves the value of an environment variable from the host operating system.

Parameters

GetEnv[name]

The function requires exactly one argument.

  • name: A String representing the name of the environment variable to retrieve.

Return Value

Returns a String containing the value of the specified environment variable. This function only returns successfully if the variable is found.

Errors

An unrecoverable error will occur under the following conditions:

  • If the argument is not a String.
  • If the specified environment variable name is not found in the host environment.

Examples

(* Retrieves the value of the "PWD" environment variable *)
GetEnv["PWD"]; (* "/Users/username" *)

(* Retrieves the current user name *)
GetEnv["USER"]; (* "username" *)

(* This will cause an error if the variable "P" is not set *)
GetEnv["P"];
(* ?
Error[ksl::builtin::GetEnv]: Failed to get environment variable `P` with `environment variable not found`.
*)

GetMessage

Blocks execution of the current thread to wait for and receive a single message from an active UDP Socket.

This function is the primary way to receive data from a network stream. The calling thread will pause and will not proceed until a UDP packet arrives at the socket.

Parameters

GetMessage[stream]

The function requires exactly one argument.

  • stream: An active UDP Socket object.

    A UDP Socket is a NativeObject with the type tag #UDPSocket, created by OpenStream.

Return Value

Returns a String containing the raw payload of the received UDP packet.

No domain-specific errors are thrown by this function.

Examples

The following is a complete example demonstrating two threads communicating over UDP. One thread (recv) listens for messages using GetMessage, while the other (call) sends them.

Block[
  Thread[recv, { }, Block[
    OpenStream[mb, "127.0.0.1:11453"],
    Sleep[0.32], (* wait for other thread spawn *)
    Let[{ cmd, data }, { "START", 0 }],
    While[Not[Eq[cmd, "DONE"]], {
      Sleep[0.32],
      Let[cmd, GetMessage[mb]],
      Print[{ #recv, cmd }],
      Let[data, Add[ParseNumber[Head[
        Try[ReCapture["^next = (?<num>\d+(\.\d+((e|E)(\+|-)?\d+)?)?)$",
          cmd, { #num }], { "0" }]]], data]],
    }],
    CloseStream[mb],
    Print["recv shutdown!"],
    data
  ]],
  Thread[call, { }, Block[
    OpenStream[em, "127.0.0.1:11454"],
    Do[{ Sleep[0.64], Print[{ #call, n }], SendMessage[em, "127.0.0.1:11453",
      Concat["next = ", ToString[n]]] }, n, Range[1, 8, 64]],
    SendMessage[em, "127.0.0.1:11453", "DONE"],
    CloseStream[em],
    Print["call shutdown!"],
  ]],
  { Consume[recv], Consume[call] }
];

Execution Log

{#call, 1}
{#recv, "next = 1"}
{#call, 9}
{#recv, "next = 9"}
{#call, 17}
{#recv, "next = 17"}
{#call, 25}
{#recv, "next = 25"}
{#call, 33}
{#recv, "next = 33"}
{#call, 41}
{#recv, "next = 41"}
{#call, 49}
{#recv, "next = 49"}
{#call, 57}
call shutdown!
{#recv, "next = 57"}
{#recv, "DONE"}
recv shutdown!

Final Return Value

{232, ()}

Explanation

  • The recv thread opens a socket and enters a While loop. Inside this loop, the line Let[cmd, GetMessage[mb]] causes the thread to block and wait.
  • The call thread periodically sends messages to the recv thread’s address. Each time a message is sent, the GetMessage call in the recv thread is unblocked, it processes the message, and then loops back to block again, waiting for the next one.
  • This continues until the call thread sends the "DONE" message, which causes the recv thread’s While loop to terminate.

GetTime

Retrieves the current local time from the system.

Parameters

GetTime[]

The function requires no arguments.

Return Value

Returns a List containing four Numbers representing the current local time in the format:

  • {hour, minute, second, nanosecond}
    • hour: The hour of the day, in 24-hour format $\left(0 \sim 23\right)$.
    • minute: The minute of the hour $\left(0 \sim 59\right)$.
    • second: The second of the minute $\left(0 \sim 59\right)$.
    • nanosecond: The nanosecond part of the current second.

No domain-specific errors are thrown by this function.

Examples

(* Assuming the current time is 01:16:01 and 340,662,000 nanoseconds *)
GetTime[]; (* {1, 16, 1, 340662000} *)

GetType

Retrieves the type tag of an Object or a NativeObject.

This function is used for runtime type inspection, allowing code to verify the kind of a structured data object before operating on it.

Parameters

GetType[object]

The function requires exactly one argument.

  • object: The Object or NativeObject whose type tag is to be retrieved.

Return Value

Returns an Atom representing the type tag of the input object.

No domain-specific errors are thrown by this function.

Examples

Getting the Type of a Standard Object

Let[o, Object[A]];
GetType[o]; (* #A *)

Getting the Type of a NativeObject

(* Create a UDP Socket, which is a NativeObject *)
OpenStream[s, "127.0.0.1:11453"];

(* Get its type tag *)
GetType[s]; (* #UDPSocket *)

CloseStream[s];

Greater

Compares two numbers and returns #t if the first is strictly greater than the second.

Parameters

Greater[x, y]

The function requires exactly two arguments.

  • x: The first Number in the comparison.
  • y: The second Number in the comparison.

Return Value

Returns an Atom.

  • #t if x is strictly greater than y.
  • #f if x is less than or equal to y.

No other domain-specific errors are thrown by this function.

Examples

(* 10 is greater than 5 *)
Greater[10, 5]; (* #t *)

(* 5 is not greater than 10 *)
Greater[5, 10]; (* #f *)

(* 5 is not strictly greater than 5 *)
Greater[5, 5]; (* #f *)

(* Works with negative numbers *)
Greater[-1.5, -2.5]; (* #t *)