Here’s a little bit of Python code, from <https://gitlab.com/ldo/inotipy>:
info_type = infoptr.contents.info_type
infoptr = ct.cast \
(
infoptr,
ct.POINTER
(
{
FANOTIFY.EVENT_INFO_TYPE_FID : FANOTIFY.event_info_fid,
FANOTIFY.EVENT_INFO_TYPE_DFID_NAME : FANOTIFY.event_info_fid,
FANOTIFY.EVENT_INFO_TYPE_DFID : FANOTIFY.event_info_fid,
FANOTIFY.EVENT_INFO_TYPE_PIDFD : FANOTIFY.event_info_pidfd,
FANOTIFY.EVENT_INFO_TYPE_ERROR : FANOTIFY.event_info_error,
}[info_type]
)
)
That’s a conditional cast, based on a common header field, to the appropriate struct type for that data block.
That’s not just a switch-expression, it’s a switch-expression
returning a type.
Here’s a little bit of Python code, from <https://gitlab.com/ldo/inotipy>:
info_type = infoptr.contents.info_type
infoptr = ct.cast \
(
infoptr,
ct.POINTER
(
{
FANOTIFY.EVENT_INFO_TYPE_FID : FANOTIFY.event_info_fid,
FANOTIFY.EVENT_INFO_TYPE_DFID_NAME : FANOTIFY.event_info_fid,
FANOTIFY.EVENT_INFO_TYPE_DFID : FANOTIFY.event_info_fid,
FANOTIFY.EVENT_INFO_TYPE_PIDFD : FANOTIFY.event_info_pidfd,
FANOTIFY.EVENT_INFO_TYPE_ERROR : FANOTIFY.event_info_error,
}[info_type]
)
)
That’s a conditional cast, based on a common header field, to the appropriate struct type for that data block.
That’s not just a switch-expression, it’s a switch-expression
returning a type.
... what does this have to do with C, or anything at all?
Apart from being an apallingly bit of code.
However I can't see the switch-expression; there is a Dict constructor,
where all elements are evaluated, not just the one selected. That is not
how 'switch' works.
On Sat, 17 Aug 2024 11:19:30 +0100, Bart wrote:
... what does this have to do with C, or anything at all?
C is supposed to be the epitome of the low-level language that can do bit- fiddling and unsafe type conversions and the like. This is an example of
an unsafe type conversion (offering a typesafe interface to the caller, of course) done dynamically, in a language which is generally considered to
be “higher-level” than C.
In sum: types as first-class objects + low-level bit-fiddling = a
combination unavailable in traditional “low-level” languages like C.
Apart from being an apallingly bit of code.
How would you it less “apallingly”?
(This sentence no verb. Also speling.)
However I can't see the switch-expression; there is a Dict constructor,
where all elements are evaluated, not just the one selected. That is not
how 'switch' works.
How does a switch-expression work, then? Can you give us an example?
On Sun, 18 Aug 2024 00:20:49 +0100, Bart wrote:
On 17/08/2024 23:11, Lawrence D'Oliveiro wrote:
On Sat, 17 Aug 2024 11:19:30 +0100, Bart wrote:
... what does this have to do with C, or anything at all?
C is supposed to be the epitome of the low-level language that can do
bit-fiddling and unsafe type conversions and the like. This is an
example of an unsafe type conversion (offering a typesafe interface to
the caller, of course) done dynamically, in a language which is
generally considered to be “higher-level” than C.
In sum: types as first-class objects + low-level bit-fiddling = a
combination unavailable in traditional “low-level” languages like C. >>>
Apart from being an apallingly bit of code.
How would you it less “apallingly”?
(This sentence no verb. Also speling.)
It's an adverb. Although there should have been two P's.
Still not answering the question.
However I can't see the switch-expression; there is a Dict
constructor, where all elements are evaluated, not just the one
selected. That is not how 'switch' works.
How does a switch-expression work, then? Can you give us an example?
Take this Python code that has a similar dict constructor:
def prnt(x): print(x); return len(x)
i=3 a={1:prnt("One"), 2:prnt("Two"), 3:prnt("Three")}[i]
print(a)
It selects the third element keyed with '3', but the output is:
One Two Three 5
So 'prnt' has been called 3 times instance of just once. (Also using a
non-existent key gives an error.)
So do it this way:
a = \
{
1 : lambda : prnt("One"),
2 : lambda : prnt("Two"),
3 : lambda : prnt("Three"),
}[i]()
(Also using a non-existent key gives an error.)
Want a default case for your switch? Easy:
a = \
{
1 : lambda : prnt("One"),
2 : lambda : prnt("Two"),
3 : lambda : prnt("Three"),
}.get(i, lambda : «default»)()
On 17/08/2024 23:11, Lawrence D'Oliveiro wrote:
On Sat, 17 Aug 2024 11:19:30 +0100, Bart wrote:
... what does this have to do with C, or anything at all?
C is supposed to be the epitome of the low-level language that can do
bit-fiddling and unsafe type conversions and the like. This is an
example of an unsafe type conversion (offering a typesafe interface to
the caller, of course) done dynamically, in a language which is
generally considered to be “higher-level” than C.
In sum: types as first-class objects + low-level bit-fiddling = a
combination unavailable in traditional “low-level” languages like C.
Apart from being an apallingly bit of code.
How would you it less “apallingly”?
(This sentence no verb. Also speling.)
It's an adverb. Although there should have been two P's.
However I can't see the switch-expression; there is a Dict
constructor, where all elements are evaluated, not just the one
selected. That is not how 'switch' works.
How does a switch-expression work, then? Can you give us an example?
Take this Python code that has a similar dict constructor:
def prnt(x): print(x); return len(x)
i=3 a={1:prnt("One"), 2:prnt("Two"), 3:prnt("Three")}[i]
print(a)
It selects the third element keyed with '3', but the output is:
One Two Three 5
So 'prnt' has been called 3 times instance of just once. (Also using a non-existent key gives an error.)
(Also using a non-existent key gives an error.)
The equivalent using 'switch' in one of my languages ...
On 18/08/2024 01:23, Lawrence D'Oliveiro wrote:
On Sun, 18 Aug 2024 00:20:49 +0100, Bart wrote:
On 17/08/2024 23:11, Lawrence D'Oliveiro wrote:
On Sat, 17 Aug 2024 11:19:30 +0100, Bart wrote:
Apart from being an apallingly bit of code.
How would you it less “apallingly”?
(This sentence no verb. Also speling.)
It's an adverb. Although there should have been two P's.
Still not answering the question.
On Sun, 18 Aug 2024 01:45:48 +0100, Bart wrote:
On 18/08/2024 01:23, Lawrence D'Oliveiro wrote:
On Sun, 18 Aug 2024 00:20:49 +0100, Bart wrote:
On 17/08/2024 23:11, Lawrence D'Oliveiro wrote:
On Sat, 17 Aug 2024 11:19:30 +0100, Bart wrote:
Apart from being an apallingly bit of code.
How would you it less “apallingly”?
(This sentence no verb. Also speling.)
It's an adverb. Although there should have been two P's.
Still not answering the question.
Still not answering the question.
Here’s a little bit of Python code, from <https://gitlab.com/ldo/inotipy>:
Lawrence D'Oliveiro <[email protected]d> writes:
Heres a little bit of Python code, from <https://gitlab.com/ldo/inotipy>:
Which doesn't have anything to do with C. Take it elsewhere.
| Sysop: | Keyop |
|---|---|
| Location: | Huddersfield, West Yorkshire, UK |
| Users: | 715 |
| Nodes: | 16 (2 / 14) |
| Uptime: | 156:11:44 |
| Calls: | 12,092 |
| Files: | 15,000 |
| Messages: | 6,517,729 |