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
Volte
Oct 4, 2004

woosh woosh

ExcessBLarg! posted:

I'm struggling to think of a good example of having 100+ "else if" blocks even for generated code. Surely it could be converted to some kind of dispatch mechanism, or at least convert the conditional tests into a tree form.
The idea of having to do one of a large but static number things based on a discriminator value isn't very strange. One example off the top of my head that could come up even in non-generated code would be something like implementing an interpreter/recompiler for an instruction set

if (opcode == FOO) { ... } else if (opcode == BAR) { ... } else if ...

Yes, it can be trivially converted to other forms that don't run up against the limit, but there's no inherent reason you'd start there when something as simple as this is the lowest hanging fruit and adding in a dispatch mechanism isn't necessarily going to be good for performance.

Adbot
ADBOT LOVES YOU

Athas
Aug 6, 2007

fuck that joker

ExcessBLarg! posted:

I'm struggling to think of a good example of having 100+ "else if" blocks even for generated code. Surely it could be converted to some kind of dispatch mechanism, or at least convert the conditional tests into a tree form.

Yes, I would expect the compiler to compile it efficiently, based on its knowledge of the target architecture. That's why I would generate C instead of machine code.

ExcessBLarg!
Sep 1, 2001

Volte posted:

if (opcode == FOO) { ... } else if (opcode == BAR) { ... } else if ...
This could trivially be written as switch/case even within a code generator.

Athas posted:

Yes, I would expect the compiler to compile it efficiently, based on its knowledge of the target architecture.
I suppose that a compiler that's smart enough to coalesce a bunch of if/else if conditionals into a dispatch table probably should also not have a hilariously low limit on the number of tests. At that point writing if/else if vs. switch/case is a stylistic detail that's irrelevant given that it's intermediate-language code not intended for human consumption.

Volte
Oct 4, 2004

woosh woosh

ExcessBLarg! posted:

This could trivially be written as switch/case even within a code generator.
Nobody has argued that it's the only way to write it, just that it is a place where it could conceivably come up in a non-horror situation. And a switch only works in the event that all of the conditions are simple single-value comparisons.

ExcessBLarg!
Sep 1, 2001

Volte posted:

Nobody has argued that it's the only way to write it, just that it is a place where it could conceivably come up in a non-horror situation.
To refine my earlier point, I'm struggling to think of an example of needing 100+ "else if" blocks that couldn't be written in a better (whether stylistic or performant) way.

Which is to say that the limitation itself may be defensible.

CPColin
Sep 9, 2003

Big ol' smile.
I remember when the generated code for our most extremely complicated enum got so big that the bytecode for the static initializer no longer fit under the JVM limit and we had to retool things. (or something like that)

LOOK I AM A TURTLE
May 22, 2003

"I'm actually a tortoise."
Grimey Drawer
I once worked with a C# library that generated database mapping code based on a model file. The generated code was full of lambda functions, and was all piled into a single method. With our oversized database model, the amount of lambdas in that file got so big that the method threw a StackOverflowException. It's still the only time I've seen a stack overflow that wasn't caused by recursion.

RPATDO_LAMD
Mar 22, 2013

🐘🪠🍆

ExcessBLarg! posted:

To refine my earlier point, I'm struggling to think of an example of needing 100+ "else if" blocks that couldn't be written in a better (whether stylistic or performant) way.

Which is to say that the limitation itself may be defensible.

you have some pretty weird standards if you think 100 if-elses is bad but 100 switch cases is suddenly good / "better"
it's all the same poo poo

DaTroof
Nov 16, 2000

CC LIMERICK CONTEST GRAND CHAMPION
There once was a poster named Troof
Who was getting quite long in the toof

RPATDO_LAMD posted:

you have some pretty weird standards if you think 100 if-elses is bad but 100 switch cases is suddenly good / "better"
it's all the same poo poo

If 100 if-elses or switch cases need to select from one of 100 branches based on a single value, I'd rather represent those branches with a map of functions.

But the more likely scenario is that the branches depend on multiple conditions that can be isolated into discrete components.

Xarn
Jun 26, 2015
In C and C++, switches only support direct comparisons of integral type. If-else chain can do whatever the gently caress it wants, and even have side effects in the conditions.

100 case switch is lot less scary than 100 checks in an if-else ladder

Bruegels Fuckbooks
Sep 14, 2004

