Episode Transcript
Available transcripts are automatically generated. Complete accuracy is not guaranteed.
Wolf (00:03):
Howdy everybody! It's
another episode of Runtime
Arguments.
I'm Wolf, and my partner, asalways, is Jim McQuellen.
Hey Jim.
Hey Wolf.
Um today uh we are gonna talkabout types.
Um the word types probably madeyou want to change the channel,
(00:24):
but it's actually really cool.
There's lots of different facetsto types.
Types mean something to the CPU,they mean something to the
language you're in, they meansomething to the human beings
who are reading your program,they mean something to you when
you're compiling.
I think there's a lot to sayabout types.
Um we're gonna start at thebottom and work our way up.
(00:46):
Uh this is gonna be an episodewhere uh we both do a lot of
talking.
Um so uh at the beginning, uhJim knows a lot about machines
and CPUs.
I know a lot about languages.
Uh I think it's gonna be uh I'mpretty pleased with what we're
gonna s we what we're gonna saytoday.
Um as always, uh we care aboutyour feedback.
(01:10):
Uh in a in a little bit, we'regonna talk about some feedback
we've had to some previousepisodes.
Uh, and at the very end of theshow, we're gonna reiterate how
you can best give feedback.
Uh I'm gonna say it right now:
the absolute best way to give us (01:22):
undefined
feedback is email to feedback atruntimearguments.fm.
Um and in fact, uh speaking ofrecent episodes, uh, I am super
pleased to say that uh our mostrecent episode, the one on AI,
(01:46):
had more downloads in two weeksthan any episode we've done yet.
And uh people have actually comeup to me uh at the office and in
user group meetings and whatnotuh to talk about it, to tell me
their opinions, both good andbad.
Um but before we talk about anyfeedback, uh let's uh let's talk
(02:08):
about us.
Jim, how how'd your week go?
Jim (02:12):
You know, it was a it was a
busy, busy week.
I got a lot of work done, and Iactually got to try some AI
stuff.
Uh I'm super glad about that.
Wolf impressed upon me that Ishould.
And uh so I uh I have an accountuh to use Gemini because we
we're a Google workspace or wwhatever they call that thing,
(02:33):
workspace, workplace, whatever.
Anyway, we have an account touse Gemini Pro.
So I gave that a little bit of atry, and it was kind of
interesting.
I asked it to generate a Sudokusolver in Perl.
And it took a few seconds and itspit out this maybe 75-line Perl
program.
Oh, that's pretty cool.
(02:54):
So I cut and pasted it into ainto a file, made it executable,
and I ran it and it blew up.
Uh syntax error.
I looked at the code, and therewas a couple of places it was
trying to dereference a scalarthat wasn't a reference at all,
and it died.
Wolf (03:11):
So I went back to Gemini
and which you noticed because
you're a domain expert.
Jim (03:16):
Yeah, yeah, okay.
Uh I I I fixed the bug becauseit it was obvious what was
wrong.
So I fixed it and I ran it, andit was great.
But I went to I went back toGemini, and because Gemini and
and these AIs, Claude and therest, they're conversational.
Um I said, hey, your program hasa bug in it.
It's trying to dereferencesomething that's not a
(03:38):
reference.
And it was funny because, firstof all, it apologized, and then
it told me I was smart and thankyou for for finding the bug, and
it chewed on that for a coupleof seconds and produced a new
version.
It said, here's a new version.
This one doesn't have that bug.
And I copied that into a fileand I ran it, and that one
(03:58):
worked, so that was kind ofneat.
Uh I my question is, why didn'tit give me the bug-free version
to start?
That that kind of surprised me.
Uh, but it was interesting.
And I I also used AI to uh, youknow, I I do a lot of Googling,
and I've I I've changed my way Ipose my questions.
(04:19):
Uh I I I I saw a preview for amovie, or it wasn't even a
preview, it was just a clip onone of the one of the social
media sites on a movie, and I uhRussell, no, not Russell Crows,
somebody else was in it.
Um I can't even remember rightnow.
But uh I I just went to I wentto Google and I typed in uh uh
(04:39):
what's the movie about the theold west uh uh soldier who is
tasked with taking a Indianchief back to his uh uh home
grounds in Montana.
That was all I said.
And immediately it replied backand it told me uh the name of
the movie and who was in it, anduh that was cool.
(05:01):
That that's not how Google usedto work, and I think that's just
because of it's got Gemini builtin, and it it sort of changed
how I think about asking myquestions now.
I don't try to ask them as aprogrammer, I try to ask them
more as a human, I think.
And it was kind of neat.
Anyway, how's your uh weekgoing, Wolf?
Wolf (05:21):
Uh my previous week uh has
been so astoundingly busy.
I'm always working onproductivity and trying to uh
not just get more stuff done,but get of the things I have to
get done the most important,most move the needle kind of
(05:42):
stuff done.
Um and I'm not doing this veryscientifically, I'm making lots
of changes at once.
Um, you know, what hours I sleepand when I commute and how much
exercise I'm getting and how Iprioritize.
I'm changing it all at once, andso last week was really
(06:02):
successful, but I don't knowwhy.
So that was uh that was uhunfortunate for me.
I'm feeling good that I'mgetting more done.
Um but uh I'm a programmer, Iwant to know why.
That's I I hate the kinds ofbugs where they just suddenly go
(06:22):
away and you don't know what waswrong or why it why it ever
worked or why it doesn't worknow or why it works again, or I
feel the same way about myproductivity.
So that was my previous week.
Feeling good about stuff doneand less good about
understanding how I got stuffdone.
Um but let's get into thefeedback.
(06:43):
Um like I said, we had someoverwhelming response to this uh
AI episode.
Um what have what have yougotten back, Jim?
Jim (06:57):
Um well we we we got
several emails back from people,
and they were all uh uh reallyenjoying the article, or the the
uh episode rather.
And um and so that was nice tosee.
And you know, not exactlyfeedback, but I uh this is
related.
I did I read an article uh abouthow uh the free as uh free BSD
(07:19):
project.
Uh they've got a proposal outthere that says they shouldn't
accept any contributionsgenerated by AI, including
source code, documentation,commit messages, and because the
LLMs are unable to provide thenecessary assurance that the
generated material is compatiblewith the free BSD license or
that the material has beenappropriately attributed to the
(07:41):
original authors.
Um I think that's prettyinteresting.
Uh I'm not sure if this proposalwill actually get approved, but
uh if you're if you'recontributing to the free BSD
project, um it it's it's prettyinteresting.
You're not supposed to use AI inany portion of your
contribution.
(08:01):
Uh whereas uh the Linux kernelteam, um, they do accept
AI-generated source code anddocumentation, but must still
meet the strict quality uh andstyle standards that uh that all
contributors uh uh must mustmeet.
Uh and they have to betransparent about the fact that
(08:22):
it was generated generated byAI, and contributors must ensure
that AI generated code doesn'tviolate any copyright or
licensing agreements.
I don't know how you're gonna dothat.
How do you generate somethingwith AI and be uh certain that
it's not violating anybody'scopyright?
(08:43):
Uh that's interesting.
Wolf (08:45):
Um Yeah, um of those two
things, I I feel like I'm more
on the side of the uh Free B SDproposal than I am on the Linux
side.
Um because I I just feel likeyou can't necessarily prove that
what you're adding is uhlicensed correctly, safe, and
(09:09):
attributed.
I don't think those things Ifeel like the FreeBSD guys got
it right.
Jim (09:14):
I I think so, except that
contradicts something that you
said during the episode, andthat was you use uh uh AI to
generate your commit messages.
Well, the Free BSD guys aresaying you can't even do that.
So I guess you're notcontributing to the Free BSD
project.
Wolf (09:34):
Or when I do, I'll write
my own commit commit messages.
Yeah.
The reason like I feel likethat's okay is um that there's
uh the repo things come from andthere's the product you're
delivering.
And if a commit message you gothelp from AI, well, that's in
(09:57):
the repo, yeah.
But it's not part of theshipping product.
It doesn't contribute at all tothe shipping product.
So I feel like that's an edgecase.
Is it okay or not?
Yeah, I I I haven't reallythought about it enough to to
have an answer to that.
Jim (10:16):
Okay.
I'll I guess I'll accept that.
And I I I think the FreeBSD guysI I I side with them as well.
Um we'll we'll see how it shakesout if that proposal actually uh
gets approved.
Uh I think that's prettyinteresting.
Wolf (10:32):
Um I got some feedback.
Jim (10:34):
Yeah, okay.
Wolf (10:36):
Uh I got some feedback in
person uh from somebody I really
respect, uh who's really smart,and he listened to the whole
episode.
And uh I think the episode waslong, but I made I made two
pretty important points.
Uh one of the points is AIdoesn't give you the best
(11:00):
answer.
Uh it certainly doesn't give youan answer that's better than
anybody has ever given before.
It gives you the most popularanswer.
Um and the second thing I saidwas that um to to have success
with AI, you you need to I'm I'mgonna use the words domain
(11:23):
expert, but you need to be ableto look at the end product and
know enough to know does itsolve the problem?
Is it correct?
Does it have weaknesses?
You've got to understand whatyou made.
And maybe you made it together,and maybe AI made it entirely by
(11:43):
itself, but if you don't if youcan't prove that it does the
right thing, um, you're notgonna have a very good day.
So those are the two things thatI thought were the biggest
takeaways from the AI episode,and this smart guy that listened
to the episode, um he liked theepisode, but he said, his
(12:07):
conclusion was, but your pointswere maybe valid for a person
like Jim who hasn't used it yet,but everybody else already knows
what you said.
Uh and I felt like um no, peopledon't know this.
There are people who think AI istheir girlfriend, or that it's
(12:31):
actually thinking, or that it'spredicting the future, or it's
it's giving you the bestpossible answer.
So yeah, yeah, uh I thinkthere's all there are smart
people who didn't need to hearthe things I said, but my
feeling is a lot of people did.
So that was the feedback I got.
(12:51):
Anyway, enough about AI.
Jim, let's talk about types.
Jim (12:56):
All right, thank you.
Well, um, you know, before wecan talk about types, we have to
spend just a few seconds talkingabout uh uh some some units of
measure uh and what they mean.
Uh you know, a bit, it's thesmallest uh piece of data that a
that a computer can know.
It's a zero or a one, right?
Um it's a binary thing.
Uh bytes are made up of bits.
(13:18):
A byte is eight bits.
Uh then there's this thingcalled a nibble, which is four
bits, but in all my years I'venever used a nibble.
Uh, you know, I might think of anibble as one hex digit in a in
a hex string, but you know, whatelse?
What else is a nibble?
Um and then words.
Uh words are made up of somemultiple of bytes, uh, and it
(13:41):
really depends on the machinearchitecture, the CPU
architecture.
Uh if it's uh if it's a 16-bitprocessor, a word is likely
going to be 16 bits long.
Uh if it's a 32-bit processor,it's a 32-bit word.
And of course, a 64-bitprocessor is guess what?
64 bits.
Um we don't talk much aboutwords either.
(14:02):
You know, it's really aboutbytes, I think.
Um let's get into the hardware alittle bit, though.
Uh, the hardware, the CPU, itknows about these things.
Uh, it knows about uh therethere are integral types.
I'm gonna talk sort of in termsof a C programmer, uh, because I
did an awful lot of Cprogramming a long, long time
ago.
I haven't touched it muchlately.
Wolf (14:23):
Can I actually say the
thing about C that um it's the
thing they said when C firstcame about.
And that is C was written to bea portable assembly language.
Jim (14:38):
Yeah.
Yeah, an assembly language, uh,you know, for those that that
haven't used it, that's like theclosest you'll ever get to a
CPU.
At the assembly language level,you are talking uh about machine
instructions and and things thatare really specific to that CPU.
So you would write differentmachine instructions for a uh
x86 architecture than you wouldfor uh for an ARM or for a risk
(15:03):
uh CPU or or really any of thoseuh CPUs, they all have different
instruction sets.
And at it at the assemblerlevel, that's what you're you're
talking.
Uh at the C level, as Wolfpointed out, that's a portable
assembly language.
Uh at the C level, uh everybodytalks the same.
Doesn't matter what your CPU is.
(15:25):
Um but you know there are someuh there are some basic types
that uh that C knows about andand the hardware knows about.
So we'll you know I'll point outthat there's uh there's a uh
character, uh C-H-A-R, car orchar.
I'm never quite sure how to howto pronounce it.
How do you pronounce it?
Wolf (15:42):
I guess I say it different
than everybody because I say
care.
Jim (15:46):
Care.
Okay, I I guess I say it thatway too when I say it.
Um uh that's eight bits, right?
Um uh then you get into yourintegers.
Uh an integer, uh you know, uhWolf pointed out a couple of
weeks ago when we startedtalking about this episode that
uh that an integer size isgenerally the natural size of
(16:07):
the registers of the CPU.
I did a little bit digging, andand and that's not what I found.
Um uh registers, or uh notregisters, but uh an integer in
an x86 uh 64, it's a 64-bitprocessor, integers are only 32
bits long.
Uh Raspberry Pi, that's an ARMchip, again, 32 bits long.
(16:28):
Uh a Mac, I've got an M1 in myuh studio, and that's 32 bits.
I kind of thought those wouldall be 64 bits, but they're not.
Wolf (16:38):
I I will point out that uh
in these CPUs, um, yes, the CPU
registers are wide, 64 bits, butall of the CPUs that you just
named have operations that workon the entire register and also
(16:58):
just half of the register.
Jim (17:01):
Yeah, a lot of the
registers, there are there are
general registers in theseprocessors, and they're 64 bits
wide.
Uh but then those registersactually have have uh uh uh they
can be looked at in pieces.
You you know, in a s in a 64-bitregister, you can look at it as
two 32-bit uh uh there there's aname for that register, and it
(17:21):
references the either the first32 bits or the second 32 bits.
And there's also breakdowns by16 bits.
Uh, but the the generalregisters are 64 bits long, uh,
but for some weird reason, intsare 32 bits.
Uh then you got you you've gotlike uh uh little adjectives you
can put on these uh uh when youdeclare them in C.
Uh you've got a short int, uh,and that would be 16 bits.
(17:44):
Uh a long int, there is your 64bits.
And interestingly, there's athere's a type called long long
int.
That's also 64 bits.
Uh I don't know why it's notlike 128 bits.
It seems like if a long is 64,long, long ought to be longer.
But it's not.
The C language has thisinteresting specification, and
(18:05):
let's see if I can get thisright.
Uh it doesn't really make anyassumptions about the size of a
short, an int, or a long.
I think the specification says ashort shall be no longer than an
int, and a long shall be noshorter than an int.
It's possible they're all allthree of those are the same
(18:26):
size.
Uh I think by convention,though, uh now uh uh shorts are
16, ints are 32, and longs are64.
Um C99, which is long after Istarted uh uh after I was
programming in C, C99 added abool type.
It's actually underscore capitalB O O L.
(18:49):
Uh interesting, that takes eightbits, even though it's only got
a value of zero or one.
And the cool thing is if you putany other value into a bool
variable, it's still gonna haveeither a zero or a one.
If you put a zero in, it's gonnabe zero.
If you put a three in, it'sgonna have a value of one.
I thought that was kind ofweird.
Wolf (19:09):
Okay, now I have a
question.
Jim (19:10):
Yeah.
Wolf (19:12):
Um so I can see two
possibilities for a bool.
Uh one is that any non-zerovalue is true, and the other is
that it's only looking at the lelowest level, the lowest bit.
Um so three is true because thethe zero bit is set.
(19:34):
But would two be true?
That's my question.
I actually don't know theanswer.
Jim (19:39):
I I I'm I'm guessing that's
uh uh machine specific, but uh
if you were writing themicrocode to process that, you
may just look at the at the theleading bit to see what it is,
you know, because uh uh uh a oneis only one bit long and and you
know, zero or one.
So if it's uh if it's zero iszero, if it's one, it's it's
(19:59):
true.
Um a two is uh binary one zero,leading bit as one.
A three is binary one one,leading bit as one.
A four is binary one zero zero,again, leading bit as one.
Uh we could spend all dayrunning on this, but uh um I I
thought it was interesting.
(20:19):
And we're gonna talk aboutstructs a little bit later on,
but a struct is something youcan use to to combine uh data uh
uh data types.
And so Wolf sort of uh positedthe the uh assumption uh that if
you put a couple of bools into astruct that maybe the struct
(20:40):
would only be eight bits long.
Uh no.
Wolf (20:44):
If you are there are some
languages where you can make
that be true.
Jim (20:50):
Okay.
Uh well in in my testing, what Ifound is if you put three uh
bools into a struct, that structis 24 bits long, three times
eight.
So I I thought that wasinteresting.
Um aside from uh those uhintegral types, uh care in tool,
uh floating types.
Uh if you got to do floatingpoint arithmetic, there's a
(21:11):
float, there's a double, andthere's a long double.
A float is uh 32 bits.
Um it uses something called theIEEE 754 single precision binary
floating point format.
Um now that came out in uh let'ssee, in 1985.
Again, I was programming a Cbefore that.
(21:34):
I've been doing this a longtime.
Uh, so that was even before theIEEE 754 spec.
Uh, but now pretty mucheverybody's using that IEEE
spec.
And there's a 32-bit version, a64-bit version, and 128-bit
version of that spec or formatfor that spec.
Uh a float in C is 32 bits, adouble is 64 bits, and a long
double is 128 bits.
(21:55):
So now we're getting into the128 bits.
That's uh what was that, 16bytes?
Is my math right?
Um one piece of data taking upuh uh 16 bytes.
That's that's kind of neat.
Um anyway, these types areimportant to to the compiler
(22:15):
because it it uses that togenerate machine code that
actually runs to generate the uhthe uh uh instructions um that
that your program will run with.
Um another type that that ifyou're a C programmer, you run
into this all the time, andthat's pointers.
Uh pointers are 64 bits on onthese uh uh systems.
(22:39):
Uh but it's important to notepointers are not integers.
You might want to treat it likean integer.
A lot of people do.
They should not.
Um uh they're they're they'rethey're they're pointers.
They're not integers, they'repointers.
You can do arithmetic with them.
Um something I didn't mentionearlier though, with uh back to
the uh integral types is um uhwith with uh integers you have
(23:03):
signed and unsigned.
Uh and all they do with integersuh is they they uh set the the
most significant bit to a one ifit's negative.
Um and it would be zero if it'spositive.
Uh so you know go back to aneight-bit uh byte, um that can
hold it it's eight bits, it canhold 256 different
(23:27):
possibilities, right?
So it can hold uh if you'retalking about uh unsigned, it
can hold from zero to twofifty-five.
That's that's the types ofnumbers you can fit into that
space.
Um if it's signed, then it's uhwhat is it, uh uh 127, a
positive 127, a negative 128, Ithink.
(23:49):
Um that's the range of valuesyou can store in that.
Uh 60, uh 16-bit, it's a largerset.
Uh 65,536 uh is the uh from zeroto sixty five thirty five is the
range of uh values you can storein that.
Uh 32-bit, it's four fourmillion something, right?
(24:12):
Is that right?
Four million, four billion.
Wolf (24:14):
I think it's bigger than
millions.
Jim (24:15):
That's four, four billion.
Wolf (24:16):
I hate big numbers.
Jim (24:18):
Yeah, four billion.
But if it's signed, it's gonnabe half of that, right?
It's gonna be two billionsomething positive, two billion
something negative.
Uh it it it's kind ofexhausting.
But uh it's it's uh that that'show the computer does its thing
with all these different uh uhtypes internally.
Um let's talk just a little bitabout the floating point
(24:39):
numbers.
Um uh the IEEE spec.
Uh it defines several things.
One of them I was reallysurprised about.
If you ever work in JavaScriptand you have something that you
treat it as a number and itturns out not to be, uh you get
the value of N-A-N, capital Nlittle A, capital N, not a
number.
That's an I think 754 thing.
Wolf (25:03):
If you have the value NAN
in JavaScript and you use the
type function on it, the thingit tells you is that it's a
number.
I think that's it.
Jim (25:17):
I'm not sure my life has.
Even though uh so what you'resaying is not a number has a
type of number.
That's what I'm saying.
Even though it's not it tellsyou it's not, uh, but but
JavaScript will tell you it is.
So that that's kind ofinteresting.
Um the IEEE uh 754, it definesthe interchange formats, uh
(25:40):
rounding rules.
That's important that everybodydo rounding up or down.
When you're when you're dealingwith floating point, um you're
gonna have to round numberssomewhere along the way.
Uh the IEEE spec defines whatthose rounding rules are.
Uh, like 7.5 if you round it toan integer, is that seven or
eight?
Uh the rules are are in the IEEEspec.
(26:03):
Uh it also handles uh exceptionhandling.
Uh what happens if you divide byzero?
Right?
That's an exception.
IEEE spec tells you uh uh thatthat's a that's an exception.
Uh overflow.
What happens if you try to stuffa number into a variable or into
a into a uh uh uh a piece ofmemory and the data type uh
doesn't hold that number?
(26:24):
Um I I know this isn't floatingpoint, but you know, if you look
at uh uh a character, eightbits, and like I said, it holds
from zero to two fifty-five.
What happens if you put 300 intoit?
Right?
You got overflow.
Uh the uh the CPU doesn't caremuch if you do that.
It'll just give you uh uh weirdbehavior.
Wolf (26:48):
I wanted to uh mention
something when you talk started
talking about nan, I wasimmediately uh reminded of null
in uh database stuff because twonans are not equal no matter
what, even though they are thesame thing.
And any uh expression that has anan in it, no matter what, the
(27:14):
answer to that expression isnan.
Um so it's not equal to anythingelse, right?
It's not equal to itself, andevery use of it leads to another
nan, um just like null in adatabase.
Almost just like null in adatabase.
Jim (27:30):
It is, yeah.
Null in a database confusespeople.
Um if you try to comparesomething to null, uh unless
you're looking specifically fornull, but if you try to compare
a variable to null, that thatcomparison is false.
No matter what.
If you're comparing anything tonull, and I think even if you
compare null to null, it's gonnabe false.
(27:50):
Um so that's yeah, that's a lotlike not a number.
Um if you um uh well we talkedabout uh overflow, um, but let's
talk about not overflow, let'stalk about alignment.
Uh this is important in a in aCPU, uh data alignment.
(28:10):
Um and what I what do I mean bydata alignment?
Um you know, we talk about uh8-bit by 16-bit and 32-bit
values.
Um if you try to do anyoperations on some of these
values, um they have to bealigned on a on a word boundary
many times.
Uh with an x86 CPU, they can bealigned on an 16-bit or 32-bit
(28:35):
or 64-bit boundary.
Wolf (28:37):
Where by alignment you
mean that the address in memory
is divisible by that number.
Jim (28:43):
Yeah, the address in memory
is a multiple of that.
So if it if it needs to bealigned on a 32-bit boundary,
that means that uh if you let'ssay you're starting at zero, if
you store a byte there, uh it'sgonna start in in the first uh
eight bits, and the next uh uhthe next uh 24 bits are gonna be
(29:03):
wasted.
So if you try to store anotheruh uh uh well if you try to
store a word after that or a youknow a 32-bit after that
eight-bit, it's gonna store that32-bit uh value uh uh starting
at position three, right?
Four.
Starting at position four.
Wolf (29:25):
Right.
Jim (29:26):
Yeah.
Uh 0, 1, 2, and 3, that's 32bits.
That's gonna be your firstvariable or your first uh your
first bit of data.
If you try to store a 32-bitvalue after that, uh it's gonna
start in position four.
Um and this is gosh, programmersdon't don't think about this
(29:48):
stuff anymore.
Um and I kind of think theyshould.
Uh back uh years ago whenstorage was was expensive.
Uh you know, now you can buy 16terabyte drives for a couple of
hundred bucks, right?
Uh but back uh when I wasstarting out, uh this is my old
guy rant uh being turned onright now.
(30:11):
Uh we used to uh I used to do alot of COBOL programming, and we
used to lay out records inCOBOL, and we were really,
really careful about laying outthat record so that everything
uh would fit in the least amountof space.
So like you didn't you didn'tstore a byte and then a 32-bit
(30:32):
value and a byte and a 32-bitvalue because that would take uh
what I just described therewould take uh uh four 32-bit
chunks of memory, right?
So four 32, 100, that would take128 bits, even though it's only
uh 8-bit.
It's two 8 bits and 2 32 bits.
(30:53):
It seems like it should add upto um uh what is that, seven uh
80?
No, it takes 128 bits to storethat.
Wolf (31:02):
Uh because you got to be
able to do that.
So you use the word alignment,yeah.
Uh, and alignment is the causeand the effect.
There's a special word for thattoo in languages.
That word is packing.
And um C has a rule that it laysout the variables in exactly the
(31:24):
order that you declared them.
And some other languages, I'mlooking at you, Rust, uh, have
the opportunity to rearrange thevalues uh so that they pack
tighter together.
Rust has to obey the same uhunderlying machine laws that C
obeys, like alignment andpacking.
(31:46):
Uh but unless you specificallytell it, I need this to be
compatible with C, Rust isallowed to move things around to
give you the best answer.
Jim (31:58):
That's pretty neat.
Uh you know, back in my day, wedidn't we didn't have that.
We had to specifically say theorder that you wanted things.
And what made it even moreimportant, like I said, storage
was expensive.
Um the thing you had to worryabout back then was the sector
size on the disk.
And I uh uh my cobalt days, Iwas dealing with uh Texas
(32:20):
Instruments uh Business System800.
And the sector size on the discson those was 768 bytes long.
So very much like alignment, uhif we stored uh 768 bytes, that
would fit into a sectorbeautifully.
It was fast to access.
If you did a disk read, it wouldread one sector of disk.
(32:40):
Uh if you stored 769 bytes, uhit would take up two whole
sectors on the disk.
Extremely wasteful.
So that's why we had to reallybe careful about how we laid out
the data.
Um and and and uh uh otherwisewe would we would consume vast
(33:01):
amounts of wasted space.
Wolf (33:03):
Um and that's kind of how
it's kids today don't even know
what a bite is.
Jim (33:09):
You know, I wanted to
mention that.
I've uh over my years I've I'veI've uh interviewed a lot of a
lot of programmers uh either tocome work for me or to work for
my clients or whatever.
And one of the questions Ialways ask them is uh uh tell me
what a byte is.
And weirdly, most of the timethey can't tell me what a byte
(33:30):
is.
I I'm I'm astounded by that.
How do you develop software?
How can you call yourself aprofessional programmer if you
can't tell me what a byte is?
And I I don't hire those guys.
Uh I just think like that'sthat's something you need to
(33:52):
know.
Uh forget about 32-bit and64-bit integers and stuff.
Wolf (33:57):
If they don't know what a
byte is, uh I don't I'm not a
hundred percent sure I agreewith you.
Um there's lots of stuff I don'tthink a candidate needs to know.
Um but I like I have differentcriteria.
They have to be able to workwith other people and learn and
(34:20):
know where to look.
But I'm not sure, for instance,I I don't think you need to know
how to implement a quick sort,but you need to know where to
get a quick sort.
Jim (34:30):
Okay.
Wolf (34:31):
Uh and these days maybe
you don't need a byte.
Uh because let's the place whereyou would often encounter a byte
is in strings.
And strings today are oftenUnicode.
Maybe they're 16 bits at a time.
Jim (34:46):
Yeah, yeah, Unicode, UTF-8.
That that just blows the doorwide open when you're talking
about character strings, doesn'tit?
A character can be uh betweenone and four bytes long.
So now a single character, umuh, you know, if it's the letter
A, that's gonna fit in in onebyte.
Wolf (35:06):
Uh but if it's just go
straight from the eighth
character to the eighth byte.
It just doesn't work that wayanymore.
Right.
So my my and do I care if aprogrammer knows what a byte is?
I mean I kinda do because it'sso fundamental.
Maybe you don't actually use itanymore.
(35:28):
Um, but it's the same as knowingwho Rembrandt is.
Um you are not gonna paint likeRembrandt, you're not gonna deal
with any Rembrandt paintings,but you really ought to know who
he is.
Um maybe you're not gonna use abyte.
I don't know.
I don't know.
Jim (35:44):
It it feels like
fundamentals to me.
It feels like fundamentals tome.
And I think you ought to knowthe difference between an int
and a float.
I I think uh you you should bethinking about uh uh an integer
being 32 bits versus a doublebeing 64 bits.
If you're working on a program,uh especially if you're in a in
a strictly typed language likeC, um if you're trying to store
(36:08):
a number in an int, you need toknow what the limitations of
that int are.
What can you fit into 32 bits?
Right?
Maybe in this case, I have amuch bigger number.
Uh you know, I've got this uh uhyou know uh 18-digit number.
You gotta think of how how bigof a uh uh uh store, how much
(36:29):
storage do you need to allocatefor that?
What data type do you need touse for that?
Wolf (36:34):
Yeah, easy in Python, hard
in C.
Jim (36:37):
Yeah.
Yeah.
Um anyway.
Uh I I wanted to mention brieflythere are types in uh well,
there's certainly types inlanguages.
We talked a lot about uh the Clanguage.
Um there are types in databases.
I use Postgres.
Um it's it's like MySQL, it'slike SQL Server, it's like
Oracle.
There are data types in in adatabase.
(36:58):
Uh Postgres has text, varkar,car, uh they have integers, 32
bits, they got small int, that's16, they got big int, that's 64.
Sounds an awful lot like uhshort int and and uh long to me.
Uh they got a Boolean data type,they got decimal and numeric.
Um, this is a good chance for meto talk.
Uh, you know, if you're doingfloating point, if you're let's
(37:20):
say you're writing in C, andI've seen people do this.
When you deal with money, I'mtalking about writing a program
that handles currency.
People are tempted to say, well,you know, it's got a decimal
point, so it should be a float.
Oh boy, are you in a world ofhurt if you start doing that?
Uh float is uh is interesting,it's not exact.
(37:43):
Uh it can be very, very, very,very accurate, very close, but
it's not exact.
Um if you uh you know forinstance, you you take um you
take a uh uh in a value, adollar value, say$100, and you
wanna um you want to multiply itby 6%.
(38:04):
So you multiply it by 0.06,right?
Because you want to get the taxon it.
Um 0.06, that's a floating pointnumber, right?
It's a decimal number.
You multiply that by 100, youdon't get$6.
You might get 6.000000001.
(38:25):
You might get uh uh 5.999998,you might get something like
that.
It's not six.
Uh people run into trouble allthe time with that.
You end up with rounding errors,you end up with all kinds of
goofy things.
So uh when you're dealing withum dollars or currency of any
kind, whether it's euros ordollars or yen or whatever, you
(38:47):
don't want to store it in afloating point number.
Uh what I've always done is I Iconvert that number to cents.
I I uh you know, if I have ahundred dollars, well that's uh
uh ten thousand cents.
Right?
Uh if if I want to uh if I wantto represent six percent, um uh
(39:10):
well what do I do?
I gotta I gotta do the maththere, but you want to multiply,
you know, it's six, you multiplythat by your your ten thousand,
uh, and you get a number, andthen you might have to divide
that to get the the percent.
You have to divide it by ahundred to get your percent.
Um you have to you gotta bethinking in terms of data types
to know that what you're tryingto represent will actually fit
(39:31):
in that data type.
And again, floats are not aplace, floats and doubles are
not a place to be storing money.
Wolf (39:37):
See, you're making me
think about Rust again because
uh Rust has uh uh these thingscalled Rust knows that certain
data types support certainoperations.
Okay.
And one thing that uh your thetypical language you use doesn't
(39:59):
know.
But as a programmer, you do knowis that you can't compare two
floats.
That that doesn't give you theright answer.
You have to see if two floatsare close enough together.
That's what you call equal.
For instance, Rust has thenotion that some types support
(40:23):
the properties of uh equality.
You can tell if they're equal ornot.
You can test that.
And you can also tell if they'reorderable, a couple other
things.
And integers you can compare forequality.
They have this type, this trait.
And floats don't.
If you try to use equal equalwith floats, the compiler will
(40:45):
tell you you're not allowed to.
Jim (40:47):
So that fails at compile
time.
That's pretty cool.
C won't do that.
C will let you compare twofloats.
Uh and it'll work just fine.
Well, it'll compile just fine.
It may not give you the answeryou're expecting uh at runtime,
which you know if you can catchit at compile time, you're
you're ahead of the game, right?
(41:09):
Um there's lots of other typesin databases.
Uh Postgres even has types forJSON data and binary data and
UUIDs and uh uh internetaddresses and all kinds of
stuff.
It's it's pretty cool.
Um uh but let's let's um let'sit's enough talking about just
(41:30):
the CPU.
Let's talk about languages for aminute because there are
strictly typed languages, thoseare languages like Rust was uh
uh Wolf was talking about Rust,that's a strictly typed
language.
C, C, those are strictly typed.
Uh uh and really to me the thething that uh makes me think
that it's strictly type is if Ihave to declare the variable
(41:52):
before I use it, and I've had tosay what it is.
You know, a lot of languages,the dynamic languages, you don't
have to do that.
But in C and C and C sharp andRust and Swift and lots of other
languages, you have to say upfront what that variable type
is, and then you better makesure that's what you put in it.
(42:13):
Um so uh strictly typedlanguages, I named a bunch of
them, but also you get likeTypeScript that takes JavaScript
and sort of puts a type systemaround it, and the then the
compiler for that enforces uhyour types.
SQL is definitely a typedlanguage.
You uh you know, you can'tcompare an int and a string.
(42:34):
Um the the the the SQL uh uhengine will catch that and and
uh tell you you can't do that.
Uh Fortran, Cobalt, those areall strictly typed languages.
You have to declare the variablebefore you use it, you've got to
say what it is.
Um the benefits of a strictlytyped language are um uh the
compiler will stop you fromdoing something you're not
(42:56):
supposed to do with that datatype.
If you're trying to put thewrong type of data into a
variable that isn't declared tohold that type, it's gonna tell
you.
Uh it might be a warning, itmight be a hard error, but it's
gonna tell you.
Wolf (43:08):
The the way I look at it
is um there's strictly and
dynamically typed languages, andit's kind of a loose uh division
between the two.
Both of those languages, both ofthose kinds of languages have
types.
Um both of them obey all therules of the CPU.
(43:32):
Uh there they are gonna haveintegers in them somewhere,
somehow.
Maybe you don't see them, itdepends on what it is.
But when it does have an integerthat is a four-byte integer,
it's gonna obey alignment, it'sgonna obey packing.
Whether it's strict or dynamic,it doesn't matter, that's going
to be true.
(43:52):
Uh the the way you can tell alanguage is strictly typed or
dynamically typed, is that in astrictly typed language, you
make a specific name correspondto a specific type.
(44:13):
So when you say account balance,right at the place where you
make up the name accountbalance, you say, and it's going
to be a long, or whatever yourlanguage makes you say, to say
that this is going to be a32-bit or a 64-bit value.
(44:34):
And from then on, in a strictlytyped language, um two sides of
that type come into play.
One, the CPU side, that name isalways going to refer to a
specific shape in memory, a64-bit long shape.
(44:57):
And on the human side, you'vemade a promise that you will
only use that name with valuesof a compatible type.
You're only going to use it withintegers, almost always with
64-bit integers, uh, or whateveryou know, size you made.
Uh but some languages also letyou use that name in a way where
(45:21):
things get converted to 64 bitsor whatever it is.
So that's what it is in astrictly typed language.
In a dynamic language, usuallyyou just make up a name, and
that name can essentially bebound to point to.
It's not necessarily like thatname is a pointer.
(45:42):
Uh it might be, but the thething to know is that that name
is referring at any moment tosomething.
It might be referring to aninteger, it might be referring
to a string, it might bereferring to some complicated
thing that you had to make upyourself.
Uh it's dynamic because you canthen make it point to something
(46:08):
totally different.
It was pointing to an integer,and now it's pointing to a
string.
That's dynamic.
You're not making any promises.
Um, the compiler isn't going tostop you from doing uh a whole
bunch of things that thesestrictly typed languages are
going to stop you from doing,but it's still pointing to an
(46:31):
object with a specific shape.
There are still types.
They're in there.
The value that you have is sometype.
And in a lot of these languages,you can ask.
There might be a type functionor operator.
So if your name is pointing atan int and you say type of that
(46:52):
name, it's going to return int.
Uh because that's what it is atthat moment.
But there's no promises.
I didn't mean to interrupt youfor that long.
Go ahead.
Jim (47:01):
Um That's uh that that's we
were gonna get to that.
I'm glad you did.
Uh that's pretty good.
Um so let's talk about dynamiclanguages a little bit.
Uh uh, you know, there's a bunchof them.
Perl, Python, PHP.
Those are all dynamic languages.
You don't have to declare avariable before you use it.
Uh uh like with Perl, you cansay uh use strict.
(47:23):
That means you have to everyevery variable has to be named
uh ahead of using it.
Uh that way it catches uh uh uhtypos.
Right?
Um JavaScript doesn't have that.
You can just you can just referto a variable uh whether it
exists or not.
(47:43):
And sometimes it'll catch it,sometimes it won't.
Uh Python, do you clear declareuh variables at all in Python?
Do you have to say I'm gonna I'mgonna give this this uh piece of
data a name?
Wolf (47:55):
Uh variables in Python
exist because you assigned into
them.
If you later use a name thatdoesn't that was a typo, um
there is a kind of error youwill get, but it's not like it's
a compile time error.
(48:15):
Uh what happens is at runtime uhyou use it and it says hasn't
been assigned a value yet.
Um but it is assignment thatbrings a name into existence in
a Python program.
Jim (48:29):
Sure.
Well you know in Perl we can sayuse strict, and if you're if
you're a Perl programmer, Istrongly advise you always
include use strict at the top ofyour programs.
That means you have to haveassigned something to a variable
before you can use it.
Um and in fact, it doesn'treally mean you have to assign
(48:49):
something to it, you have todeclare that it's got that there
is the variable of this name.
And then anywhere in yourprogram you can refer to that.
It may or may not have a value.
If it doesn't have a value,you'll get a runtime error if
you try to read from it.
Um but uh if you don't usestrict, um you can you you can
(49:10):
just make up all kinds of namesand typos don't get caught until
runtime, and it's really, reallya pain.
Um uh JavaScript, uhJavaScript's got all kinds of
weird things, you know.
Like in JavaScript, you can sayvar and then give it a the name
of a variable, you can say letand give it the name of a
variable, you can say const andgive it the name of a variable,
but you don't have to.
(49:30):
That's kind of weird aboutJavaScript.
You can just uh later on referto a variable and a it's it's
it's a little bit weird.
Um Ruby, Go, Lisp, those are alldynamically uh typed languages.
Um you don't declare what thetype of a variable is.
Uh internally, as Wolf pointedout, it knows at any given
(49:54):
moment, it knows that this thingis is referring to an int.
Uh it has to, otherwise the CPUwouldn't know what to do with
it, or the compiler wouldn't beable to write the instructions
to tell the CPU what to do withit.
Um there are some benefits of adynamic language.
Um what we've said already makesit sound like dynamic languages
are really bad, but there aresome benefits.
Uh, first of all, it's uh Ithink in Wolf's words, it's all
(50:17):
loosey-goosey.
If you're just right trying towrite a quick program, you know
exactly what you're doing, youcan do that.
You can you can without havingto declare variables ahead of
time, you can just write thislittle program that does the
thing.
And that's kind of neat.
And uh duct typing.
What you you've mentioned ducttyping to me before.
What is that, Wolf?
Wolf (50:35):
So the exact origin of the
phrase is that if it walks like
a duck and quacks like a duck,it's a duck.
Um in Python, it's duct typing.
Uh if something uh supports theoperations that you apply to it,
like let's say in your programyou've you've got a couple of
(50:56):
names and you uh stick themtogether with a plus sign.
If those types that they areright at that moment support
plus, then that's gonna work.
Um and that's kind of a use ofduct typing.
Uh the fact that they bothsupport plus uh lets you lets
(51:18):
you use plus.
The the use of types in aprogram um on the human side,
when you so this is especiallytrue in strictly typed
languages, when you say thisname is this type, that's a
little bit for the compiler, butmost of what that's for is for
(51:41):
you, the programmer, the humanbeing, and for the people who go
to read that program.
When you say this is an int,you're not saying to the
compiler, and you're alwaysgoing to store it that way.
The compiler could have figuredthat out by itself.
Uh, what you've really done, themost important thing that you've
done, is you've made a promiseabout how you're going to use
(52:06):
that thing.
And the language that you'reimplementing in can check
whether you keep your promises.
And it can refuse to compilewhen you don't.
Uh and any place where you don'tkeep your promise, uh that's
almost certainly a mistake.
It's an error, a programmingerror that you made, that you
(52:29):
want to get caught as early aspossible.
Um and so that's the great thingabout strictly typed languages,
um, and the opposite of that,where you don't make any
promises, um that's theprogramming equivalent of move
(52:50):
fast and break things.
You can um a good Pythonprogrammer uh and a good C
programmer in Python you'remaking less promises, you can
use fewer words, it could verywell be that you can more
(53:13):
quickly implement the same levelof functionality that a C
programmer could.
They would need more words tosay the same thing that you
said, and it might take themlonger.
Now the C programmer can dosomething you can't.
The C programmer can find moremistakes before actually running
(53:39):
the program, and the Cprogrammer can write a bigger
program uh with a betterlikelihood of running the first
time than a Python programmer uhwriting a program of the same
size.
So it's the existence of thesepromises that help you find
(54:03):
mistakes and big build biggerimplementations.
Uh both languages are good whenyou're applying them to the
right problem uh with the rightconstraints.
Uh the it's the problems you'resolving and the the skills of
your team that help you pick thelanguage that's right for that
(54:26):
situation.
Uh both are good.
I happen to love Python, Ihappen to love C.
Jim (54:33):
Yeah, I uh there are there
are places to use both.
Um it it depends on what you'retrying to do.
And and uh I've written an awfullot of Perl code and it's it's a
dynamic language, and uh I I Icouldn't have moved that
quickly, uh I think in astrictly typed language.
(54:53):
Um but as as a project manager,um a strictly typed language,
like you said, would catch theerror, the compiler would catch
the error.
I'd much rather catch the errorat compile time than at runtime,
because I'm not in in my code,I'm not always gonna run every
branch of execution myself, butmy users have a way of running
(55:17):
things, uh, and I hate it whenthey catch the air.
I want my compiler to doeverything it can for me.
Uh that's why I turn on usestrict, at least with with
Pearl.
Um yeah, so I I was uh as wewere preparing for this episode,
uh I was talking to a friend ofmine.
Uh yes, Wolf, I have otherfriends besides you.
(55:41):
Uh at least one.
Um and he was asking, um, if youstore uh a number in a in a
dynamic language, let's let'spick Pearl, for instance, if you
store uh the value 10 in avariable, um and uh internally
it's probably gonna assign a uh32-bit integer, if that's the
(56:02):
the machine architecture, right?
Uh it's gonna assign that uh a32-bit integer to store it.
Um and then you add a muchlarger number to it.
How does the language deal withthat?
Does it uh under the, you know,let's say I've got the value 10,
and I add uh uh uh six trillionto it.
(56:24):
What happened?
Do I overflow?
What what happens in general?
Wolf (56:29):
In Perl, I don't know,
because I don't I don't know
much about Perl.
In Python, I actually happen toknow this exact problem.
Uh and Python has the thing thething that it shows human beings
is that there's a type callednumber.
Um and it turns out that thesize of the number you're trying
(56:53):
to work with controls what theactual underlying inside the
computer implementation of thatnumber is.
If you're using a very smallnumber, something less than 254,
um not only is that numberstored in the smallest, but you
need an object.
(57:14):
If if it if you want the number200, the number 200, but you
want that thing to be an objectin Python, there is an object
for 200 and it's a singleton.
There's only one.
Every 200 that you have in yourprogram is that same object.
(57:34):
If what you have is bigger than254, uh but small enough to fit
in 32 bits, um, then it'sprobably a very small Python
object, not much bigger than awrapper around the 32-bit value.
And no matter whether that's thesame as some other uh number
(57:57):
somewhere else in the program,they are each their own object.
Um and if your number getsreally big, bigger than a 32-bit
value or or whatever the limitis, um, it gets represented by a
special growing uh datastructure that can represent uh
(58:18):
almost any sized uh number,integer anyway, uh limited only
by the amount of memory youhave.
And the single name that youhave pointing to this number,
um, no matter what you do tothat number, the underlying
thing that name points to willfluidly switch back and forth to
(58:42):
the appropriate implementationwithout telling you anything,
without changing the name,without revealing what it is uh
from you using uh whatever typeof or type operator the language
gives you.
It just is going to do the rightthing.
Now that doesn't mean you can'ttell what that thing is.
(59:04):
Uh Python is kind of a gluelanguage in a lot of ways, and
it has a special relationshipwith, for instance, C.
It has a C API boundary that youcan get to.
Uh in C, you can write C codethat sees one of these Python
objects and can look at theobject and tell which one of
(59:28):
those things I just said it is.
But on the Python side, youdon't know.
So that's how Python does it.
Uh I'm sure what Perl does isplenty sophisticated.
Uh I just don't know what it is.
Jim (59:42):
Okay, so yeah, it it morphs
that little variable to it it
bends it to be whatever it needsto be.
Right.
Um let's get on to uh we talkeda lot about the types that are
built into the to the uh the tothe to the language, uh if it's
a strictly typed language.
(01:00:03):
Uh but there's also somethingcalled user defined types.
You can define your own types.
That's kind of neat.
If you're in C, you can dosomething called a type def,
which lets you basically uh nameyour type.
You could easily say typed uh II forget the order of this X,
but but like uh typed uh uh uhtypedef uh int um my int.
(01:00:27):
And it lets you create avariable uh a data type called
my int.
Right?
That's not very important whenyou're just dealing with these
simple little data types.
That becomes much more importantwhen you're dealing with
something else that we want totalk about and that is structs
uh uh structures in C.
It's it's a struct uh and that'sa um an aggregation of types is
(01:00:47):
that is that what you'd say yeahI'd call that an aggregate type
you used exactly the right wordyeah an aggregate type uh so you
might declare uh uh in C youmight declare something called a
coordinate which would have uhat least two two variables an X
and a Y and a three-dimensionalthing it might have th a Z.
So you create something called acoordinate you use a type def to
(01:01:10):
give that a name uh now you candeclare a variable of that new
type so so you you you uh createa structure and you use type def
and you give it a name of uhchord C O O R D that's common
right uh and then uh and thenyou uh because you use type def
to give it that name anywhere onyour program now you can declare
a chord uh you can declare avariable of type chord um and
(01:01:35):
that variable's got two or threepieces to it and that's what a
struct is uh and here'ssomething I I learned just like
last week uh I've done a lot ofstructs in C.
I did a little bit in C and uhand then I hadn't used them for
years and years and then I did alot of Swift programming and
Swift has structs and somethingthat really surprised me is
(01:01:57):
Swift structs let you havemethods on them.
I thought wow that's reallycool.
Well Wolf told me C has alwayshad that I just never realized
it I always thought classes hadmethods and structs had just an
aggregation of fields.
But no uh uh structs can have uhmethods attached to them as part
(01:02:19):
of the definition.
Wolf (01:02:20):
And in fact the thing that
I told you is that the in C, the
only difference between structsand classes is the default
visibility of the uh members.
Uh in a struct the default iseverything is public and in a
class the default is uheverything is uh I think
(01:02:43):
everything is private unless yousay otherwise and you usually
do.
Jim (01:02:49):
So it makes me wonder why
wh why are there two then why do
they have structs and classes?
And I suppose one of the reasonsis structs came from C, C didn't
have classes, structs uh the themembers were public, so maybe uh
C is still that way because alot of people moved from C to C
and it's kind of compatible.
Wolf (01:03:09):
In fact the original name
of C was quote C with classes
unquote.
Jim (01:03:17):
Yeah.
Okay.
So so maybe that's why we stillhave two of them.
It seems like you could do thesame thing with one and it might
be safer if by defaulteverything were private and you
had to declare it public inorder to access it.
But that's uh that's where we'reat, right?
And then there's uh anotherthing you can use and that's a
union.
Unions are kind of neat becausethat's basically saying two
(01:03:38):
different types of things canoccupy the same bit of memory.
Right?
Wolf (01:03:43):
Is that how you'd say it
uh I would I would what you said
sounds great except I would haveadded but not at the same time
or certainly not at the sametime.
Jim (01:03:56):
Back in days yeah back in
my COBOL days we we took
advantage of something like thisall the time.
I think it was called uh uhredefines you would declare a
record layout and then belowthat you'd declare another
record layout and you'd say itredefines the first record
layout.
So now you've got two recordlayouts pointing to the same bit
of memory that uh are totallydifferent layouts.
(01:04:17):
Um and boy you can get yourselfin trouble when you do that kind
of thing.
Wolf (01:04:21):
It's kind of a way to do
what C and C call uh cast C
casts C only has one way tocast.
Maybe I don't know what C99 has.
C has a bunch of different kindsof casts.
The one that this would be likea union or a uh re-declare
(01:04:44):
that's called a uh reinterpretcast you grab out something
inside a value uh by one typebut you had already put it in
with some other type so it'sjust like you changed types
maybe what you got isn'tsensible but but that's what you
did.
Jim (01:05:05):
Yeah it's it it's uh it's
interesting you can get yourself
in trouble uh but it can beuseful sometimes um uh enums um
you know the enums exist in Cand I used them a little bit and
I think an enum uh uh uh in cwas just an integer right that
could have a a defined set ofvalues that's what I'm
(01:05:29):
remembering um uh but enumsexist in in Swift and I think
they do in Rust as well.
Wolf (01:05:36):
They do and uh in Swift
and Rust they are not at all
like what they are in C.
In Swift and Rust they rock.
Jim (01:05:44):
Yeah they're pretty similar
I think uh I've done the Swift
stuff you've done the Rust stuffand I think what we've talked
about they're they're quitesimilar um they're they're
they're just pretty cool you canstore strings in them and and
all kinds of things in them andthen do funny uh iterations on
them like each different namethat is a possibility in an enum
(01:06:06):
can be its own totally differenttype.
Wolf (01:06:10):
And when you have an
object that is an instance of
this enum um it's discriminated.
It knows which of those thingsit is uh like it for instance uh
in r in uh Rust there's a thingcalled uh a result and a result
is an enum that's uh specific tosome particular type like you
(01:06:35):
might have a result that is apoint um and that result uh is
either um okay and the value isthat point uh or it's error uh
error I think is what the actualname is inside the the enum and
(01:06:56):
the value of that name insidethe enum is an error of some
kind uh and there's also a thingcalled option uh and the two
names are sum and none uh noneis that has no value at all it's
just that name and sum has thevalue of uh like we we were just
(01:07:17):
saying yet a point uh sum thevalue is that point um so if you
had a point to return you returna sum SOME sum some point uh and
if there was no point answer itjust didn't have one uh then you
return none uh and if you had anerror i you would return uh an
(01:07:40):
error uh so either you're gonnareturn an option or you're gonna
return a result um I mean Iguess you could return a result
that is an option if it's okayto return none um uh and that
there's a possibility of anerror in some other way uh but
(01:08:01):
those are two you could returnone the other or some
combination of them uh error isa name inside result and none is
a name inside option um so ifyou're returning an option
none's a possibility if you'rereturning a result error is a
possibility okay if you'rereturning a result where the
(01:08:23):
thing that it is uh a uh aresult of is an option then you
could return you could returnokay some point or okay none
okay or error something I I I Isort of remember early on when
you first started looking intoRust you were uh surprised that
(01:08:46):
it didn't have exceptions and Ithink as you learned surprised
and disappointed to surprise anddisappointed but as you learned
you sort of learned the power ofthis thing that you return and I
think Enum was part of yourdescription of that and uh and
then you started to realizethat's not so bad.
Jim (01:09:04):
Not having an exception.
Wolf (01:09:05):
Not only is it not so bad
the Rust way uh once you
understand it uh felt superiorto me.
Like I I once I understood I Iliked the Rust way better.
That's pretty neat how you sortof had a flip in your thoughts
about that just uh as youstarted to understand the
language and and the way itreturns uh the way it returns
things including errors um uhlet's talk about references for
(01:09:31):
just a second here uh this thisepisode is running much longer
than I expected it to uh but Ithink you wanted to talk a
little bit about references sureum so the CPU knows about
pointers uh it doesn't know awhole lot about pointers um it's
the exact arrangement of theinstructions that go to the CPU
(01:09:54):
that let the CPU understand whata pointer points to but the CPU
knows that there's a thingcalled a pointer or uh in inside
the CPU we usually say addressand there's a lot of
instructions that followaddresses and get someplace.
A reference is uh a feature of alanguage and a reference is
(01:10:18):
typically exactly like a pointerbut in always hidden from the
programmer.
So in C, for instance, or C, umif you have a pointer named X,
and it points to an int, you cansay star X and that is an int.
(01:10:43):
If you just say X, X is apointer.
Star X is the int.
So star D references thepointer.
That's exactly right.
If y and you could make X pointto some other int.
If you said X equals and thengave it a different pointer, X
is now referring to a differentint.
(01:11:05):
If you have a reference if X isinstead of being a pointer to an
int, a reference to an int, thenwhen you say X it's implemented
as a pointer but you don't haveto dereference X.
When you say X in your programit looks like and acts like an
(01:11:29):
int.
That dereference happensautomatically so that's one
difference from a pointer.
The other is when you have areference you can't point it at
a different int somewhere elsein memory.
You can't rebind it.
Yes, you can change the int uhthat's at the space it's
(01:11:53):
referencing at the place it'spointing to it was pointing at
the value three you can makethat three become a four but the
three is gone now.
The place in memory that had athree in it now has a four.
If it was a pointer you could bepointing at a three somewhere in
(01:12:13):
memory and you could make itpoint someplace else in memory
where there was a four and thatthree would still exist and
maybe other things would bepointing to it.
Jim (01:12:22):
Sure.
Wolf (01:12:23):
That's a difference.
And it's a really crucialdifference because of um these
promises, these APIs um how youcall things how things pass
through functions a thing wedidn't discuss that is very
(01:12:44):
important to types but only withrespect to how they pass through
functions.
You've got some and you're goingto call a function and you want
to tell the function thosethings a ver th something that
you pass into a function insidea function those values can be
(01:13:05):
passed by value or pass byreference.
For most small things you willpass by value.
If you had a character the thingyou send into the function is a
character a copy of what you hadbefore.
And if inside the function thatcharacter gets changed that's
(01:13:28):
not visible outside.
It got a copy it changes thecopy nobody outside the function
sees that if you passed in apointer um or reference.
Well well let me start withpointers that pointer is copied.
If inside the function thepointer name that it got gets
(01:13:56):
pointed at something else that'snever seen outside the function.
But if you changed the thingthat pointer is pointing at, if
you destroyed the three and puta four there, the caller will
see that.
That's a way to pass back uhvalues to the caller.
(01:14:19):
It's not a way people likeusually they want you to return
a value.
And there's also pass byreference.
If instead of copying in apointer you passed a reference
then you could inside thatfunction change what the
reference was the value thatreference referred to and that
(01:14:43):
is just like destroying the 3and sticking in a 4.
The caller will see that.
In C, and maybe in C99, I Idon't know enough about C99, you
can say it is a const referencethat the thing it points to
can't be changed.
(01:15:06):
In old style C, if something wasthe right size, it automatically
was passed by value.
Characters and ints wereautomatically passed by value.
And if it was a struct orsomething it was I think
automatically passed byreference.
In more modern languages and Ithink C99 is one of those uh but
(01:15:31):
certainly in C and Rust you getto say I'm gonna pass this by
reference or pass by value andeven a very large object might
end up getting copied sometimesnot even by the compiler
sometimes according to thefunctions methods that you've
(01:15:53):
supplied to that big aggregatetype that you've made.
For instance C has a type calledstring and you can say whether a
string is passed by value orreference.
A string might get copied.
Maybe that's not what you want.
Maybe you wanted to pass it byconst reference so that you got
(01:16:14):
the original string but youweren't able to do anything to
it.
This is all in the end yeah it'smachinery about how types and
objects get moved around but inthe end it's all about promises.
It's all about uh and thespecial word we make for
promises when we design themwith these strictly type
(01:16:37):
languages APIs um which areapplication uh programming
interfaces um that's what helpsus make especially libraries act
sensible they have a model andthey're designed to work like
(01:17:01):
you would guess that model worksuh so you don't have to know all
the promises up front they'resensible promises if the
library's designed right andthat's a thing that lets you
build bigger programs becauseyou don't have to look inside
the libraries to know that theyobey their promises.
(01:17:24):
Um and that's what I wanted tosay about that.
Jim (01:17:27):
Yeah thank you um we've
gone on a long time I we've just
got a few more things we want tocover uh we we talked
extensively about uh passing byvalue passing by reference uh
and that's pretty neat uh andlet's talk just a second about
casting um and that's what youdo to turn one type into another
(01:17:48):
sometimes casting is going to besomething really simple like
turning a uh uh uh a short intinto a long int right that
happens automatically for youand see if you try to assign uh
uh the value of a short into along it'll extend that value to
fit it'll take your 16 bits andand expand it out to 64 bits
(01:18:10):
that just happens automaticallyfor you if you go the other
direction like you won't loseany data that way if you go the
other direction if you try totake a 64 bit int and fit it
into a 16 bit int you're likelygoing to lose some data right in
C.
In C, yeah.
Uh and and that's why I I thinkit's important to understand
(01:18:31):
what data types you're workingwith and what happens.
Wolf (01:18:33):
You know if you have a big
long number that's uh let's say
uh uh one and a half million thevalue is uh one and a half
million and you try to c uh uhmove that into a a short you're
in trouble you're you're notgonna fit and the compiler and
in some languages for you insome languages uh there's casts
(01:18:56):
that can do the appropriatething um there's uh casts that
can cause uh the assignment tohappen but be checked at runtime
that's one kind of cast andthere's uh the compiler might
understand what values are thereso you could tell at compile
(01:19:16):
time that the thing you're doingis wrong and to ask for that
specific uh kind of checkthere's a sp special cast for
that in whatever language you'reusing.
Uh C has maybe four or five,maybe even more casts than that.
Some of which are easy and someof which are hard, uh some of
(01:19:38):
which cost you money at runtimeand some of not money.
Well money, yeah.
But cost you calculations atruntime and some which only
happen at at compile time.
Uh so casts are very importantin this way.
Uh helping you catch errors andwhere you are charged for the
performance of a cast, that's athing.
Jim (01:20:01):
Thank you.
Um I I I think uh I I thinkwe've probably talked enough
about types, don't you think?
That's uh took a lot longer thanI thought.
Uh we do have a few takeawaysthough.
Um you want me to do them?
Yeah, yeah.
You you go ahead.
Wolf (01:20:18):
I think uh the things we
want to say types about types
are pretty simple.
Uh there's the side of types uhthat the CPU knows about.
Let's call those types atomictypes.
Uh they're the ones that arebuilt into the instructions, and
the CPU knows what you'retalking about uh by the
(01:20:41):
instructions you're using.
That's one side.
Um and then there's thelanguages uh that you use.
Those go those have types beyondwhat the CPU knows.
Uh they are types built into thelanguage, um, but there's not
(01:21:02):
specific operations at the CPUlevel.
Um but there often are in thoselanguages.
Um shoot, I'm suddenly blankabout what kind of uh languages
and types those those might be.
Um but uh an example certainlyis Boolean.
(01:21:25):
There's lots of Booleanoperations um and what numbers
can be convert, what things canbe converted back and forth from
Booleans, and languages oftenknow about those things, whereas
uh CPUs only really know aboutintegers, for instance.
Uh in strictly typed languages,when you use a type, it's not
(01:21:46):
just uh saying how you want itto be stored.
You're making promises about howyou will use it, and the
strictly typed language cancheck that you obey those
promises.
And it's those checks at compiletime that help you write more
consistent and often bigger, ifthey need to be bigger, programs
(01:22:11):
more safely.
Uh and in dynamic languages,it's because you don't make
those promises uh that you cansometimes move more quickly.
And I think finally, the lasttakeaway is uh that types are
building blocks, and when youwant to model something much
(01:22:32):
more complicated than aninteger, uh and I'm gonna just
use the example of a bankaccount, uh, where you have
lists of deposits andwithdrawals, and maybe you have
multiple actual piles of money.
Um that's an aggregate type thatmight have methods of its own,
(01:22:55):
it might be stored its own way,it might be passed in and out of
functions in specific ways.
Um these user-defined types arehow you make bigger promises.
Um and in the languages that aregood at that, um, they're how
(01:23:16):
you build the language thatyou're actually going to use to
solve your problem.
Because Python doesn't solveyour problem, C doesn't solve
your problem, Postgres doesn'tsolve your problem.
None of these things bythemselves do.
It's you modeling the problemwith these bigger types and
(01:23:42):
building the language that'sspecific to these objects and
types that let you have a toolthat's really good at solving
those problems.
I think that's the set oftakeaways.
Jim (01:23:58):
I I I I think the the the
real takeaway for me is based on
what you just said, that the thethe one to remember is promises,
right?
Uh when you're dealing withtypes, you're making a promise
to the to the machine uh whatthis is gonna what what kind of
value this thing is gonna hold,and you're making a promise to
the programmer that this is uh abank account, or this is a
(01:24:21):
transaction uh that goes againsta bank account.
Promises.
Uh that that that's kind of likethe word that that sums it up, I
think.
Um I I before we finish, uh Iwant to comment about a little
cartoon that uh uh Wolf passedthis to me maybe yesterday or
the day before.
It's a cart uh it's a uh it's alittle cartoon thing.
(01:24:43):
Um and the uh you know we're anaudio-only podcast.
I can't show you what thiscartoon is, but I can describe
it.
It's quick.
Uh it's SpongeBob.
Uh in the first one panel is uhuh he says, uh Rust, when I have
an atom of difference between mytype and the expected type.
(01:25:04):
And Spongebob has got thisfrustrated look on his face.
And then the next one is uhPython, when I cast a float into
an unsigned Toyota Yaris 2023.
Uh and SpongeBob's got a bigsmile on his face uh because
he's happy.
Uh that kind of sums up, Ithink, what we've talked about.
Uh between strictly typedlanguages like Rust and
(01:25:25):
dynamically typed languages uhlike like uh Python and Pearl.
You can do all kinds of crazythings.
Um anyway, that's that's ourtakeaways.
Um so much for listening.
Yeah, take us out, Wolf.
Wolf (01:25:42):
Uh exactly what Jim just
said.
Uh we appreciate listening,especially if you got all the
way to here in such a longepisode.
Uh there are show notes whereveryou got your podcast.
The show notes ought to beattached there.
We have a website,runtimearguments.fm.
We want your feedback.
Uh feedback means so much to us.
(01:26:04):
It guides us, it corrects us, itgives us something to tell you
uh and more things to thinkabout.
Uh we already told you, I'mgonna tell you again.
Feedback at runtimearguments.fm.
Uh thanks so much, and uh we'llsee you later.
Jim (01:26:22):
Jim?
Thanks.
Thanks, Wolf.
Thanks, listeners.
Uh, I appreciate you guyslistening, and we've had a lot
of fun with this episode.
So thank you, and um lookingforward to the next one.
All right, bye bye.