If Bobcat Goldthwait’s goal as a filmmaker is to avoid mainstream success at all costs, then “World’s Greatest Dad” may be the pinnacle of his career. Calling it a dark comedy doesn’t do justice to the word “dark,” nor does it adequately convey how funny it is. To enjoy the film requires not just the customary suspension of disbelief but the suspension of taste.

All of the preceding is meant as a compliment, by the way. “World’s Greatest Dad” isn’t for everyone (what movie is?), but it’s fully committed to its insanely morbid ideas. No chickening out for Bobcat! When he decides to do something, he does the hell out of it.

The star is Robin Williams. Yes, I know, it’s been ages since Williams top-lined a comedy that wasn’t awful, but that is the magic of Bobcast! (And of not giving Williams free rein to ad lib, I suspect.) Williams plays Lance Clayton, a high school English teacher and the divorced father of a 15-year-old jerk named Kyle (Daryl Sabara, from “Spy Kids”), whom we’ll get to in a minute. Lance teaches a sparsely attended poetry class and has a casual romantic relationship with a fellow teacher named Claire (Alexie Gilmore). He’s also a frustrated writer, the author of unpublished novels with titles like “Door-to-Door Android” and “The Narcissist’s Life Vest.”

He doesn’t have his own son as a student, and it’s just as well. I was being kind when I called Kyle a jerk. He’s addicted to Internet porn, hateful toward all his classmates (and they return the sentiment), contrarian and obstinate in all interactions with his father and other adults, and just an all-around first-class slimy bastard. He is the type of belligerent teenager who calls everyone a “fag” and claims to hate literally everything except for pictures of naked women. He even says he hates music. All music. “The only thing queerer than music are the people who like it,” he says.

What can you do with a brat like that? Lance walks on eggshells around his son and wonders how Kyle’s lone friend, the quiet and respectful Andrew (Evan Martin), can tolerate him. How awful to have a child that you’re biologically compelled to love but that under any other circumstances you would hate.

Then things are changed by a major event. I’m hesitant to describe it. It occurs 40 minutes into the film, which is just far enough to put it into “spoiler” territory, and the trailer doesn’t reveal it. I will just say that Kyle suffers an embarrassing accident and his dear old dad shuffles the facts around to save whatever dignity Kyle has left. In the process, Lance inadvertently turns his loathsome son into a folk hero, suddenly beloved by classmates who hated him before. Moreover, Lance is also able to exercise his creative writing skills as he manufactures diaries and letters supposedly written by Kyle. Lance is finally a star — except that it’s a sham, it’s his son who’s really famous, and it’s all based on a twisted series of lies anyway.

It’s OK if certain elements remind you of “Heathers.” Heck, it’s a good thing in general to be reminded of “Heathers” now and then. Goldthwait’s vision is unique, though. In addition to having written a screenplay with razor-sharp dialogue, he also demonstrates remarkable artistic competence. One sequence, set to The Deadly Syndrome’s “I Hope I Become a Ghost,” shows the aftermath of Kyle’s accident and Lance’s deception in a way that’s funny, creative, and even touching. You could be moved to tears without forgetting that what’s going on is deeply absurd.

Robin Williams’ performance is his most likable and entertaining in at least a decade. There’s genuine complexity in Lance Clayton, in the way he lets himself get pushed around by his son and girlfriend, in the way his need for validation competes with his moral compass, and in the way he ultimately finds absolution. (That the absolution involves some labored symbolism only hurts the film a little.) Lance does some ethically shady things, manipulating people for his own gain, yet we never feel like he’s a bad person. This is partially because we recognize the film as a black comedy, almost a farce, and thus don’t expect virtuous protagonists. But it’s also because, after seeing what a creep Kyle is, we feel nothing but sympathy for his beaten-down dad.

For fans of perversely dark comedy, “World’s Greatest Dad” is a treasure trove of morbid sarcasm and gleeful malevolence. Like its TV soulmates “Curb Your Enthusiasm” and “It’s Always Sunny in Philadelphia,” the film follows a story that is outlandish yet logical, with Lance getting himself deeper and deeper into a mess until there’s no way out that doesn’t involve his utter humiliation. But unlike those shows, there’s redemption in it, too — Lance actually learns something. That’s the cherry on this riotous, oddly affecting sundae.