Coding: Fleeting Thoughts

A place to discuss the implementation and style of computer programs.

Moderators: phlip, Moderators General, Prelates

User avatar
Sizik
Posts: 1158
Joined: Wed Aug 27, 2008 3:48 am UTC

Re: Coding: Fleeting Thoughts

Postby Sizik » Tue Sep 06, 2016 7:46 pm UTC

That's nothing compared to having to loop through a table where the rows don't actually exist until they are scrolled into view.
gmalivuk wrote:
King Author wrote:If space (rather, distance) is an illusion, it'd be possible for one meta-me to experience both body's sensory inputs.
Yes. And if wishes were horses, wishing wells would fill up very quickly with drowned horses.

User avatar
Xeio
Friends, Faidites, Countrymen
Posts: 5086
Joined: Wed Jul 25, 2007 11:12 am UTC
Location: C:\Users\Xeio\
Contact:

Re: Coding: Fleeting Thoughts

Postby Xeio » Tue Sep 06, 2016 8:31 pm UTC

Ok, you got me there, that'd be... interesting.

User avatar
Xanthir
My HERO!!!
Posts: 5215
Joined: Tue Feb 20, 2007 12:49 am UTC
Location: The Googleplex
Contact:

Re: Coding: Fleeting Thoughts

Postby Xanthir » Wed Sep 07, 2016 7:02 am UTC

Xeio wrote:

Code: Select all

driver.FindElementsByXPath(...);

Code: Select all

driver.FindElement(By.XPath(...));


?????

Also: using XPath?????????
(defun fibs (n &optional (a 1) (b 1)) (take n (unfold '+ a b)))

korona
Posts: 495
Joined: Sun Jul 04, 2010 8:40 pm UTC

Re: Coding: Fleeting Thoughts

Postby korona » Wed Sep 07, 2016 7:20 am UTC

I don't do much GUI stuff outside of the occasional web app experiment so excuse my ignorance: What is so bad about XPath? Is it completely obsoleted by CSS selectors?

User avatar
Xanthir
My HERO!!!
Posts: 5215
Joined: Tue Feb 20, 2007 12:49 am UTC
Location: The Googleplex
Contact:

Re: Coding: Fleeting Thoughts

Postby Xanthir » Wed Sep 07, 2016 7:30 am UTC

Yes. There are a few minor things it can do that Selectors never picked up (and at this point, quite a few things Selectors can do that XPath never picked up) (I'm really talking about XPath 1, which is the only version that got anywhere near the web; XPath 2 has lots more crazy stuff), but as far as the web is concerned, Selectors won *decisively*. Largely, I think, by virtue of being the selection language of CSS, and who wants to learn a second selection language for the DOM?

XPath only exists now as a curiosity that's still vaguely supported on the web for compat reasons, and so it strikes me as extra weird to see it actively used on webpage-related code in Java.

There's nothing fundamentally wrong with it as a tech, tho. XPath and Selectors are 90% identical; their differences are largely incidental and could have gone either way had history warped a little.
(defun fibs (n &optional (a 1) (b 1)) (take n (unfold '+ a b)))

User avatar
Xeio
Friends, Faidites, Countrymen
Posts: 5086
Joined: Wed Jul 25, 2007 11:12 am UTC
Location: C:\Users\Xeio\
Contact:

Re: Coding: Fleeting Thoughts

Postby Xeio » Wed Sep 07, 2016 2:06 pm UTC

Xanthir wrote:
Xeio wrote:

Code: Select all

driver.FindElementsByXPath(...);

Code: Select all

driver.FindElement(By.XPath(...));

?????

Also: using XPath?????????
This is mostly me doing a proof of concept, so not everything is quite check-in quality... (I did actually make the calls the same after I was doing some refactoring).

As for XPath vs CSS, not any particular reason, probably just that some of the first Selenium examples I found were using XPath.

One thing I can't seem to find easily is a way to do a child lookup in CSS though... The equivalent of this, which finds all input elements that are children of the seleniumElement object.

Code: Select all

seleniumElement.FindElement(By.Xpath(".//input"))


My Google-Fu may be bad though.

User avatar
Xanthir
My HERO!!!
Posts: 5215
Joined: Tue Feb 20, 2007 12:49 am UTC
Location: The Googleplex
Contact:

Re: Coding: Fleeting Thoughts

Postby Xanthir » Wed Sep 07, 2016 2:17 pm UTC

You want the Combinators section of the Selectors spec.

The answer is just a space. ".foo .bar" selects all .bar elements that are descendants of .foo elements. If you want only direct children, ".foo > .bar"
(defun fibs (n &optional (a 1) (b 1)) (take n (unfold '+ a b)))

User avatar
phlip
Restorer of Worlds
Posts: 7543
Joined: Sat Sep 23, 2006 3:56 am UTC
Location: Australia
Contact:

Re: Coding: Fleeting Thoughts

Postby phlip » Wed Sep 07, 2016 2:26 pm UTC

Sure, that lets you find the .bar elements that are children of any .foo elements. But how to find the .bar elements that are children of a specific .foo element? Specifically, the one I have in this variable here?

There's Element.querySelector when you're looking specifically for descendants, but nothing you can use like the other combinators...

Code: Select all

enum ಠ_ಠ {°□°╰=1, °Д°╰, ಠ益ಠ╰};
void ┻━┻︵​╰(ಠ_ಠ ⚠) {exit((int)⚠);}
[he/him/his]

User avatar
Xeio
Friends, Faidites, Countrymen
Posts: 5086
Joined: Wed Jul 25, 2007 11:12 am UTC
Location: C:\Users\Xeio\
Contact:

Re: Coding: Fleeting Thoughts

Postby Xeio » Wed Sep 07, 2016 2:29 pm UTC

phlip wrote:
Sure, that lets you find the .bar elements that are children of any .foo elements. But how to find the .bar elements that are children of a specific .foo element? Specifically, the one I have in this variable here?
^Exactly this, usable with the Selenium library specifically.

User avatar
Xanthir
My HERO!!!
Posts: 5215
Joined: Tue Feb 20, 2007 12:49 am UTC
Location: The Googleplex
Contact:

Re: Coding: Fleeting Thoughts

Postby Xanthir » Wed Sep 07, 2016 11:34 pm UTC

Yeah, `foo.querySelector(".bar")` finds the descendants. If you want children specifically, `foo.querySelector(":scope > .bar")` (the :scope pseudo matches the element the method is called on).

Unfortunately you can't use + or ~ with this, because querySelector was misdesigned and filters the results to be descendants of the scoping element. The DOM spec previously defined the better-designed .query() function, but it's been temporarily removed until JS/WebIDL allow Array subclassing properly (we don't want to return *another* weird Array-like class, like querySelector() does).
(defun fibs (n &optional (a 1) (b 1)) (take n (unfold '+ a b)))

User avatar
Xeio
Friends, Faidites, Countrymen
Posts: 5086
Joined: Wed Jul 25, 2007 11:12 am UTC
Location: C:\Users\Xeio\
Contact:

Re: Coding: Fleeting Thoughts

Postby Xeio » Thu Sep 08, 2016 3:33 pm UTC

Ah, I may have actually been looking for a problem that didn't exist, apparently seleniumElement.FindElement(By.CssSelector(...)) already exclusively searches children, which is different behavior from By.Xpath... nothing confusing here.

But now I have a dfifferent question if I was to try and use CSS selectors. I can't find an equivalent to matching inner text of an element, like this XPath selector: //div[text()='some text']

I see :contains, but apparently that's a JQuery specific thing and doesn't work.

User avatar
Xanthir
My HERO!!!
Posts: 5215
Joined: Tue Feb 20, 2007 12:49 am UTC
Location: The Googleplex
Contact:

Re: Coding: Fleeting Thoughts

Postby Xanthir » Thu Sep 08, 2016 5:07 pm UTC

Correct, Selectors does not have that functionality. (It's far too expensive of an operation to be reasonable exposed as part of CSS, and browsers have been slow/reluctant to support Selectors features for non-CSS contexts. So far we've only got one - the "deep" combinator that reaches through shadow-DOM boundaries.)
(defun fibs (n &optional (a 1) (b 1)) (take n (unfold '+ a b)))

User avatar
Xenomortis
Not actually a special flower.
Posts: 1397
Joined: Thu Oct 11, 2012 8:47 am UTC

Re: Coding: Fleeting Thoughts

Postby Xenomortis » Tue Sep 13, 2016 10:05 am UTC

I've just fixed a bug in some code I wrote that compared Java strings using == instead of String.equals(...).
This is ridiculous. String overloads "+" to do the "intuitive" thing, but not "==".
But that's not the worst bit; the worst bit is that "==" will actually work just often enough to trap you. The unit tests for this code passed because Java will often reuse string instances.
Image

User avatar
Flumble
Yes Man
Posts: 1944
Joined: Sun Aug 05, 2012 9:35 pm UTC

Re: Coding: Fleeting Thoughts

Postby Flumble » Tue Sep 13, 2016 11:15 am UTC

How long have you been programming in Java without being very aware that == is not overloaded for strings? :o (I, for one, was made aware in the second week of computer science (couldn't manage to compare string literals with input))

Then again, the switch statement does perform String.equals since Java 7.

User avatar
Xenomortis
Not actually a special flower.
Posts: 1397
Joined: Thu Oct 11, 2012 8:47 am UTC

Re: Coding: Fleeting Thoughts

Postby Xenomortis » Tue Sep 13, 2016 11:39 am UTC

Oh, I've known for a long time.
But typing "==" is so instinctive that I often trip up (excluding Strings, equality testing for non primitives is really quite rare in our codebase, and it works as expected in every other language I've used).
That it sometimes "appears" works is what annoys me (largely due to the use of literals).
Image

User avatar
Yakk
Poster with most posts but no title.
Posts: 11045
Joined: Sat Jan 27, 2007 7:27 pm UTC
Location: E pur si muove

Re: Coding: Fleeting Thoughts

Postby Yakk » Tue Sep 13, 2016 1:41 pm UTC

A competent compiler suite must have warnings for that.

I make no claims about the competence of your compiler suite.
One of the painful things about our time is that those who feel certainty are stupid, and those with any imagination and understanding are filled with doubt and indecision - BR

Last edited by JHVH on Fri Oct 23, 4004 BCE 6:17 pm, edited 6 times in total.

User avatar
Xenomortis
Not actually a special flower.
Posts: 1397
Joined: Thu Oct 11, 2012 8:47 am UTC

Re: Coding: Fleeting Thoughts

Postby Xenomortis » Tue Sep 13, 2016 2:25 pm UTC

Eclipse 'Problems' tab wrote:Warnings (100 of 8365)

Yeah...

Actually, this wasn't highlighted in a warning.
And to be fair, testing for referential equality on objects may be something you really want to do, although it's almost certainly not what you have in mind when Strings are involved.
Image

User avatar
Yakk
Poster with most posts but no title.
Posts: 11045
Joined: Sat Jan 27, 2007 7:27 pm UTC
Location: E pur si muove

Re: Coding: Fleeting Thoughts

Postby Yakk » Tue Sep 13, 2016 5:42 pm UTC

Yes. So the warning is referential equality on objects that are not explicitly casted to be objects prior to the operation.

If that is too much to swallow, on objects with an Equals method.

If that is too much to swallow, just on Strings that are not cast to Object manually.

Saying "no, I really mean reference equality" on something that already supports Equals seems like something you should say explicitly, instead of having it silently generate bugs only when your unit test is complete (which has pooled the two objects).

But that is just me. Warnings with a high chance of being bugs, and not 8000 warnings about crap that doesn't matter. And builds that build with warnings-as-errors, which each warning that isn't an error manually marked in-context as not being a problem.
One of the painful things about our time is that those who feel certainty are stupid, and those with any imagination and understanding are filled with doubt and indecision - BR

Last edited by JHVH on Fri Oct 23, 4004 BCE 6:17 pm, edited 6 times in total.

User avatar
ahammel
My Little Cabbage
Posts: 2135
Joined: Mon Jan 30, 2012 12:46 am UTC
Location: Vancouver BC
Contact:

Re: Coding: Fleeting Thoughts

Postby ahammel » Wed Sep 14, 2016 4:57 pm UTC

Node has to be the only programming environment where the most popular way to do control flow is a third-party library.
He/Him/His/Alex
God damn these electric sex pants!

commodorejohn
Posts: 958
Joined: Thu Dec 10, 2009 6:21 pm UTC
Location: Placerville, CA
Contact:

Re: Coding: Fleeting Thoughts

Postby commodorejohn » Wed Sep 14, 2016 5:03 pm UTC

Suffice to say, the first time I heard of Node was the Great leftpad Debacle. That pretty well encapsulated everything I needed to know about it.
"'Legacy code' often differs from its suggested alternative by actually working and scaling."
- Bjarne Stroustrup
www.commodorejohn.com - in case you were wondering, which you probably weren't.

Tub
Posts: 311
Joined: Wed Jul 27, 2011 3:13 pm UTC

Re: Coding: Fleeting Thoughts

Postby Tub » Wed Sep 14, 2016 6:07 pm UTC

I don't understand why anyone would *want* to use javascript on the server when there are better languages available, but I found it useful for code-sharing. Have some complicated validation logic in the browser, but need to do the same validation server-side? Save yourself the maintenance headaches and just use the same code.

User avatar
Xeio
Friends, Faidites, Countrymen
Posts: 5086
Joined: Wed Jul 25, 2007 11:12 am UTC
Location: C:\Users\Xeio\
Contact:

Re: Coding: Fleeting Thoughts

Postby Xeio » Wed Sep 14, 2016 6:40 pm UTC

You get to share the same code between client and server!

Yaaay.

That code is in Javascript!

Aaaaaaa.

ThemePark
Posts: 450
Joined: Fri Jun 27, 2008 5:42 pm UTC
Location: Århus, Denmark

Re: Coding: Fleeting Thoughts

Postby ThemePark » Wed Sep 14, 2016 7:21 pm UTC

Xeio wrote:That code is in Javascript!

Aaaaaaa.

My sentiments exactly.
I have traveled from 1979 to be a member of the unofficial board Council of Elders. Phear M3

commodorejohn
Posts: 958
Joined: Thu Dec 10, 2009 6:21 pm UTC
Location: Placerville, CA
Contact:

Re: Coding: Fleeting Thoughts

Postby commodorejohn » Wed Sep 14, 2016 7:44 pm UTC

I honestly cannot fathom why anybody would use Javascript for anything they don't absolutely have to (i.e. webpages,) except for novelty value. It doesn't so much coerce types as vivisect them.
"'Legacy code' often differs from its suggested alternative by actually working and scaling."
- Bjarne Stroustrup
www.commodorejohn.com - in case you were wondering, which you probably weren't.

User avatar
ahammel
My Little Cabbage
Posts: 2135
Joined: Mon Jan 30, 2012 12:46 am UTC
Location: Vancouver BC
Contact:

Re: Coding: Fleeting Thoughts

Postby ahammel » Wed Sep 14, 2016 8:00 pm UTC

See, I don't mind js for front-end stuff at all. Most of my gripes about it are around the weak typing craziness, but in general it's a perfectly serviceable dynamic language.

The problems I have with Node are not language-level features at all, but rather a) the "don't block the event loop" nonsense, which you have to think about for every single line of code that you write, and b) the callback-based control-flow model, which I think is mostly a result of a).
He/Him/His/Alex
God damn these electric sex pants!

User avatar
Xeio
Friends, Faidites, Countrymen
Posts: 5086
Joined: Wed Jul 25, 2007 11:12 am UTC
Location: C:\Users\Xeio\
Contact:

Re: Coding: Fleeting Thoughts

Postby Xeio » Wed Sep 14, 2016 9:09 pm UTC

It wouldn't be so bad if they would just add async/await syntax to the language already...

Tub
Posts: 311
Joined: Wed Jul 27, 2011 3:13 pm UTC

Re: Coding: Fleeting Thoughts

Postby Tub » Wed Sep 14, 2016 9:52 pm UTC

Oh please. Javascript is rarely my language of choice, but it's not that bad.

You have to write the client-side validation logic in javascript anyway (unless you use one of those newfangled [favourite language]-to-javascript transpilers, but then you have two problems). And I never said that you have to write *all* of your server code in JS. Just run nodejs for the shared code and call into it from your favourite language when needed. If validation code is all that runs on nodejs, then you don't run into the problems with nonblocking callback-based APIs, either.

User avatar
phlip
Restorer of Worlds
Posts: 7543
Joined: Sat Sep 23, 2006 3:56 am UTC
Location: Australia
Contact:

Re: Coding: Fleeting Thoughts

Postby phlip » Wed Sep 14, 2016 11:52 pm UTC

commodorejohn wrote:
Suffice to say, the first time I heard of Node was the Great leftpad Debacle. That pretty well encapsulated everything I needed to know about it.

Having never used npm, the most interesting thing I learned from some of the stories from the leftpad stuff: there is a module called "is-positive", that just checks that a number is positive.

It's at version 3.1.0.

Code: Select all

enum ಠ_ಠ {°□°╰=1, °Д°╰, ಠ益ಠ╰};
void ┻━┻︵​╰(ಠ_ಠ ⚠) {exit((int)⚠);}
[he/him/his]

commodorejohn
Posts: 958
Joined: Thu Dec 10, 2009 6:21 pm UTC
Location: Placerville, CA
Contact:

Re: Coding: Fleeting Thoughts

Postby commodorejohn » Thu Sep 15, 2016 12:06 am UTC

The thing that simultaneously amused and horrified me the most about the leftpad thing was the sheer number of people I saw in the comments sections of articles about it who were arguing (quite vociferously) that it was unreasonable to blame it on programmers depending on an external third-party include for a trivial six-line function, and that the fault was actually with npm for allowing the author to remove the package from the repository. What a world, what a world.
"'Legacy code' often differs from its suggested alternative by actually working and scaling."
- Bjarne Stroustrup
www.commodorejohn.com - in case you were wondering, which you probably weren't.

User avatar
chridd
Has a vermicelli title
Posts: 762
Joined: Tue Aug 19, 2008 10:07 am UTC
Location: ...Earth, I guess?
Contact:

Re: Coding: Fleeting Thoughts

Postby chridd » Thu Sep 15, 2016 12:22 am UTC

phlip wrote:Having never used npm, the most interesting thing I learned from some of the stories from the leftpad stuff: there is a module called "is-positive", that just checks that a number is positive.

It's at version 3.1.0.
Version 1.0.0: Throws an exception if the argument isn't a number, returns true for ±0.
Version 2.0.0: Changed to return false for ±0.
Version 3.0.0: Returns false instead of throwing an exception for non-numbers
Version 3.1.0: Also returns true for positive Number objects (though the tests don't correctly test for this; there's another commit after this that fixes this), and some package configuration stuff I don't really understand.
~ chri d. d. /tʃɹɪ.di.di/ (Phonotactics, schmphonotactics) · they (for now, at least) · Forum game scores
mittfh wrote:I wish this post was very quotable...
flicky1991 wrote:In both cases the quote is "I'm being quoted too much!"

User avatar
Flumble
Yes Man
Posts: 1944
Joined: Sun Aug 05, 2012 9:35 pm UTC

Re: Coding: Fleeting Thoughts

Postby Flumble » Thu Sep 15, 2016 12:46 am UTC

chridd wrote:returns true for ±0.

...and people actually use(d) this as a module? :shock:

User avatar
Xeio
Friends, Faidites, Countrymen
Posts: 5086
Joined: Wed Jul 25, 2007 11:12 am UTC
Location: C:\Users\Xeio\
Contact:

Re: Coding: Fleeting Thoughts

Postby Xeio » Thu Sep 15, 2016 3:11 am UTC

Tub wrote:Oh please. Javascript is rarely my language of choice, but it's not that bad.
To be fair, I have a high disdain for dynamic languages. JavaScript isn't the worst of those in any case.

I really should look into TypeScript again, not that I'll ever get to use it for work stuff.

I like my errors at compile time dammit.

User avatar
Thesh
Made to Fuck Dinosaurs
Posts: 5497
Joined: Tue Jan 12, 2010 1:55 am UTC
Location: Colorado

Re: Coding: Fleeting Thoughts

Postby Thesh » Thu Sep 15, 2016 4:01 am UTC

Runtime errors are like fun little surprises. Debugging a new program is like Christmas - all those little bugs you find are like presents to unwrap.
Honesty replaced by greed, they gave us the reason to fight and bleed
They try to torch our faith and hope, spit at our presence and detest our goals

korona
Posts: 495
Joined: Sun Jul 04, 2010 8:40 pm UTC

Re: Coding: Fleeting Thoughts

Postby korona » Thu Sep 15, 2016 12:36 pm UTC

Seemingly unpopular opinion: JavaScript has the cleanest type system of all dynamic languages. Prototype based inheritance just makes so much more sense for a dynamic language than classes.

Also: JavaScript has one of the best async programming models of all imperative languages. Yes, you need an external library (unless your engine supports Promise) but then it just works. The only contenders are Python 3 and C# as they have await. There is not a single non-managed language that supports async programming well. In C/C++ you need to manage the closure state yourself which gets very ugly for the smallest non-trivial tasks or use horribly inefficient stackful coroutines. Things will get much better when await is standardized in C++. It is already implement in MSVC and clang will have it soon.

User avatar
raudorn
Posts: 347
Joined: Mon Jun 13, 2011 11:59 am UTC

Re: Coding: Fleeting Thoughts

Postby raudorn » Thu Sep 15, 2016 1:42 pm UTC

korona wrote:Also: JavaScript has one of the best async programming models of all imperative languages.


I'm curious how that works since I've mostly used JS in a webdev context where asynchronicity is usually event based (do thing X when event Y happens). I suppose JS also supports async in a more parallel programming kind of way? (do thing X whenever the resources are free, which might be on another core)

EvanED
Posts: 4324
Joined: Mon Aug 07, 2006 6:28 am UTC
Location: Madison, WI
Contact:

Re: Coding: Fleeting Thoughts

Postby EvanED » Thu Sep 15, 2016 1:53 pm UTC

korona wrote:Seemingly unpopular opinion: JavaScript has the cleanest type system of all dynamic languages. Prototype based inheritance just makes so much more sense for a dynamic language than classes.
Ugh.

I haven't used JS enough to have an opinion on the prototype system, but there is no way that, even if it's better, it can make up for the rest of the "type" system being designed when Brendan Eich was growing through his "psychopath" phase.

I think I've heard Douglas Crockford say that he views JS as a great core language with a bunch of bad design choices built on top of it, that you can mostly avoid. I view it the other way around -- it's a bunch of really nice stuff built on a core of absolute shit (the type system). You know how The Good Parts tells you that you should usually be using === and that'll avoid the pitfalls of ==? Great, that's one operator down. How do I make the other dozen shit operators behave like not shit? There's no not-shit equivalent of +, or -, or ., etc.

commodorejohn
Posts: 958
Joined: Thu Dec 10, 2009 6:21 pm UTC
Location: Placerville, CA
Contact:

Re: Coding: Fleeting Thoughts

Postby commodorejohn » Thu Sep 15, 2016 2:18 pm UTC

I'll agree that prototype-based inheritance is neat. (Though ultimately I prefer Smalltalk's classes-implemented-with-prototype-inheritance model.) But Lordy does it not make up for the myriad other sins of the language.
"'Legacy code' often differs from its suggested alternative by actually working and scaling."
- Bjarne Stroustrup
www.commodorejohn.com - in case you were wondering, which you probably weren't.

User avatar
Yakk
Poster with most posts but no title.
Posts: 11045
Joined: Sat Jan 27, 2007 7:27 pm UTC
Location: E pur si muove

Re: Coding: Fleeting Thoughts

Postby Yakk » Thu Sep 15, 2016 2:45 pm UTC

If I wrote a prototype-based inheritance system in C++, would you swoon?

Because that could be fun.

(Why use the 1980s object model that ships with C++? Roll your own!)
One of the painful things about our time is that those who feel certainty are stupid, and those with any imagination and understanding are filled with doubt and indecision - BR

Last edited by JHVH on Fri Oct 23, 4004 BCE 6:17 pm, edited 6 times in total.

Tub
Posts: 311
Joined: Wed Jul 27, 2011 3:13 pm UTC

Re: Coding: Fleeting Thoughts

Postby Tub » Thu Sep 15, 2016 4:20 pm UTC

Yakk wrote:If I wrote a prototype-based inheritance system in C++, would you swoon?

Probably, because it'd be interesting to see in how many ways it clashes with a static type system, and how you solve those problems. Take this code for example:

Code: Select all

var prototype1 = { foo: 42 }
var prototype2 = { foo: 'fourty-two' }
var obj = Object.create(Math.random() < 0.5 ? prototype1 : prototype2);
var x = obj.foo;

What type is x?

commodorejohn wrote:I'll agree that prototype-based inheritance is neat.

Yeah, "neat", but ultimately, even though prototypes provide a superset of class functionality, I never really got any advantage out of it. The common javascript engines optimize under the assumption of static classes with immutable prototypes, so that's what you use. Can you show a problem that can be solved more elegantly with prototypes than with classes?

User avatar
ahammel
My Little Cabbage
Posts: 2135
Joined: Mon Jan 30, 2012 12:46 am UTC
Location: Vancouver BC
Contact:

Re: Coding: Fleeting Thoughts

Postby ahammel » Thu Sep 15, 2016 5:27 pm UTC

Tub wrote:
Yakk wrote:If I wrote a prototype-based inheritance system in C++, would you swoon?

Probably, because it'd be interesting to see in how many ways it clashes with a static type system, and how you solve those problems. Take this code for example:

Code: Select all

var prototype1 = { foo: 42 }
var prototype2 = { foo: 'fourty-two' }
var obj = Object.create(Math.random() < 0.5 ? prototype1 : prototype2);
var x = obj.foo;

What type is x?

It's undefined, because type-checker would reject that program. That particular problem has nothing to do with prototypes, though. It's easy enough to write the same program in a dynamic language with inheritance-based classes, such as Python:

Code: Select all

class A(object): foo = 42
class B(object): foo = 'fourty-two'
obj = A() if random.choice([True, False]) else B()
x = obj.foo
He/Him/His/Alex
God damn these electric sex pants!


Return to “Coding”

Who is online

Users browsing this forum: No registered users and 12 guests