Now, listen - I know the two of you are very different from each other in a lot of ways, but you have to understand that as far as Grandpa's concerned, you're both pieces of shit! Yeah. I can prove it mathematically.

RPATDO_LAMD posted:

you have some pretty weird standards if you think 100 if-elses is bad but 100 switch cases is suddenly good / "better"
it's all the same poo poo

switch with a lot of cases is easier to understand and debug than if/elseif with a lot of branches - hell i'd say the core of many emulators for cpus is just a giant switch statement - you put all the opcodes in the switch, if there are duplicate instructions you can group them up easily using labels, it's easy to deal with unimplemented opcodes using a default case rather than having to trace through the debugger trying every if statement.... even if the method is huge it's still easy to add opcodes to it if you find new ones etc. meanwhile 100 chained elsifs, the conditions are harder to read, it's harder to add new conditions, there's always the possibility that jimmy put logic in one of the conditions and made it that much harder to understand, and you have to read all the conditions and step through them all all to know if it's going to the "end" at the bottom of the method.

EssOEss
Oct 23, 2006
128-bit approved
None of which matters at all when the whole context is autogenerated interface glue code.

DaTroof
Nov 16, 2000

CC LIMERICK CONTEST GRAND CHAMPION
There once was a poster named Troof
Who was getting quite long in the toof

EssOEss posted:

None of which matters at all when the whole context is autogenerated interface glue code.

Absolutely, there's that. If we're talking about generated code that no human should inspect unless they're debugging the generator itself, none of this matters

redleader
Aug 18, 2005

Engage according to operational parameters

Xarn posted:

In C and C++, switches only support direct comparisons of integral type. If-else chain can do whatever the gently caress it wants, and even have side effects in the conditions.

100 case switch is lot less scary than 100 checks in an if-else ladder

a language for cowards, unlike the chad java script:
JavaScript code:
var isUser = o => o?.userId > 0

var match = (x) => {
  switch (true) {
    case x === 1: return 'x is one'
    case x < 100: return 'x is less than one hundred'
    case x.toLowerCase?.() === "foo": return 'x is fooish'
    case typeof x === 'string': return 'x is a string of some kind'
    case isUser(x): return `user: ${x.userName}`
    default: return 'i dunno mate'
  }
}

[1, 90, 'FOO', 'what', { userId: 100, userName: 'jeffrey of yospos' ].map(x => console.log([x, match(x)]))
i also found out today that you can sort of pattern match with destructuring:
JavaScript code:
switch (true) {
    case { prop: value } = x, value >= 5: return `x.prop is ${value}, which is at least 5`
    case { bar } = x, !!bar: return 'x.bar is ' + bar
}
the value returned from a case must strictly equal the value in the switch statement itself for the case to be executed, which is why i used that !!bar construct in the second case. (for those luckily enough to work outside of browser land, !!x is idiomatic js for converting a truthy/falsy value into a proper boolean true or false value)

not sure what version of js you need for this to work. it works in the chrome and firefox consoles, but not on jsfiddle or in the typescript playground. you also have to use the comma operator (lol) - case { prop: value } = x && value >= 5: return value doesn't match, no doubt for reasons that you could consider logical in the context of brendan eich's masterpiece of language design. i'd dig more but i've already spent far too much time on this nonsense

DaTroof
Nov 16, 2000

CC LIMERICK CONTEST GRAND CHAMPION
There once was a poster named Troof
Who was getting quite long in the toof

redleader posted:

a language for cowards, unlike the chad java script:
JavaScript code:
var isUser = o => o?.userId > 0

var match = (x) => {
  switch (true) {
    case x === 1: return 'x is one'
    case x < 100: return 'x is less than one hundred'
    case x.toLowerCase?.() === "foo": return 'x is fooish'
    case typeof x === 'string': return 'x is a string of some kind'
    case isUser(x): return `user: ${x.userName}`
    default: return 'i dunno mate'
  }
}

[1, 90, 'FOO', 'what', { userId: 100, userName: 'jeffrey of yospos' ].map(x => console.log([x, match(x)]))
i also found out today that you can sort of pattern match with destructuring:
JavaScript code:
switch (true) {
    case { prop: value } = x, value >= 5: return `x.prop is ${value}, which is at least 5`
    case { bar } = x, !!bar: return 'x.bar is ' + bar
}
the value returned from a case must strictly equal the value in the switch statement itself for the case to be executed, which is why i used that !!bar construct in the second case. (for those luckily enough to work outside of browser land, !!x is idiomatic js for converting a truthy/falsy value into a proper boolean true or false value)

not sure what version of js you need for this to work. it works in the chrome and firefox consoles, but not on jsfiddle or in the typescript playground. you also have to use the comma operator (lol) - case { prop: value } = x && value >= 5: return value doesn't match, no doubt for reasons that you could consider logical in the context of brendan eich's masterpiece of language design. i'd dig more but i've already spent far too much time on this nonsense

Javascript equality lols aside, that kind of switch(true) case anti-pattern is possible in drat near any language with a switch, and it's almost always a bad idea

LOOK I AM A TURTLE
May 22, 2003

"I'm actually a tortoise."
Grimey Drawer

DaTroof posted:

Javascript equality lols aside, that kind of switch(true) case anti-pattern is possible in drat near any language with a switch, and it's almost always a bad idea

drat near any language? You definitely can't use the pattern in C-family languages like C, C++, Java or C#, as they all require the case expressions to be made up of only constant values.

Amusingly, Java doesn't even allow something like switch (true) { case true: ... } because "boolean cannot be converted to int".

Soricidus
Oct 21, 2010
freedom-hating statist shill

LOOK I AM A TURTLE posted:

drat near any language? You definitely can't use the pattern in C-family languages like C, C++, Java or C#, as they all require the case expressions to be made up of only constant values.

Amusingly, Java doesn't even allow something like switch (true) { case true: ... } because "boolean cannot be converted to int".

Don’t worry, JEP 455 will fix this in a year or two!!!

Zopotantor
Feb 24, 2013

...und ist er drin dann lassen wir ihn niemals wieder raus...

redleader posted:

a language for cowards, unlike the chad java script:
JavaScript code:
var isUser = o => o?.userId > 0

var match = (x) => {
  switch (true) {
    case x === 1: return 'x is one'
    case x < 100: return 'x is less than one hundred'
    case x.toLowerCase?.() === "foo": return 'x is fooish'
    case typeof x === 'string': return 'x is a string of some kind'
    case isUser(x): return `user: ${x.userName}`
    default: return 'i dunno mate'
  }
}

[1, 90, 'FOO', 'what', { userId: 100, userName: 'jeffrey of yospos' ].map(x => console.log([x, match(x)]))

That's just a COND with unnecessary syntactical clutter.

DaTroof
Nov 16, 2000

CC LIMERICK CONTEST GRAND CHAMPION
There once was a poster named Troof
Who was getting quite long in the toof

LOOK I AM A TURTLE posted:

drat near any language? You definitely can't use the pattern in C-family languages like C, C++, Java or C#, as they all require the case expressions to be made up of only constant values.

Amusingly, Java doesn't even allow something like switch (true) { case true: ... } because "boolean cannot be converted to int".

"drat near any" might have been an overstatement, but it can be done in, e.g., Javascript, Python, Ruby, PHP, Visual Basic, Kotlin, Swift, Erlang, and Perl.

Elixir supports it with an explicit `cond` statement, which provides a little nicer syntax but is still a bit of a smell.

CPColin
Sep 9, 2003

Big ol' smile.
My code smells like roses

DaTroof
Nov 16, 2000

CC LIMERICK CONTEST GRAND CHAMPION
There once was a poster named Troof
Who was getting quite long in the toof
Must be nice.

Mine mostly smells like tobacco and flop sweat

pokeyman
Nov 26, 2006

That elephant ate my entire platoon.

Zopotantor posted:

That's just a COND with unnecessary syntactical clutter.

JavaScript is just a Scheme with unnecessary syntax clutter so this checks out!

CPColin
Sep 9, 2003

Big ol' smile.

DaTroof posted:

Must be nice.

Mine mostly smells like tobacco and flop sweat

If your code runs and your feet smell, you might be upside-down!

Nalin
Sep 29, 2007

Hair Elf

LOOK I AM A TURTLE posted:

drat near any language? You definitely can't use the pattern in C-family languages like C, C++, Java or C#, as they all require the case expressions to be made up of only constant values.

Amusingly, Java doesn't even allow something like switch (true) { case true: ... } because "boolean cannot be converted to int".

C# has gotten a little weirder. You could do the example given in C# just fine.
https://dotnetfiddle.net/Y0PIxo
code:
using System;
using System.Collections.Generic;
					
public class Program
{
	public record struct User(int userId, string userName);
	
	public static bool isUser(User o) => o.userId > 0;
		
	public static string match(object x)
	{
		return x switch
		{
			int i when i == 1 => "x is one",
			int i when i < 100 => "x is less than one hundred",
			string s when s.ToLowerInvariant() == "foo" => "x is fooish",
			string _ => "x is a string of some kind",
			User u when isUser(u) => $"user: {u.userName}",
			_ => "i dunno mate"
		};
	}
	
	public static void Main()
	{
		List<object> args = [1, 90, "FOO", "what", new User(userId: 100, userName: "jeffrey of yospos")];
		args.ForEach(x => Console.WriteLine($"x: {x.ToString()}, {match(x)}"));
	}
}
EDIT: Patterns are fun. https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/patterns

Nalin fucked around with this message at 18:39 on Dec 23, 2023

more falafel please
Feb 26, 2005

forums poster

pokeyman posted:

JavaScript is just a Scheme with unnecessary syntax clutter so this checks out!

the more features are introduced to a language the more it becomes a lisp

Falcon2001
Oct 10, 2004

Eat your hamburgers, Apollo.
Pillbug

CPColin posted:

My code smells like roses

My code smells awful but somehow some of the code I'm reviewing is even worse.

DaTroof
Nov 16, 2000

CC LIMERICK CONTEST GRAND CHAMPION
There once was a poster named Troof
Who was getting quite long in the toof

CPColin posted:

If your code runs and your feet smell, you might be upside-down!

hahahahaha

redleader
Aug 18, 2005

Engage according to operational parameters

Nalin posted:

C# has gotten a little weirder. You could do the example given in C# just fine.

EDIT: Patterns are fun. https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/patterns

yeah, the pattern matching switch is a lot newer, and imo only started to become properly useful in the last couple of c# versions. it's good, but not equivalent to the switch (thing) {case "a": ... } static dispatch statement in c, java, ...

i also like c#'s dedication to never naming things the same way that every other lang does. the switch expression should have been called match, Select should have been Map, etc

i guess i'm just surprised by how switch works in more dynamic langs

repiv
Aug 13, 2009

fruit company finds one weird trick to port x86 asm to ARM

intel hates it!

https://twitter.com/CoreSerena/status/1738623858788163962

ullerrm
Dec 31, 2012

Oh, the network slogan is true -- "watch FOX and be damned for all eternity!"

my curiosity got the better of me -- it's a specialized compressor for paged-out virtual memory that is hand-written to fit in a single 4KB page:

https://opensource.apple.com/source/xnu/xnu-4570.1.46/osfmk/arm64/WKdmCompress_4k.s.auto.html

Sadly, this is not the worst sin that I've seen Apple commit in their libraries. Nearly every mature iOS app has one of two things:

A) A bunch of impl swizzling to work around various bugs
or
B) A refusal to run on anything less than the latest iOS version

ullerrm fucked around with this message at 06:21 on Dec 24, 2023

Zopotantor
Feb 24, 2013

...und ist er drin dann lassen wir ihn niemals wieder raus...

more falafel please posted:

the more features are introduced to a language the more it becomes a lisp

everything evolves toward perfection

Polio Vax Scene
Apr 5, 2009



lisp, the crab of programming

Dijkstracula
Mar 18, 2003

You can't spell 'vector field' without me, Professor!

more falafel please posted:

the more features are introduced to a language the more it becomes a lisp

Polio Vax Scene posted:

lisp, the crab of programming

Consinization

Beef
Jul 26, 2004

Polio Vax Scene posted:

lisp, the crab of programming

NihilCredo
Jun 6, 2011

iram omni possibili modo preme:
plus una illa te diffamabit, quam multæ virtutes commendabunt

Dijkstracula posted:

Consinization

car-cons-ization was right there

Dijkstracula
Mar 18, 2003

You can't spell 'vector field' without me, Professor!

NihilCredo posted:

car-cons-ization was right there

:negative:

Doc Hawkins
Jun 15, 2010

Dashing? But I'm not even moving!


they cdrnt think of it

leper khan
Dec 28, 2010
Honest to god thinks Half Life 2 is a bad game. But at least he likes Monster Hunter.

NihilCredo posted:

car-cons-ization was right there

car-cons-inogens

Volmarias
Dec 31, 2002

EMAIL... THE INTERNET... SEARCH ENGINES...
Guess they just cdr-n't do it

Adbot
ADBOT LOVES YOU

bobthenameless
Jun 20, 2005

they could have if they used a claw with current continuation

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