Why doesn't your favorite language have first-class sum types, Jow Forums?

Why doesn't your favorite language have first-class sum types, Jow Forums?

Attached: rustenum.png (345x120, 4K)

Other urls found in this thread:

youtube.com/watch?v=7uLzaKlZSQQ
twitter.com/NSFWRedditGif

Common Wisp

Because I can just create a class with overloaded constructors you dumb autist

sealed trait IP
case class IPv4(w: Short, x: Short, y: Short, z: Short) extends IP
val myip = IPv4(127,0,0,1)

myip match {
case IPv4(127,_,_,x) => x
case IPv4(192,_,x,y) => (x,y)
}


Why doesnt your language have case classes and pattern matching?

>getting excited about features that ocaml and other functional programming languages have had for years.
Stop being a shill.

can you explain to me what the fuck I'm supposed to be seeing here?

academic boners

even C has sum types.
union {
struct {
uint8_t a,b,c,d;
} fst;
struct {
char *str;
size_t len;
} snd;
}

so you're trying to implement an IP address and you can have two representations for the same address?
that sounds like a bad idea.
sum types can be useful but in your case you should just store the address as ints and provide a function that converts a string into that.

wtf, why would you represent v6 addresses as a string? this is niggerlicious as fuck

If you think that's even close to a replacement then maybe you should reevaluate your knowledge of computer science

The enum.

Just the enum.

a rustfag wanking over enum

which is basically C union for newfags

Because it's designed in the 80's

You mean tagged unions.

variant enums are type-safe, if function recieves union type as argument it can't say which variant is that unless you also pack enum signaling it
so Rust just has fancy syntactic sugar for matching them

That's a product type

add a tag to make it a tagged union aka sum type.
this is kinda unsafe though, as it is now the programmers job to make sure that the tag is always set correctly.
struct {
int tag;
union {
struct {
uint8_t a,b,c,d;
} fst;
struct {
char *str;
size_t len;
} snd;
};
}

Imagine trying so hard to awkwardly recreate what OOP does naturally and not even having type safety.

That's not a first class sumtype

OOP adds layers of pointer indirection and doesn't offer matching.

>pointer indirection
What do you mean by this? Pointers dont even matter unless you're still coding in these assembly language with syntax sugar

>pointer indirections don't matter

Attached: drooling brainlet cover.jpg (799x707, 66K)

>c is the only language

>not using C will make extra pointer indirections go away

Attached: 1527234922201.jpg (645x588, 57K)

>ADTs aren't type safe

Attached: 1511409916831.png (645x729, 62K)

Are you implying that an enum is an ADT?
*inserts picture tiny brain*

>ADTs are just enums

Attached: Making+good+use+of+those+new+images+from+the+recent+_0cea279451426ea1db205ad8635ef918.jpg (645x729, 40K)

Thats what you said not me

because i can't think of a single real-world example of when this would be acceptable. just structure your data better to begin with and stop using these stupid meme features because all this kind of shit ever does is allow substandard programmers to write more obscure bugs.

> his favorite language doesn't validate ip address

it's not builtin and it doesn't have nice syntax, but it is still first class.
It can be passed and returned from functions, it can be constructed at runtime, assigned to a variable, and it can be tested for equality.

Why doesn't yours have unsigned 128 bit integers?

metatables.

>his favourite language doesn't have first-class GADTs
type 'a t = | Array : 'a array -> 'a t
| Bytes : bytes -> 'a t

>his favorite language isnt based beaverlang

Attached: beaver.gif (480x268, 3.19M)

In c we call that a switch statement

>switch
>type safety

sorry in c we call that [Segmentation fault]

Why use meme languages that will be dead in 3 months when you can use PHP.

Why use PHP when you can save yourself and commit suicide instead

a bunch of if statements is better

>he thinks it segfaults if it's wrong
You wish it would always segfault. Moreover
typedef enum {
A,
B
} SomeEnum;
typedef enum {
C,
D
} SomeOtherEnum;
...
SomeOtherEnum e = C;
...
switch(e){
case A:
printf("OP is a faggot\n");
break;
case B:
return 1;
break;
}

How can anyone propose this is the same as pattern-matching is beyond me. Type safety is the first feature you lack with switch, there is also the pattern-matching part where you can't do
whatis x = match (isMale x, sucksDick x) with
| (true, true) -> Faggot
| (false, true) -> Thot
| (_,_) -> WhoCares

Because my language is powerful enough to express it as a library feature without additional overhead.

>>what is enum class

C++ could already do this without being a diversity hire language.

>enum class
>C
also
>C++
>pattern-matching

A Rust enum is a sum of products. Explain how that's not algebraic.

Terry would be disappointed in you
youtube.com/watch?v=7uLzaKlZSQQ

are you really using "switch statement" as verb?

using IPv4 = std::tuple;
using IPv6 = std::string;
using IpAddr = std::variant;

IpAddr local = IPv4(127, 0, 0, 1);
IpAddr loopback = IPv6("::1");

To be fair, std::variant is a very new feature and most codebases aren't using it.

std::variant is kind of bad desu, I'd be surprised if someone hadn't already made something better.

What language is this?

Switch statements are not the same thing as match statements, though they are used for similar purposes

Because of std::visit and lack of pattern matching?

That, but there are also some issues with questionable implicit conversions and there's no easy way to test if a particular instantiation of std::variant for some set of types is liable to enter the empty-by-exception state.

>readthefilename.png

not him but is this bait