Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
Cybernetic Vermin
Apr 18, 2005

seems on shaky ground philosophically though

Adbot
ADBOT LOVES YOU

Captain Foo
May 11, 2004

we vibin'
we slidin'
we breathin'
we dyin'

The Curious Case of the Switch Statement

JawnV6
Jul 4, 2004

So hot ...

quote:

It’s not even useful as an optimization any more; I’m pretty sure our compilers are plenty smart enough by now to recognize an if tree and turn it into a jump table or binary search or whatever. It’s one of those things that feels like it’ll make a program faster, but almost certainly won’t make any difference whatsoever.
heh

raminasi
Jan 25, 2005

a last drink with no ice

hating on duff's device???

hifi
Jul 25, 2012

quote:

None of those are intended as huge complaints, and I’m not proposing we start a petition to remove switch from the next version of C or anything. I merely object… aesthetically. (And if we were really going to change C to match my aesthetic preferences, we’d start with something much more important, such as ditching the braces.)

Oh, so that's what matters

CPColin
Sep 9, 2003

Big ol' smile.
Switch statements that refuse to compile when you don't cover all the cases are good, though.

Carthag Tuek
Oct 15, 2005

Tider skal komme,
tider skal henrulle,
slægt skal følge slægters gang



switch in swift is cool

fallthrough must be explicitly specified
must be exhaustive
intervals
tuples & pattern matching

https://developer.apple.com/library...14097-CH9-ID127

qntm
Jun 17, 2009

raminasi posted:

hating on duff's device???

it's a disgrace that duff's device even compiles

Gazpacho
Jun 18, 2004

by Fluffdaddy
Slippery Tilde
:qq: triggered by control flow :qq:

Bloody
Mar 3, 2013

switch statements are super fun in verilog. you can switch on a constant and match against variables:

case(4'b0101) begin
foo[3:0]: begin ... end
{bar[2:0], baz}: begin ... end
endcase

also wildcard-matching is its own thing in two different forms and when to use which seems to be an open subject of debate: casex and casez, which can do nice things like:

casez(hello[3:0]) begin
4'b1???: begin... end // matches all hello where hello[4] == 1
4'b0101: begin ... end // matches one concrete case
{butts[1:0], 2'b??}: begin .. end // matches on hello[3:2] == butts[1:0] and ignores hello[1:0]
endcase

and because its verilog, you can (probably? i honestly dont know) match multiple cases at the same time and that (might?) be totally okay

it's probably definitely fine if the two cases don't conflict internally, for example:

1: a <= 5;
2: b <= 6;
1 only touches a, 2 only touches b. those statements can happen at the same time, nbd
but:
1: a <= 5;
2: a <= 6;
both touch a. if they happen at the same time, your behavior is probably synthesizer-dependent. a might be 5, might be 6, might catch on fire, might fail synthesis (in detectable cases)... in simulation, you'll at least get a warning that it has two drivers

i actually have no idea how much of this is right or synthesizable and i use this language a lot. i love languages where 90% of the spec is a landmine

the only patterns i ever use are switches on variables with entirely concrete matches or non-overlapping matches if wildcards are used

Bloody fucked around with this message at 18:53 on Sep 22, 2016

HappyHippo
Nov 19, 2003
Do you have an Air Miles Card?

quote:

ALGOL W was a big step towards something recognizable as a modern language. It brought us such revolutionary innovations as null, a value that crashes your program.

Symbolic Butt
Mar 22, 2009

(_!_)
Buglord

Gazpacho posted:

:qq: triggered by control flow :qq:

HappyHippo
Nov 19, 2003
Do you have an Air Miles Card?
This article has some good quotes

quote:

Perl 5, historically, did not have a switch statement. Perl 6, in its quest to include literally every single language feature ever conceived, sought to remedy this.

Captain Foo
May 11, 2004

we vibin'
we slidin'
we breathin'
we dyin'

eevee is very good

FamDav
Mar 29, 2008

Symbolic Butt posted:

they use the character for REALITY to represent True, that's badass

No, it means true. it's most frequently used as an adjective a la really/truly. saying it means reality is like saying "truth" means philosophical truth rather than "something that is true".

Symbolic Butt
Mar 22, 2009

(_!_)
Buglord

FamDav posted:

No, it means true. it's most frequently used as an adjective a la really/truly. saying it means reality is like saying "truth" means philosophical truth rather than "something that is true".

oh. whatever source I used to look this up gave an embellished meaning I guess

Cybernetic Vermin
Apr 18, 2005

typescript 2 out, with some work towards abolishing nulls, seems promising

https://blogs.msdn.microsoft.com/typescript/2016/09/22/announcing-typescript-2-0/

Malcolm XML
Aug 8, 2009

I always knew it would end like this.

Bloody posted:

switch statements are super fun in verilog. you can switch on a constant and match against variables:

case(4'b0101) begin
foo[3:0]: begin ... end
{bar[2:0], baz}: begin ... end
endcase

also wildcard-matching is its own thing in two different forms and when to use which seems to be an open subject of debate: casex and casez, which can do nice things like:

casez(hello[3:0]) begin
4'b1???: begin... end // matches all hello where hello[4] == 1
4'b0101: begin ... end // matches one concrete case
{butts[1:0], 2'b??}: begin .. end // matches on hello[3:2] == butts[1:0] and ignores hello[1:0]
endcase

and because its verilog, you can (probably? i honestly dont know) match multiple cases at the same time and that (might?) be totally okay

it's probably definitely fine if the two cases don't conflict internally, for example:

1: a <= 5;
2: b <= 6;
1 only touches a, 2 only touches b. those statements can happen at the same time, nbd
but:
1: a <= 5;
2: a <= 6;
both touch a. if they happen at the same time, your behavior is probably synthesizer-dependent. a might be 5, might be 6, might catch on fire, might fail synthesis (in detectable cases)... in simulation, you'll at least get a warning that it has two drivers

i actually have no idea how much of this is right or synthesizable and i use this language a lot. i love languages where 90% of the spec is a landmine

the only patterns i ever use are switches on variables with entirely concrete matches or non-overlapping matches if wildcards are used

verilog is cheating

hobbesmaster
Jan 28, 2008

especially since anything remotely clever is going to be synthesized straight to ground

comedyblissoption
Mar 15, 2006

hifi posted:

Oh, so that's what matters
https://news.ycombinator.com/item?id=12532798

JawnV6
Jul 4, 2004

So hot ...

Bloody posted:

and because its verilog, you can (probably? i honestly dont know) match multiple cases at the same time and that (might?) be totally okay

no, priority is inferred from the ordering and only one matches at a time. there are priority & unique keywords, but i forget what they do

casex is the more liberal, it'll stand in for casez but not vice versa

i liked verilog case statements :v:

jony neuemonic
Nov 13, 2009

Captain Foo posted:

eevee is very good

ComradeCosmobot
Dec 4, 2004

USPOL July

Captain Foo posted:

eevee is very good

i had the opportunity to work with them hacking on something years ago and yes, they are

pokeyman
Nov 26, 2006

That elephant ate my entire platoon.

Captain Foo posted:

eevee is very good

pokeyman
Nov 26, 2006

That elephant ate my entire platoon.
on the off chance someone here hasn't read php a fractal of bad design, it is very enjoyable and also written by the aforelauded eevee

Soricidus
Oct 21, 2010
freedom-hating statist shill
i mean they're wrong about switch statements being redundant, most compilers are way less smart than they're assuming and exhaustiveness checking on enums (like in java) is really useful. but they're right about most of the rest and the history bit is interesting

gonadic io
Feb 16, 2011

>>=

Soricidus posted:

i mean they're wrong about switch statements being redundant, most compilers are way less smart than they're assuming and exhaustiveness checking on enums (like in java) is really useful. but they're right about most of the rest and the history bit is interesting

case/match statements are good, switch statements are bad.

Soricidus
Oct 21, 2010
freedom-hating statist shill
case/match is great but switch is better than chained if/else

Carthag Tuek
Oct 15, 2005

Tider skal komme,
tider skal henrulle,
slægt skal følge slægters gang



swiftswitch owns stfu

Mr SuperAwesome
Apr 6, 2011

im from the bad post police, and i'm afraid i have bad news
so i started doing a swift for a dumb project and its cool

i like it much more than obj c, and the methods got rid of all the NSFoo bullcrap which is nice too

but peppering your code with "?" and "!" seems a bit of a weird way to deal with nullable types/arguments

e.g.:

code:
        try? UIImageJPEGRepresentation(image, 0.8)?.write(to: url)
        let uploadRequest = AWSS3TransferManagerUploadRequest()
        uploadRequest?.body = url
        uploadRequest?.key = id
        uploadRequest?.bucket = S3BucketName
        uploadRequest?.contentType = "image/jpeg"

        let transferManager = AWSS3TransferManager.default()!
by weird i mean it reads weird

leftist heap
Feb 28, 2013

Fun Shoe
why not wrap the uploadRequest init in an if-let block tho

Mr SuperAwesome
Apr 6, 2011

im from the bad post police, and i'm afraid i have bad news
becuase a) this is a halfass project and b) i have no idea what i'm doing

its my first time writing swift so i'm sure all my code is terrible. and the last time i did obj-c was like 3 years ago

Arcteryx Anarchist
Sep 15, 2007

Fun Shoe
i kind of like elvis operators as a way to deal with the world of null

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe
you're trying to deal with null at every place you use the thing

that's cool, you can do that, but it's usually better to figure out at some useful point whether you have a value or not

like if you're positive something can't fail, go ahead and assert that with !

and if it might, just check and bail out, maybe with guard

DONT THREAD ON ME
Oct 1, 2002

by Nyc_Tattoo
Floss Finder
yeah you still want to make an effort to avoid null, but in the cases where you can't you have an ok option for dealing with it

qntm
Jun 17, 2009
I'm beginning to think this whole null idea was a mistake from the beginning

Mahatma Goonsay
Jun 6, 2007
Yum
after using some Haskell I never want to go back from Maybe and Either.

Gazpacho
Jun 18, 2004

by Fluffdaddy
Slippery Tilde
hoare is wrong, null is good

Mr SuperAwesome
Apr 6, 2011

im from the bad post police, and i'm afraid i have bad news
i prefer the pythonic way of dealing with None but thats probably because i like + am used to python

Adbot
ADBOT LOVES YOU

Asymmetrikon
Oct 30, 2009

I believe you're a big dork!
i am glad most languages seem to have started adding option types / actual ways of dealing with acknowledging nullability of values, rather than the "let's just sprinkle (if x == null) everywhere", or, even worse, "let's pretend this code is correct even though there's no way to guarantee a null can't just get in and break the whole drat thing".

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply