Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8834091
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T08:56:04+00:00 2026-06-14T08:56:04+00:00

In Haskell, I believe that it is possible to alias a type in such

  • 0

In Haskell, I believe that it is possible to alias a type in such a way that the compiler does not allow references between the aliased type and the unaliased type. According to this stack overflow question, one can use Haskell’s newtype like so:

newtype Feet = Feet Double
newtype Cm   = Cm   Double

where Feet and Cm will behave like Double values, but attempting to multiply a Feet value and a Cm value will result in a compiler error.

EDIT: Ben pointed out in the comments that this above definition in Haskell is insufficient. Feet and Cm will be new types, on which there will be no functions defined. Doing a bit more research, I found that the following will work:

newtype Feet = Feet Double deriving (Num)
newtype Cm   = Cm   Double deriving (Num)

This creates a new type that derives from the existing Num type (requires using switch: -XGeneralizedNewtypeDeriving). Of course, these new types will be even more valuable deriving from other types such as Show, Eq, etc. but this is the minimum required to correctly evaluate Cm 7 * Cm 9.

Both Haskell and Scala have type, which simply aliases an existing type and allows nonsensical code such as this example in Scala:

type Feet = Double
type Cm = Double

val widthInFeet: Feet = 1.0
val widthInCm: Cm = 30.48

val nonsense = widthInFeet * widthInCm

def getWidthInFeet: Feet = widthInCm

Does Scala have a newtype equivalent, assuming that this does what I think it does?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T08:56:05+00:00Added an answer on June 14, 2026 at 8:56 am

    Yeah you using something known as Unboxed Tagged Types in scala.

    This is how Tagged is defined:

    type Tagged[U] = { type Tag = U }
    type @@[T, U] = T with Tagged[U]
    

    This allows you to do something like this

    sealed trait Feet
    
    def Feet[A](a: A): A @@ Feet = Tag[A, Feet](a)
    Feet: [A](a: A)scalaz.@@[A,Feet]
    
    scala> val mass = Feet(20.0)
    mass: scalaz.@@[Double,Feet] = 20.0
    
    scala> 2 * mass
    res2: Double = 40.0
    

    to also add CM

    sealed trait CM
    
    def CM[A](a: A): A @@ CM = Tag[A, CM](a)
    CM: [A](a: A)scalaz.@@[A,CM]
    
    scala> val mass = CM(20.0)
    mass: scalaz.@@[Double,CM] = 20.0
    

    If you want to restrict multiplication to only Feet then you could write a typeclass type multiplication function

    trait Multiply[T] { self =>
       def multiply(a: T, b: T): T
    }
    implicit val footInstance = new Multiply[Feet] {
       def multiply(a: Feet, b: Feet): Feet = Feet(a * b)
    }
    implicit val cmInstance = new Multiply[CM] {
      def multiply(a: CM, b: CM): CM = CM(a * b)
    }
    
    def multiply[T: Multiply](a: T, b: T): T = {
      val multi = implicitly[Multiply[T]]
      multi.multiply(a,b)
    } 
    

    you can then do

    multiply(Feet(5), Feet(10)) // would return Feet(50)
    

    this is the best Scala can do

    To learn more about the boxed type check out
    http://eed3si9n.com/learning-scalaz-day3

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In Haskell, lifted type products mean that there's a semantic difference between (a,b,c) and
since Haskell has such expressive type system, is there something supported directly that we
Apparently it is possible to implement Haskell such that it evaluates eagerly without changing
Does haskell-mode or some alternative package offer something akin to the wonderful inferior-haskell-type inside
In Haskell function type ( -> ) is given, it's not an algebraic data
In Haskell, is there a way to compare that all wildcards are of the
I noticed as I was playing around with Haskell today that it is possible
Is it possible in Haskell to apply the function arrow type constructor (->) to
Someone said that I might not be getting how to proper code in Haskell.
There is a common problem that F# does not natively support infix-style use of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.